Date-Time API
提供了四个类,专门处理日期信息,不涉及时间或时区。
LocalDate
YearMonth
MonthDay
Year
LocalDate
LocalDate
代表ISO
日历中的**年-月-日,对于代表没有时间的日期很有用。你可以用LocalDate
来跟踪一个重要的事件,如出生日期或结婚日期。
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
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
MonthDay
MonthDay
类代表某个月的某一天,例如1月1日的元旦。
下面的例子使用MonthDay.isValidYear
方法来确定2月29日是否对2010年有效。该调用返回false,确认2010年不是一个闰年。
MonthDay date = MonthDay.of(Month.FEBRUARY, 29);
boolean validLeapYear = date.isValidYear(2010);
Year
Year
类代表一个年份。下面的例子使用Year.isLeap
方法来确定给定年份是否是闰年。该调用返回true,确认2012年是闰年。
boolean validLeapYear = Year.of(2012).isLeap();
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.javaguides.net/2018/07/java-8-date-classes-with-examples.html
内容来源于网络,如有侵权,请联系作者删除!