Java 日期比較(上下午篇)
阿新 • • 發佈:2019-02-02
Java 日期比較 (上下午篇)
一、java.util.Date篇
Date date = new Date();
Date dateTime1 = new Date(date.getYear(),date.getMonth(),date.getDate(),12,0,0);
int i = dateTime1.compareTo(date);
System.out.println(i);
二、java.util.Calendar篇
(Calendar.getInstance().get(Calendar.AM_PM) == Calendar.getInstance().AM)?"上午":"下午"
三、最後,推薦下Java8的java.time.LocalDate(日期處理與日期的格式化方面更加優雅)
LocalDate localDate = LocalDate.now();
boolean b1 = Period.between(localDate, localDate.parse("2017-05-14")).getDays() == 0;
boolean b2 = "2017-05-14".equals(String.valueOf(localDate));
System.out.println(b1);
System.out.println(b2);