package text2;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
public class DateUtil {
public static void main(String[] args) {
LocalDate d = LocalDate.now(); // 当前日期
LocalTime t = LocalTime.now(); // 当前时间
LocalDateTime dt = LocalDateTime.now(); // 当前日期和时间
LocalDate localDate = dt.toLocalDate();
LocalTime localTime = dt.toLocalTime();
System.out.println(localDate);
System.out.println(localTime);
System.out.println(d); // 严格按照ISO 8601格式打印
System.out.println(t); // 严格按照ISO 8601格式打印
System.out.println(dt); // 严格按照ISO 8601格式打印
// 指定日期和时间:
LocalDate d2 = LocalDate.of(2019, 11, 30); // 2019-11-30, 注意11=11月
LocalTime t2 = LocalTime.of(15, 16, 17); // 15:16:17
LocalDateTime dt2 = LocalDateTime.of(2019, 11, 30, 15, 16, 17);
LocalDateTime dt3 = LocalDateTime.of(d2, t2);
System.out.println(d2);
System.out.println(t2);
System.out.println(dt2);
System.out.println(dt3);
LocalDateTime dt4 = LocalDateTime.parse("2019-11-19T15:16:17");
LocalDate d5 = LocalDate.parse("2019-11-19");
LocalTime t6 = LocalTime.parse("15:16:17");
System.out.println(dt4);
System.out.println(d5);
System.out.println(t6);
/* 日期:yyyy-MM-dd
时间:HH:mm:ss
带毫秒的时间:HH:mm:ss.SSS
日期和时间:yyyy-MM-dd'T'HH:mm:ss
带毫秒的日期和时间:yyyy-MM-dd'T'HH:mm:ss.SSS
*/
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
System.out.println(dtf.format(LocalDateTime.now()));
// 用自定义格式解析:
LocalDateTime dt7 = LocalDateTime.parse("2019/11/30 15:16:17", dtf);
System.out.println(dt7);
LocalDateTime ldt =LocalDateTime.of(2019,12,31,20,30,59);
System.out.println(ldt);
//加五天减三小时
LocalDateTime ldt2= ldt.plusDays(5).minusHours(3);
System.out.println(ldt2);
//减一个月
LocalDateTime ldt3= ldt.minusMonths(1).plusMonths(5);
System.out.println(ldt3);
LocalDateTime ldt4 = LocalDateTime.of(2019, 10, 26, 20, 30, 59);
System.out.println(ldt4);
// 日期变为31日:
LocalDateTime ldt5 = ldt4.withDayOfMonth(31);
System.out.println(ldt5); // 2019-10-31T20:30:59
// 月份变为9:
LocalDateTime ldt6 = ldt5.withMonth(9);
System.out.println(ldt6); // 2019-09-30T20:30:59
// 本月第一天0:00时刻:
LocalDateTime firstDay = LocalDate.now().withDayOfMonth(1).atStartOfDay();
System.out.println(firstDay);
// 本月最后1天:
LocalDate lastDay = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
System.out.println(lastDay);
// 下月第1天:
LocalDate nextMonthFirstDay = LocalDate.now().with(TemporalAdjusters.firstDayOfNextMonth());
System.out.println(nextMonthFirstDay);
// 本月第1个周一:
LocalDate firstWeekday = LocalDate.now().with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
System.out.println(firstWeekday);
System.out.println("==================================================");
// 要判断两个LocalDateTime的先后,可以使用isBefore()、isAfter()方法,对于LocalDate和LocalTime类似:
LocalDateTime now = LocalDateTime.now();
LocalDateTime target = LocalDateTime.of(2020, 11, 19, 8, 15, 0);
System.out.println(now.isBefore(target));
System.out.println(LocalDate.now().isBefore(LocalDate.of(2020, 11, 19)));
System.out.println(LocalTime.now().isAfter(LocalTime.parse("08:15:00")));
LocalDateTime start = LocalDateTime.of(2019, 11, 19, 8, 15, 0);
LocalDateTime end = LocalDateTime.of(2020, 1, 9, 19, 25, 30);
Duration d1 = Duration.between(start, end);
System.out.println(d1); // PT1235H10M30S
Period p = LocalDate.of(2019, 11, 19).until(LocalDate.of(2020, 1, 9));
System.out.println(p); // P1M21D
Duration d3 = Duration.ofHours(10); // 10 hours
Duration d4 = Duration.parse("P1DT2H3M"); // 1 day, 2 hours, 3 minutes
System.out.println(d3);
System.out.println(d4);
}
}
package text2;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ZonedDateUtil {
public static void main(String[] args) {
ZonedDateTime zbj = ZonedDateTime.now(); // 默认时区
ZonedDateTime zny = ZonedDateTime.now(ZoneId.of("America/New_York")); // 用指定时区获取当前时间
System.out.println(zbj);
System.out.println(zny);
LocalDateTime ldt = LocalDateTime.of(2019, 9, 15, 15, 16, 17);
ZonedDateTime zbj1 = ldt.atZone(ZoneId.systemDefault());
ZonedDateTime zny1 = ldt.atZone(ZoneId.of("America/New_York"));
System.out.println(zbj1);
System.out.println(zny1);
// 以中国时区获取当前时间:
ZonedDateTime zbj2 = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
// 转换为纽约时间:
ZonedDateTime zny2 = zbj2.withZoneSameInstant(ZoneId.of("America/New_York"));
System.out.println(zbj2);
System.out.println(zny2);
LocalDateTime ldt3 = zbj2.toLocalDateTime();
System.out.println(ldt3);
}
}