Java 学习笔记 - 时间日期常用案列

Posted 笑虾

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 学习笔记 - 时间日期常用案列相关的知识,希望对你有一定的参考价值。

Java 学习笔记 - 时间日期常用案列

获取当前时间

以前

Date date = new Date();
Date date = new Date(System.currentTimeMillis());

Java8

LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
Instant now = Instant.now();
ZonedDateTime zonedDateTime = ZonedDateTime.now();

带时区的日期时间

ZonedDateTime zonedDateTimeDefault = ZonedDateTime.ofInstant(now, ZoneId.systemDefault());// 系统默认时区
ZonedDateTime zonedDateTimeE8 = ZonedDateTime.now().withZoneSameInstant(ZoneId.of("Asia/Shanghai")); // 指定东8区

转换

Instant

直接: LocalDateTime, ZonedDateTime, Date, Timestamp

Instant instant = Instant.now();

LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.of("Asia/Shanghai"));
ZonedDateTime zonedDateTime1 = instant.atZone(ZoneId.systemDefault());
ZonedDateTime zonedDateTime2 = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
Date date = Date.from(instant);
Timestamp timestamp = Timestamp.from(instant);

ZonedDateTime

直接:LocalDate, LocalTime, LocalDateTime, Instant
间接:Date

ZonedDateTime zonedDateTime = ZonedDateTime.now();

LocalDate localDate = zonedDateTime.toLocalDate();
LocalTime localTime = zonedDateTime.toLocalTime();
LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
Instant instant = zonedDateTime.toInstant();

Date date = Date.from(zonedDateTime.toInstant());

LocalDateTime

直接:LocalDate, LocalTime, ZonedDateTime, Instant
间接:Date

LocalDateTime localDateTime = LocalDateTime.now();

LocalDate localDate = localDateTime.toLocalDate();
LocalTime localTime = localDateTime.toLocalTime();
ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
Instant instant = localDateTime.toInstant(ZoneOffset.of("+8"));

Date date = Date.from(instant);

OffsetDateTime

直接:LocalDate, LocalTime, LocalDateTime, ZonedDateTime, Instant , long

OffsetDateTime offsetDateTime1 = Instant.now().atOffset(ZoneOffset.of("+8"));
OffsetDateTime offsetDateTime2 = OffsetDateTime.of(LocalDateTime.now(), ZoneOffset.of("+8"));
OffsetDateTime offsetDateTime = OffsetDateTime.now();

LocalDate localDate = offsetDateTime.toLocalDate();
LocalTime localTime = offsetDateTime.toLocalTime();
LocalDateTime localDateTime = offsetDateTime.toLocalDateTime();
ZonedDateTime zonedDateTime = offsetDateTime.toZonedDateTime();
Instant instant = offsetDateTime.toInstant();
long timestamp = offsetDateTime.toEpochSecond();

Date

直接:Instant
间接:LocalDate, LocalTime, LocalDateTime

Date date = new Date();

Instant instant = Instant.ofEpochMilli(date.getTime());
Instant instant1 = date.toInstant();

ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
LocalDate localDate = zonedDateTime.toLocalDate();
LocalTime localTime = zonedDateTime.toLocalTime();
LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();

LocalDateTime localDateTime1 = LocalDateTime.ofInstant(instant, ZoneId.of("Asia/Shanghai"));
LocalTime localTime1 = localDateTime1.toLocalTime();
LocalDate localDate1 = localDateTime1.toLocalDate();

时间戳(毫秒数)

  • 直接获取当前时间戳
// 13 位
long timestamp = System.currentTimeMillis();
long timestamp1 = Instant.now().toEpochMilli();
long timestamp2 = new Date().getTime();
long timestamp4 = Clock.systemDefaultZone().millis();
// 10 位
long ts1= LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
long ts2= ZonedDateTime.now().toEpochSecond();
long ts3= OffsetDateTime.now().toEpochSecond();
  • LocalDate -> LocalDateTime -> Instant -> long
LocalDate localDateNow = LocalDate.now();
LocalDateTime now = localDateNow.atStartOfDay();
long time = now.toInstant(ZoneOffset.of("+8")).toEpochMilli();
  • LocalDate -> LocalDateTime -> Timestamp -> long
LocalDate localDate = LocalDate.now();
LocalDateTime now = localDate.atStartOfDay();
long time = Timestamp.valueOf(now).getTime();
  • long -> Instant -> Timestamp -> LocalDateTime
