格式化时间字符串yyyy 您所在的位置:网站首页 24小时制和12小时制的区别 格式化时间字符串yyyy

格式化时间字符串yyyy

2024-07-12 12:39| 来源: 网络整理| 查看: 265

时间格式化的时候HH:mm:ss和hh:mm:ss的区别

hh:mm:ss 按照12小时制的格式进行字符串格式化

如果时间处于00:00:00——12:59:59,则返回的字符串正常

如果时间处于13:00:00——23:59:59,则返回的字符串是实际时间-12小时后的值,也就是说比真实的时间少了12个小时。

例如:14:00:00进行格式化后的字符串为“2:00:00”

HH:mm:ss

按照24小时制的格式进行字符串格式化

当时间为任意一个区间,则返回的字符串都是正常的。

yyyy-MM-dd和YYYY-MM-dd的区别 import java.text.SimpleDateFormat import java.time.LocalDate import java.time.format.DateTimeFormatter DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd") DateTimeFormatter simpleDateFormat2 = DateTimeFormatter.ofPattern("YYYY-MM-dd") LocalDate localDate = LocalDate.of(2019,12,31) String qqq = dateTimeFormatter.format(localDate) String www = simpleDateFormat2.format(localDate) System.out.println(qqq) System.out.println(www)

在这里插入图片描述

解释一波: yyyy-MM-dd是真真正正的年份,但是YYYY-MM-dd代表的是基于周的年份日期,也就是说一周处在跨年中(比如这一周的周3是2019年,周4是2020年,这一周就是跨年周),那么采用YYYY-MM-dd就会把这周归入到下一年.也就会造成显示错误.这其实不是bug,就是这样设计的.所以日常在使用格式化时间日期的时候采用yyyy开头的比较好

还有大写的DD代表的是处于这一年中的哪一天,而不是处于这个月的哪一天

Date

字符串转Date

package com.test.dateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import org.junit.Test; public class String2Date { @Test public void test() throws ParseException { String string = "2016-10-24 21:59:06"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.parse(string)); } }

Date转字符串

package com.test.dateFormat; import java.text.SimpleDateFormat; import java.util.Date; import org.junit.Test; public class Date2String { @Test public void test() { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); System.out.println(sdf.format(date)); sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(date)); sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); System.out.println(sdf.format(date)); } }

Date转换也可以使用commons-lang3 下的时间工具类DateUtils/DateFormatUtils

org.apache.commons commons-lang3 3.10

注意:如果你系统已经有 commons-lang,注意如果直接替换成 commons-lang3,将会编译错误。commons-lang3 中相关类与 commons-lang 一样,但是包名不一样。

// Date 转化为字符串 DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"); // 字符串 转 Date DateUtils.parseDate("2020-05-07 22:00:00","yyyy-MM-dd HH:mm:ss"); //时间加减 Date now = new Date(); // Date 加 1 天 Date addDays = DateUtils.addDays(now, 1); // Date 加 33 分钟 Date addMinutes = DateUtils.addMinutes(now, 33); // Date 减去 233 秒 Date addSeconds = DateUtils.addSeconds(now, -233); // 判断是否 Wie 同一天 boolean sameDay = DateUtils.isSameDay(addDays, addMinutes); // 过滤时分秒,若 now 为 2020-05-07 22:13:00 调用 truncate 方法以后 // 返回时间为 2020-05-07 00:00:00 Date truncate = DateUtils.truncate(now, Calendar.DATE); 字符串转LocalDateTime,LocalDateTime转Date

字符串转LocalDateTime

String timeStr = "2019-08-26 18:03:33"; DateTimeFormatter timeDtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); //字符串转LocalDateTime LocalDateTime localDateTime = LocalDateTime.parse(timeStr, timeDtf); //LocalDateTime转Date ZoneId zone = ZoneId.systemDefault(); Instant instant = localDateTime.atZone(zone).toInstant(); Date date = Date.from(instant); LocalDateTime转为自定义的时间格式的字符串 public static String getDateTimeAsString(LocalDateTime localDateTime, String format) {   DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);   return localDateTime.format(formatter); } 将long类型的timestamp转为LocalDateTime(时间戳转LocalDateTime)

方法1

public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {   Instant instant = Instant.ofEpochMilli(timestamp);   ZoneId zone = ZoneId.systemDefault();   return LocalDateTime.ofInstant(instant, zone); }

方法2,指定时区 Instant类的ofEpochMilli()方法使用从1970-01-01T00:00:00的纪元作为参数传递的毫秒数来帮助获取Instant,即传入的参数是从1970-01-01T00:00:00开始的毫秒值。

import java.time.Instant import java.time.LocalDateTime import java.time.ZoneOffset //获得时间戳 //long milliseconds = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); long milliseconds = 1616052960373 // 将时间戳转为当前时间 LocalDateTime localDateTime = Instant.ofEpochMilli(milliseconds) .atZone(ZoneOffset.ofHours(8)) .toLocalDateTime(); System.out.println(localDateTime); println(ZoneOffset.ofHours(8))

在这里插入图片描述 关于ZoneId

println(ZoneOffset.ofHours(8)) println(ZoneId.of(ZoneId.systemDefault() as String)) println(ZoneId.of("Asia/Shanghai"))

在这里插入图片描述

时间戳转Date

import java.text.SimpleDateFormat SimpleDateFormat simpleDateFormat = new SimpleDateFormat('yyyy-MM-dd HH:mm:ss') println(simpleDateFormat.format(new Date(1616052960373l)))

在这里插入图片描述

将LocalDateTime转为long类型的timestamp(LocalDateTime转时间戳)

public static long getTimestampOfDateTime(LocalDateTime localDateTime) {   ZoneId zone = ZoneId.systemDefault();   Instant instant = localDateTime.atZone(zone).toInstant();   return instant.toEpochMilli(); }

将某时间字符串转为自定义时间格式的LocalDateTime

public static LocalDateTime parseStringToDateTime(String time, String format) {   DateTimeFormatter df = DateTimeFormatter.ofPattern(format);   return LocalDateTime.parse(time, df); }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有