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

如何使用Java的时间和日期函数?

发布时间:2023-06-06 00:40:12

Java是一种面向对象的编程语言,提供了丰富的时间和日期函数,可以用于处理日期、时间、时区等相关的操作。

在Java中,时间和日期类主要由java.time包提供。下面我们将介绍该包中的一些常用类和方法:

## 1. LocalDate

LocalDate类表示日期,可以使用 now() 方法获得当前的日期,或者使用 ISO 日期格式创建新的日期:

LocalDate currentDate = LocalDate.now();
LocalDate newDate = LocalDate.parse("2021-01-01");

可以使用 getYear()、getMonth()、getDayOfMonth() 和 getDayOfWeek() 等方法获取日期的 year、month、dayOfMonth 和 dayOfWeek 等属性:

int year = currentDate.getYear();
int month = currentDate.getMonthValue();
int dayOfMonth = currentDate.getDayOfMonth();
DayOfWeek dayOfWeek = currentDate.getDayOfWeek();

还可以使用 plusDays()、minusWeeks() 等方法来进行日期的加减运算:

LocalDate newDate = currentDate.plusDays(5);
LocalDate newDate2 = currentDate.minusWeeks(2);

## 2. LocalTime

LocalTime类表示时间,可以使用 now() 方法获得当前的时间,或者使用格式化字符串创建新的时间:

LocalTime currentTime = LocalTime.now();
LocalTime newTime = LocalTime.parse("23:55:00");

可以使用 getHour()、getMinute()、getSecond() 等方法来获取时间的小时、分钟、秒等属性:

int hour = currentTime.getHour();
int minute = currentTime.getMinute();
int second = currentTime.getSecond();

还可以使用 plusHours()、minusMinutes() 等方法来进行时间的加减运算:

LocalTime newTime = currentTime.plusHours(2);
LocalTime newTime2 = currentTime.minusMinutes(30);

## 3. LocalDateTime

LocalDateTime类表示日期和时间的组合,可以使用 now() 方法获得当前的日期和时间,或者使用格式化字符串创建新的日期和时间:

LocalDateTime currentDateTime = LocalDateTime.now();
LocalDateTime newDateTime = LocalDateTime.parse("2021-01-01T23:55:00");

同样,可以使用 getYear()、getMonth()、getDayOfMonth()、getHour()、getMinute()、getSecond() 等方法来获取日期和时间的属性:

int year = currentDateTime.getYear();
int month = currentDateTime.getMonthValue();
int dayOfMonth = currentDateTime.getDayOfMonth();
int hour = currentDateTime.getHour();
int minute = currentDateTime.getMinute();
int second = currentDateTime.getSecond();

还可以使用 plusDays()、minusWeeks()、plusHours()、minusMinutes() 等方法来进行日期和时间的加减运算:

LocalDateTime newDateTime = currentDateTime.plusDays(5);
LocalDateTime newDateTime2 = currentDateTime.minusWeeks(2);
LocalDateTime newDateTime3 = currentDateTime.plusHours(2);
LocalDateTime newDateTime4 = currentDateTime.minusMinutes(30);

## 4. ZonedDateTime

ZonedDateTime类表示带有时区的日期和时间,可以使用 now() 方法获得当前的时间和时区,或者使用格式化字符串创建带有时区的日期和时间:

ZonedDateTime currentZoneDateTime = ZonedDateTime.now();
ZonedDateTime newZoneDateTime = ZonedDateTime.parse("2021-01-01T23:55:00+08:00[Asia/Shanghai]");

可以使用 getYear()、getMonth()、getDayOfMonth()、getHour()、getMinute()、getSecond()、getOffset() 和 getZone() 等方法来获取带有时区的日期和时间的属性:

int year = currentZoneDateTime.getYear();
int month = currentZoneDateTime.getMonthValue();
int dayOfMonth = currentZoneDateTime.getDayOfMonth();
int hour = currentZoneDateTime.getHour();
int minute = currentZoneDateTime.getMinute();
int second = currentZoneDateTime.getSecond();
ZoneOffset offset = currentZoneDateTime.getOffset();
ZoneId zone = currentZoneDateTime.getZone();

同样,还可使用 plusDays()、minusWeeks()、plusHours()、minusMinutes() 等方法来进行日期和时间的加减运算。

## 5. Duration

Duration类表示两个时间点之间的时间跨度。可以使用 between() 方法计算两个时间点之间的时间跨度:

LocalTime start = LocalTime.of(10, 0, 0);
LocalTime end = LocalTime.of(12, 0, 0);
Duration duration = Duration.between(start, end);

也可以使用 of() 方法创建指定的时间跨度:

Duration duration = Duration.ofHours(2);
Duration duration2 = Duration.ofMinutes(30);

可以使用 toHours()、toMinutes()、toSeconds() 等方法来获取时间跨度的小时数、分钟数和秒数等属性:

long hours = duration.toHours();
long minutes = duration.toMinutes();
long seconds = duration.toSeconds();

## 6. Period

Period类表示两个日期之间的时间跨度。可以使用 between() 方法计算两个日期之间的时间跨度:

LocalDate start = LocalDate.of(2021,1,1);
LocalDate end = LocalDate.of(2021,4,1);
Period period = Period.between(start, end);

也可以使用 of() 方法创建指定的时间跨度:

Period period = Period.ofMonths(3);
Period period2 = Period.ofDays(10);

可以使用 getYears()、getMonths()、getDays() 等方法来获取时间跨度的年数、月数和天数等属性:

int years = period.getYears();
int months = period.getMonths();
int days = period.getDays();

## 7. SimpleDateFormat

SimpleDateFormat类表示日期和时间以指定格式进行格式化和解析。可以使用如下方式创建SimpleDateFormat实例:

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

可以使用 format() 方法将日期和时间格式化为指定格式的字符串:

String dateString = dateFormat.format(new Date());

也可以使用 parse() 方法将指定格式的字符串解析为日期和时间:

Date date = dateFormat.parse("2021-04-01 10:00:00");

需要注意的是,SimpleDateFormat是线程不安全的,不能在多线程环境中共享实例。如果需要在多线程环境中使用,可以考虑使用ThreadLocal来实现线程局部变量。

## 8. Instant

Instant类表示从1970年1月1日0时0分0秒开始到指定时间点的毫秒数。可以通过 now() 方法获取当前时间点,或者使用 ofEpochMilli() 方法创建指定时间点的Instant对象:

Instant now = Instant.now();
Instant newInstant = Instant.ofEpochMilli(System.currentTimeMillis());

可以使用 getEpochSecond() 获取1970年1月1日0时0分0秒至当前时间点的秒数,使用 getNano() 获取当前时间点的纳秒数。

long seconds = now.getEpochSecond();
int nano = now.getNano();

同时还可以使用 plus()、minus() 方法对Instant对象进行加减运算。

## 总结

本篇文章介绍了Java中常用的时间和日期处理类及方法,包括LocalDate、LocalTime、LocalDateTime、ZonedDateTime、Duration、Period、SimpleDateFormat、Instant等。

这些类和方法提供了丰富的时间和日期处理功能,可以方便地实现日期和时间的格式化、计算、比较、转换等操作,是Java程序员必备的时间和日期处理工具。