官方示例:官方示例扩展小应用
因为学习比较晚,jdk8出来几年了,一直很少使用它的新特性,前几年思维也比较固化,最近重新看了API,也重新学习一下。
不带时间的日期
LocalDate类为主, 这里主要获取日期、日期加减,日期获取周、日期格式化,如下:
public class LocalDate1 {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS);
LocalDate yesterday = tomorrow.minusDays(2);
//打印日期
System.out.println(today);
System.out.println(tomorrow);
System.out.println(yesterday);
// 3月的第4天
LocalDate independenceDay = LocalDate.of(2020, Month.MARCH, 4);
DayOfWeek dayOfWeek = independenceDay.getDayOfWeek();
System.out.println(dayOfWeek); // WEDNESDAY
//指定格式
DateTimeFormatter germanFormatter =
DateTimeFormatter
.ofLocalizedDate(FormatStyle.MEDIUM)//格式日期长短说明
.withLocale(Locale.GERMAN);//国家
DateTimeFormatter germanFormatter2 =
DateTimeFormatter
.ofLocalizedDate(FormatStyle.FULL)
.withLocale(Locale.SIMPLIFIED_CHINESE);
LocalDate xmas = LocalDate.parse("30.03.2020", germanFormatter);
System.out.println(xmas); // 30.03.2020
System.out.println(germanFormatter2.format(today));//2020年4月1日 星期三
}
}
不带日期的时间
主要介绍LocalTime 类,包括时钟、时区、日期转换,比较时间先后
public class LocalTime1 {
public static void main(String[] args) {
// get the current time
Clock clock = Clock.systemDefaultZone();
long t0 = clock.millis();
System.out.println(t0);
//时钟转日期
Instant instant = clock.instant();
Date legacyDate = Date.from(instant);
System.out.println("时区:"+ZoneId.systemDefault());//Asia/Shanghai
ZoneId zone1 = ZoneId.of("Asia/Chongqing"); //东京
ZoneId zone2 = ZoneId.of("Asia/Tokyo");
System.out.println(zone1.getRules());
System.out.println(zone2.getRules());
// time
LocalTime now1 = LocalTime.now(zone1);
LocalTime now2 = LocalTime.now(zone2);
System.out.println(now1);
System.out.println(now2);
System.out.println(now1.isBefore(now2)); // false
//时差
long hoursBetween = ChronoUnit.HOURS.between(now1, now2);
long minutesBetween = ChronoUnit.MINUTES.between(now1, now2);
System.out.println(hoursBetween);
System.out.println(minutesBetween);
// create time(时分秒、毫秒处理)
LocalTime now = LocalTime.now();
System.out.println(now);
LocalTime late = LocalTime.of(23, 59, 59);
System.out.println(late);
//无毫秒
LocalTime now3 = now2.withNano(0); // 11:09:09
System.out.println(now3);
}
}
时间与日期
LocalDateTime:时间、日期获取、格式、设置处理类
public class LocalDateTime1 {
public static void main(String[] args) {
//自己指定年月日,时分秒
LocalDateTime sylvester = LocalDateTime.of(2014, Month.DECEMBER, 31, 23, 59, 59);
DayOfWeek dayOfWeek = sylvester.getDayOfWeek();
System.out.println(dayOfWeek); // WEDNESDAY
Month month = sylvester.getMonth();
System.out.println(month); // DECEMBER
long minuteOfDay = sylvester.getLong(ChronoField.MINUTE_OF_DAY);
System.out.println(minuteOfDay); // 1439
//时间戳
Instant instant = sylvester
.atZone(ZoneId.systemDefault())
.toInstant();
Date legacyDate = Date.from(instant);
System.out.println(legacyDate); // Wed Dec 31 23:59:59 CET 2014
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime parsed = LocalDateTime.parse("2020-04-01 09:10:40", formatter);
String string = parsed.format(formatter);
System.out.println(string); // 2020-04-01
//格式date对象
LocalDateTime localDateTime = LocalDateTime.ofInstant(legacyDate.toInstant(), ZoneId.systemDefault());
System.out.println(formatter.format(localDateTime));
//比较两个日期
LocalDateTime time2 = LocalDateTime.now().minusMonths(1);
DateTimeFormatter fmlDate2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime time1 = LocalDateTime.parse("2020-02-31 19:43:53", fmlDate2);
System.out.println("结果:"+time1.isBefore(time2));
}
}