欢迎访问宙启技术站
智能推送

使用这10个Java函数轻松处理日期时间

发布时间:2023-06-17 09:55:37

在Java编程中,处理日期时间是一个非常常见的任务。因此,Java提供了大量的函数和类来处理日期、时间和日期时间的操作。本文将介绍10个常用的Java函数来处理日期时间,包括格式化、解析、计算等操作。

1. SimpleDateFormat

SimpleDateFormat是Java中处理日期时间格式化的常用类。可以将日期时间对象转换为指定格式的字符串或者使用指定格式的字符串来生成日期时间对象。例如:

Date date = new Date();
String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
System.out.println(format);

输出结果为:2021-08-13 16:31:53。这里使用了"yyyy-MM-dd HH:mm:ss"格式的字符串来将Date对象格式化成字符串。

2. LocalDate、LocalTime和LocalDateTime

Java8引入了新的日期时间API,其中包含了三个类:LocalDate、LocalTime和LocalDateTime。这些类可以简单地处理日期和时间对象,并且不会受到时区的影响。例如:

LocalDate nowDate = LocalDate.now();
LocalTime nowTime = LocalTime.now();
LocalDateTime nowDateTime = LocalDateTime.now();
System.out.println(nowDate); // 2021-08-13
System.out.println(nowTime); // 16:34:16.684
System.out.println(nowDateTime); // 2021-08-13T16:34:16.684

这里分别创建了当天的日期、当前时间和当前日期时间对象。

3. Instant

Instant是Java8中新增的一个类,用于表示一个精确的时间点。例如:

Instant now = Instant.now();
System.out.println(now); // 2021-08-13T08:34:16.684Z

这里使用Instant.now()获取当前时间点,并将它格式化成字符串输出。注意,输出的时间点是格林威治标准时间(GMT),需要根据时区进行调整。

4. Duration和Period

Duration和Period分别用于表示时间间隔和日期间隔。Duration用于处理一段时间的秒数和纳秒数,而Period用于处理日、月或年之间的差距。例如:

Instant start = Instant.now();
Thread.sleep(3000);
Instant end = Instant.now();
Duration duration = Duration.between(start, end);
System.out.println(duration.getSeconds()); // 3

LocalDate date1 = LocalDate.of(2021, 8, 13);
LocalDate date2 = LocalDate.of(2020, 8, 13);
Period period = Period.between(date2, date1);
System.out.println(period.getYears()); // 1

这里使用Duration.between()计算了两个时间点之间的时间间隔,以秒为单位。而使用Period.between()计算了两个日期之间的年数差距。

5. Calendar和GregorianCalendar

Calendar是Java中处理日期时间的核心类,用于计算时间、处理日历操作等。GregorianCalendar则是Calendar的一个具体实现类。例如:

Calendar cal = Calendar.getInstance();
System.out.println(cal.get(Calendar.YEAR)); // 2021
System.out.println(cal.get(Calendar.MONTH) + 1); // 8
System.out.println(cal.get(Calendar.DAY_OF_MONTH)); // 13

这里获取了当前时间所在年份、月份和日期。

6. Date

Date是Java中最常用的日期时间类之一,但它已经过时了。Date表示一个精确的时间点,可以通过DateFormat类或SimpleDateFormat类将其格式化成字符串。例如:

Date now = new Date();
System.out.println(now); // Fri Aug 13 16:47:51 CST 2021

使用Date()获取当前时间点,并将其输出。

7. Instant.ofEpochMilli()

Instant.ofEpochMilli()可以将一个long类型的时间戳转换为Instant对象。例如:

Instant instant = Instant.ofEpochMilli(1628866087331L);
System.out.println(instant); // 2021-08-13T08:34:16.684Z

这里将一个表示时间戳的长整型数值转换为Instant对象。

8. LocalDateTime.parse()

LocalDateTime.parse()可以将一个字符串解析成LocalDateTime对象。例如:

LocalDateTime dateTime = LocalDateTime.parse("2021-08-13T16:34:16.684");
System.out.println(dateTime); // 2021-08-13T16:34:16.684

这里将一个日期时间字符串解析成LocalDateTime对象。

9. ZonedDateTime

ZonedDateTime是Java8中用于处理带时区的日期时间的类。例如:

ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("Europe/Paris"));
System.out.println(zdt); // 2021-08-13T18:34:16.684+02:00[Europe/Paris]

这里获取了欧洲巴黎时区下的带时区的当前日期时间对象。

10. Duration.toDays()

使用Duration.toDays()可以将时间间隔转换成天数。例如:

LocalDate date1 = LocalDate.of(2021, 8, 1);
LocalDate date2 = LocalDate.of(2021, 8, 13);
Duration duration = Duration.between(date1.atStartOfDay(), date2.atStartOfDay());
System.out.println(duration.toDays()); // 12

这里计算了日期2012-08-01和2012-08-13之间的天数间隔。