long now  = 1654068798634L;
Instant instant = Instant.ofEpochMilli(now);
Timestamp timestamp = Timestamp.from(instant);
LocalDateTime ldt = timestamp.toLocalDateTime();
System.out.println(ldt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss.SSS")));

格式化

以前:DateFormat

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

Date date = new Date();
System.out.println(dateFormat.format(date)); // 2022-06-02 12:53:35

Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime())); // 2022-06-02 12:53:35

Java8: DateTimeFormatter

0. 自带的格式

格式不少,但感觉没几个用的上。 Predefined Formatters

String isoLocalDate = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(isoLocalDate);       // 2022-06-02
String isoLocalTime = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME);
System.out.println(isoLocalTime);       // 13:09:34.282
String isoLocalDateTime = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
System.out.println(isoLocalDateTime);   // 2022-06-02T13:09:34.283

1. 自定义格式

DateTimeFormatter默认还定义了一些常用的日期格式。满足不了需求我们再自定义。

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
System.out.println(LocalDateTime.now(ZoneOffset.of("+8")).format(dtf));

2. 自定义格式(支持默认值)

  • 只有【月、日】,其它设置默认值。
DateTimeFormatter df= new DateTimeFormatterBuilder()
		.appendPattern("MMdd")
		.parseDefaulting(ChronoField.YEAR_OF_ERA, 2046)
		.parseDefaulting(ChronoField.HOUR_OF_DAY, 23)
		.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 59)
		.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 59)
		.parseDefaulting(ChronoField.MILLI_OF_SECOND, 999) // 如果字符串中有毫秒,则会冲突。		
		.toFormatter();
System.out.println(LocalDateTime.parse("0524", df).toString());
// 2046-05-24T23:59:59.999
  • 只有毫秒,没有秒。将秒默认为 0
DateTimeFormatter df = new DateTimeFormatterBuilder()
		.appendPattern("yyMMddHHmm[ss].SSS")
		.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
		.toFormatter();
System.out.println(LocalDateTime.parse("2205241234.567", df).toString()); 
// 2022-05-24T12:34:00.567
  • 综合展示
DateTimeFormatter DATE_FORMAT = new DateTimeFormatterBuilder()
		.appendPattern("yyMMdd[[HH][mm][ss][.SSS]]")
		.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
		.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
		.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
		.toFormatter();
System.out.println(LocalDateTime.parse("220524", DATE_FORMAT).toString());
// 2022-05-24T00:00
System.out.println(LocalDateTime.parse("220524.567", DATE_FORMAT).toString());
// 2022-05-24T00:00:00.567
System.out.println(LocalDateTime.parse("22052412.567", DATE_FORMAT).toString());
// 2022-05-24T12:00:00.567
System.out.println(LocalDateTime.parse("2205241234.567", DATE_FORMAT).toString());
// 2022-05-24T12:34:00.567
System.out.println(LocalDateTime.parse("2205241234", DATE_FORMAT).toString());
// 2022-05-24T12:34

3. 其它

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");

String format = LocalDate.now().format(dateTimeFormatter);
String format1 = LocalTime.now().format(dateTimeFormatter);
String format2 = LocalDateTime.now().format(dateTimeFormatter);
String format3 = ZonedDateTime.now().format(dateTimeFormatter);

时间计算

加减时间

支持直接加减指定单位,返回结果。详见:Java 学习笔记 - 日期时间 LocalDateTime

计算间隔

LocalDateTime now = LocalDateTime.now();
LocalDateTime target = now.plusDays(1);

long betweenDays = ChronoUnit.DAYS.between(now, target);
System.out.println(betweenDays);	// 1
long betweenHours = ChronoUnit.HOURS.between(now, target);
System.out.println(betweenHours);	// 24

参考资料

LocalDate (Java Platform SE 8 )
LocalDateTime (Java Platform SE 8 )
DateTimeFormatter (Java Platform SE 8 )
Instant (Java Platform SE 8 )
Timestamp (Java Platform SE 8 )

笑虾:Java 学习笔记 - 日期时间 LocalDateTime

以上是关于Java 学习笔记 - 时间日期常用案列的主要内容,如果未能解决你的问题,请参考以下文章

Java学习笔记4.5.1 日期时间 - Date类与Calendar类

Java中Api知识点总结笔记带案列

《DOM Scripting》学习笔记-——第五章第六章 案列改进

知了堂学习笔记java中常用集合的理解

学习笔记20日期和时间模快

Java重温学习笔记,日期时间