Java 8日期时间LocalDate、YearMonth、MonthDay、Year使用教程

x33g5p2x  于2021-08-22 转载在 Java  
字(3.2k)|赞(0)|评价(0)|浏览(938)

Java 8日期-时间教程

Date-Time API提供了四个类,专门处理日期信息,不涉及时间或时区。

  1. LocalDate
  2. YearMonth
  3. MonthDay
  4. Year
    让我们用实例来讨论每一类。本指南的例子可在Github上找到。

1. LocalDate

LocalDate代表ISO日历中的**年-月-日,对于代表没有时间的日期很有用。你可以用LocalDate来跟踪一个重要的事件,如出生日期或结婚日期。

1.2 LocalDate类示例

例子1:通过传递年、月、日的方法来获取本地日期。

LocalDate getLocalDateUsingFactoryOfMethod(int year, int month, int dayOfMonth) {
 return LocalDate.of(year, month, dayOfMonth);
}

例2:用这个方法获得当前日期或今天的日期。

LocalDate getLocalDateFromClock() {
 LocalDate localDate = LocalDate.now();
 return localDate;
}

例3:用此方法获得今天的下一天或特定的日期。

LocalDate getNextDay(LocalDate localDate) {
 return localDate.plusDays(1);
}

例4:通过传递特定的日期给这个方法获得前一天。

LocalDate getPreviousDay(LocalDate localDate) {
 return localDate.minus(1, ChronoUnit.DAYS);
}

例5:用此方法获得一周的日期。

DayOfWeek getDayOfWeek(LocalDate localDate) {
 DayOfWeek day = localDate.getDayOfWeek();
 return day;
}

例6: 获得每月的第一天。

LocalDate getFirstDayOfMonth() {
 LocalDate firstDayOfMonth = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
 return firstDayOfMonth;
}

例7:获取一天的开始时间。

LocalDateTime getStartOfDay(LocalDate localDate) {
 LocalDateTime startofDay = localDate.atStartOfDay();
 return startofDay;
}

例8:检查重复发生的事件,如Java 8中的生日。

public static void recurringDate(LocalDate today) {
 LocalDate dateOfBirth = LocalDate.of(2010, 01, 14);
 MonthDay birthday = MonthDay.of(dateOfBirth.getMonth(), dateOfBirth.getDayOfMonth());
 MonthDay currentMonthDay = MonthDay.from(today);
 if (currentMonthDay.equals(birthday)) {
  System.out.println("Many Many happy returns of the day !!");
 } else {
  System.out.println("Sorry, today is not your birthday");
 }
}

让我们看看几个如何格式化日期的例子。

private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");
private static final DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("d-MMM-yyyy");
private static final DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("d/MM/yyyy");
public static void main(String[] args) {
 //default format
 System.out.println("Default format of LocalDate = " + LocalDate.now());
   
 // The ISO date formatter that formats or parses a date without an
  // offset, such as '20111203'
 LocalDate date = LocalDate.now();
 
 System.out.println(date.format(DateTimeFormatter.BASIC_ISO_DATE));
 
 System.out.println(date.format(DateTimeFormatter.ISO_DATE));
 
 System.out.println(formatter.format(LocalDate.parse("16/08/2016", formatter)));
 
 System.out.println(formatter1.format(LocalDate.parse("16-Aug-2016", formatter1)));
 
 System.out.println(formatter2.format(LocalDate.parse("16/08/2016", formatter2)));
}

输出。

Default format of LocalDate = 2018-07-11
20180711
2018-07-11
16/08/2016
16-Aug-2016
16/08/2016

2. YearMonth

YearMonth类代表特定年份的月份。下面的例子使用YearMonth.lengthOfMonth()方法来确定几个年份和月份组合的天数。

YearMonth date = YearMonth.now();
System.out.printf("%s: %d%n", date, date.lengthOfMonth());

YearMonth date2 = YearMonth.of(2010, Month.FEBRUARY);
System.out.printf("%s: %d%n", date2, date2.lengthOfMonth());

YearMonth date3 = YearMonth.of(2012, Month.FEBRUARY);
System.out.printf("%s: %d%n", date3, date3.lengthOfMonth());

这段代码的输出看起来如下。

2013-06: 30
2010-02: 28
2012-02: 29

3. MonthDay

MonthDay类代表某个月的某一天,例如1月1日的元旦。

下面的例子使用MonthDay.isValidYear方法来确定2月29日是否对2010年有效。该调用返回false,确认2010年不是一个闰年。

MonthDay date = MonthDay.of(Month.FEBRUARY, 29);
boolean validLeapYear = date.isValidYear(2010);

4. Year

Year类代表一个年份。下面的例子使用Year.isLeap方法来确定给定年份是否是闰年。该调用返回true,确认2012年是闰年。

boolean validLeapYear = Year.of(2012).isLeap();

相关文章

微信公众号

最新文章

更多