10个好用的Java日期和时间函数
在Java中,日期和时间的处理是很常见的,Java提供了许多内置的日期和时间函数来帮助我们处理时间戳、日期、时间等。本文将介绍Java中的10个好用的日期和时间函数。
1. LocalDate.now()
该函数会返回当前日期,不包括时间和时区。该函数返回一个LocalDate对象,可以使用该对象的方法获取日、月、年等值。
示例代码:
LocalDate currentDate = LocalDate.now();
2. LocalDateTime.now()
该函数返回当前日期和时间,不包括时区。该函数返回一个LocalDateTime对象,可以使用该对象的方法获取日、月、年、时、分、秒等值。
示例代码:
LocalDateTime currentDateTime = LocalDateTime.now();
3. DateTimeFormatter
该类用于格式化和解析日期时间。在Java中,有许多日期时间格式,例如:yyyy-MM-dd,HH:mm:ss等。可以使用该类将日期时间格式化为指定的字符串,也可以将字符串解析成日期时间。
示例代码:
// 格式化日期时间为字符串
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime currentDateTime = LocalDateTime.now();
String formattedDateTime = currentDateTime.format(formatter);
// 将字符串解析成日期时间
String dateTimeString = "2021-03-05 17:30:00";
LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
4. Period.between()
该函数用于计算两个日期之间的间隔,返回一个Period对象,可以使用该对象的方法获取间隔的天数、月数、年数等值。
示例代码:
LocalDate startDate = LocalDate.of(2021, 1, 1);
LocalDate endDate = LocalDate.of(2021, 3, 5);
Period period = Period.between(startDate, endDate);
int days = period.getDays(); // 4
int months = period.getMonths(); // 2
int years = period.getYears(); // 0
5. Duration.between()
该函数用于计算两个时间之间的间隔,返回一个Duration对象,可以使用该对象的方法获取间隔的小时数、分钟数、秒数等值。
示例代码:
LocalTime startTime = LocalTime.of(16, 0, 0);
LocalTime endTime = LocalTime.of(17, 30, 0);
Duration duration = Duration.between(startTime, endTime);
long hours = duration.toHours(); // 1
long minutes = duration.toMinutes(); // 90
long seconds = duration.getSeconds(); // 5400
6. Instant.now()
该函数返回当前时间戳,以UTC时区为基准。该函数返回一个Instant对象,可以使用该对象的方法获取毫秒数、秒数等值。
示例代码:
Instant currentTimestamp = Instant.now();
long milliseconds = currentTimestamp.toEpochMilli(); // 1614969386000
long seconds = currentTimestamp.getEpochSecond(); // 1614969386
7. ZoneId.systemDefault()
该函数返回当前时区,可以使用该对象的方法获取时区的偏移量、ID等值。
示例代码:
ZoneId zoneId = ZoneId.systemDefault();
ZoneOffset offset = zoneId.getRules().getOffset(Instant.now()); // +08:00
String zoneIdString = zoneId.getId(); // Asia/Shanghai
8. ZonedDateTime.now()
该函数返回当前日期和时间,包括时区。该函数返回一个ZonedDateTime对象,可以使用该对象的方法获取日、月、年、时、分、秒、毫秒等值,以及时区偏移量、ID等值。
示例代码:
ZonedDateTime currentDateTimeWithZone = ZonedDateTime.now();
int year = currentDateTimeWithZone.getYear(); // 2021
int month = currentDateTimeWithZone.getMonthValue(); // 3
int day = currentDateTimeWithZone.getDayOfMonth(); // 5
int hour = currentDateTimeWithZone.getHour(); // 17
int minute = currentDateTimeWithZone.getMinute(); // 30
int second = currentDateTimeWithZone.getSecond(); // 0
int nano = currentDateTimeWithZone.getNano(); // 0
ZoneOffset offset = currentDateTimeWithZone.getOffset(); // +08:00
String zoneIdString = currentDateTimeWithZone.getZone().getId(); // Asia/Shanghai
9. LocalDate.of()
该函数用于创建指定日期的LocalDate对象,该函数需要传入年、月、日等参数。
示例代码:
LocalDate date = LocalDate.of(2021, 3, 5);
10. LocalDateTime.of()
该函数用于创建指定日期和时间的LocalDateTime对象,该函数需要传入年、月、日、时、分、秒等参数。
示例代码:
LocalDateTime dateTime = LocalDateTime.of(2021, 3, 5, 17, 30, 0);
以上就是Java中的10个好用的日期和时间函数,需要注意的是,日期时间的处理在不同的场景中可能会有不同的要求,因此需要根据实际情况灵活使用。
