1. 在 Java 8 中获取今天的日期
LocalDate now = LocalDate.now();
System.out.println(now);
//结果
2018-06-20
2. 在 Java 8 中获取年、月、日信息
LocalDate now = LocalDate.now();
int year = now.getYear();
int monthValue = now.getMonthValue();
int dayOfMonth = now.getDayOfMonth();
System.out.printf("year = %d, month = %d, day = %d", year, monthValue, ayOfMonth);
//结果
year = 2018, month = 6, day = 20
3. 在 Java 8 中处理特定日期
LocalDate date = LocalDate.of(2018, 06, 20);
System.out.println(date);
4. 在 Java 8 中判断两个日期是否相等
LocalDate now = LocalDate.now();
LocalDate date = LocalDate.of(2018, 06, 20);
if (date.equals(now)) {
System.out.println("同一天");
}
5. 在 Java 8 中检查像生日这种周期性事件
LocalDate now = LocalDate.now();
LocalDate dateOfBirth = LocalDate.of(2018, 06, 20);
MonthDay birthday = MonthDay.of(dateOfBirth.getMonth(), dateOfBirth.getDayOfMonth());
MonthDay currentMonthDay = MonthDay.from(now);
if (currentMonthDay.equals(birthday)) {
System.out.println("Happy Birthday");
} else {
System.out.println("Sorry, today is not your birthday");
}
//结果
Happy Birthday
6. 在 Java 8 中获取当前时间
LocalTime localTime = LocalTime.now();
System.out.println(localTime);
//结果可以看到当前时间就只包含时间信息,没有日期。
13:35:56.155
7. 如何在现有的时间上增加小时
LocalTime localTime = LocalTime.now();
System.out.println(localTime);
LocalTime localTime1 = localTime.plusHours(2);//增加2小时
System.out.println(localTime1);
//结果
13:41:20.721
15:41:20.721
8. 如何计算一周后的日期
LocalDate now = LocalDate.now();
LocalDate plusDate = now.plus(1, ChronoUnit.WEEKS);
System.out.println(now);
System.out.println(plusDate);
//结果
2018-06-20
2018-06-27
9. 计算一年前或一年后的日期
LocalDate now = LocalDate.now();
LocalDate minusDate = now.minus(1, ChronoUnit.YEARS);
LocalDate plusDate1 = now.plus(1, ChronoUnit.YEARS);
System.out.println(minusDate);
System.out.println(plusDate1);
//结果
2017-06-20
2019-06-20
10. 使用 Java 8 的 Clock 时钟类
Clock clock = Clock.systemUTC();
Clock clock1 = Clock.systemDefaultZone();
System.out.println(clock);
System.out.println(clock1);
//结果
SystemClock[Z]
SystemClock[Asia/Shanghai]
11. 如何用 Java 判断日期是早于还是晚于另一个日期
LocalDate tomorrow = LocalDate.of(2018,6,20);
if(tomorrow.isAfter(now)){
System.out.println("Tomorrow comes after today");
}
LocalDate yesterday = now.minus(1, ChronoUnit.DAYS);
if(yesterday.isBefore(now)){
System.out.println("Yesterday is day before today");
}
12. 在 Java 8 中处理时区
ZoneId america = ZoneId.of("America/New_York");
LocalDateTime localtDateAndTime = LocalDateTime.now();
ZonedDateTime dateAndTimeInNewYork = ZonedDateTime.of(localtDateAndTime, america );
System.out.println(dateAndTimeInNewYork);
13. 如何表示信用卡到期这类固定日期,答案就在 YearMonth
YearMonth currentYearMonth = YearMonth.now();
System.out.printf("Days in month year %s: %d%n", currentYearMonth, currentYearMonth.lengthOfMonth());
YearMonth creditCardExpiry = YearMonth.of(2018, Month.FEBRUARY);
System.out.printf("Your credit card expires on %s %n", creditCardExpiry);
//结果
Days in month year 2018-06: 30
Your credit card expires on 2018-02
14. 如何在 Java 8 中检查闰年
15. 计算两个日期之间的天数和月数
LocalDate date = LocalDate.of(2019, Month.MARCH, 20);
Period period = Period.between(now, date);
System.out.println("离下个时间还有" + period.getMonths() + " 个月");
16. 包含时差信息的日期和时间
在 Java 8 中,ZoneOffset 类用来表示时区,举例来说印度与 GMT 或 UTC 标准时区相差 +05:30,可以通过ZoneOffset.of() 静态方法来获取对应的时区。一旦得到了时差就可以通过传入 LocalDateTime 和 ZoneOffset 来创建一个 OffSetDateTime 对象。
LocalDateTime datetime = LocalDateTime.of(2014, Month.JANUARY, 14,19,30);
ZoneOffset offset = ZoneOffset.of("+05:30");
OffsetDateTime date = OffsetDateTime.of(datetime, offset);
System.out.println("Date and Time with timezone offset in Java : " + date);
17. 在 Java 8 中获取当前的时间戳
Instant timestamp = Instant.now();
System.out.println(timestamp);
// 结果
2018-06-20T06:35:24.881Z
18. 在 Java 8 中如何使用预定义的格式化工具去解析或格式化日期
19. 如何在 Java 中使用自定义格式化工具解析日期
//DateTimeFormatter 如何使用:
//解析日期
String dateStr= "2018年06月20日";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
LocalDate date= LocalDate.parse(dateStr, formatter);
//日期转换为字符串
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy年MM月dd日 hh:mm a");
String nowStr = now .format(format);
20. 在 Java 8 中如何把日期转换成字符串
LocalDateTime arrivalDate = LocalDateTime.now();
try {
DateTimeFormatter format = DateTimeFormatter.ofPattern("MMMdd yyyy hh:mm a");
String landing = arrivalDate.format(format);
System.out.printf("Arriving at : %s %n", landing);
}catch (DateTimeException ex) {
System.out.printf("%s can't be formatted!%n", arrivalDate);
ex.printStackTrace();
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。