两种构造
LocalDateTime time = LocalDateTime.now();
System.out.println(time);
LocalDateTime of = LocalDateTime.of(2015, 10, 22, 10, 10, 10);
System.out.println(of);
时间的加减
LocalDateTime plusTime = time.plusYears(2);
System.out.println(plusTime);
LocalDateTime minusTime = time.minusYears(2);
System.out.println(minusTime);
获取年月日时分秒
System.out.println(time.getYear());
System.out.println(time.getMonthValue());
System.out.println(time.getDayOfMonth());
System.out.println(time.getHour());
System.out.println(time.getMinute());
System.out.println(time.getSecond());
时间戳
时间戳(从unix元年开始算: 1970年1月1日到某个时间之间的毫秒差)
默认获取UTC时区的时间和我们的时间差8个小时,但是这里获取的时间还不是时间戳
Instant instant = Instant.now();
System.out.println(instant); //2019-11-03T12:19:03.272Z
时区纠正
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime); //2019-11-03T20:19:03.272+08:00
转为毫秒
//转为毫秒
long milli = instant.toEpochMilli();
System.out.println(milli); //1572783543272
当前时间转为毫秒
Long newSecond1 = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
相对于元年做一个时间运算 意义不明
//相较于元年做一个运算
Instant instant1 = Instant.ofEpochSecond(60);
System.out.println(instant1);
计算两个时间之间的间隔
@Test
public void test3() throws InterruptedException {
Instant instant = Instant.now();
Thread.sleep(1000);
Instant instant1 = Instant.now();
Duration duration = Duration.between(instant, instant1);
System.out.println("Instant的时间计算结果" + duration.toMillis());
LocalDateTime localDateTime1 = LocalDateTime.now();
Thread.sleep(1000);
LocalDateTime localDateTime2 = LocalDateTime.now();
System.out.println("LocalDateTime时间的计算结果" + Duration.between(localDateTime1,localDateTime2).toMillis());
}
计算两个日期之间的间隔
//计算两个日期之间的间隔
@Test
public void test4() {
LocalDate date1 = LocalDate.of(2016,12,12);
LocalDate date2 = LocalDate.now();
Period period = Period.between(date1, date2);
System.out.println("差的年份是" + period.getYears());
System.out.println("差的月份是" + period.getMonths());
System.out.println("差的天数是" + period.getDays());
}
时间矫正器
比较简单的类型
@Test
public void test5() {
LocalDateTime time = LocalDateTime.now();
System.out.println(time);
LocalDateTime time2 = time.withDayOfMonth(10);
System.out.println(time2);
/**
* 下一年的第一天
*/
LocalDateTime time3 = time.with(TemporalAdjusters.firstDayOfNextYear());
System.out.println(time3);
/**
* 下一个周一
*/
LocalDateTime netMonday = time.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
System.out.println(netMonday);
}
自定义的
下一个工作日
public static LocalDate nextWorkDay(LocalDate date) {
return date.with(t->{
LocalDate t1 = (LocalDate)t;
DayOfWeek dayOfWeek = t1.getDayOfWeek();
if(dayOfWeek.equals(DayOfWeek.FRIDAY)) {
return t1.plusDays(3);
}else if(dayOfWeek.equals(DayOfWeek.SATURDAY)){
return t1.plusDays(2);
}else{
return t1.plusDays(1);
}
});
}
下一个结婚纪念日
public static LocalDate nextMarryDay(LocalDate date, int marryMon, int marryDay){
return date.with(t->{
LocalDate t1 = (LocalDate)t;
if (t1.getMonthValue()> marryMon) {
return LocalDate.of(t1.getYear() + 1, marryMon, marryDay);
}else if(t1.getMonthValue() == marryMon) {
if(t1.getDayOfMonth() > marryDay) {
return LocalDate.of(t1.getYear() + 1, marryMon, marryDay);
}else {
return LocalDate.of(t1.getYear(), marryMon, marryDay);
}
}else{
return LocalDate.of(t1.getYear(), marryMon, marryDay);
}
});
}
都是可以用的
@Test
public void test6(){
System.out.println(TestLocalDateTime.nextWorkDay(LocalDate.now()));
System.out.println(TestLocalDateTime.nextMarryDay(LocalDate.now(), 10, 10));
}
格式化
使用自带的格式化
DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;
LocalDateTime ldt = LocalDateTime.now();
String strDate = ldt.format(dtf); //2019-11-03
自定义格式化
DateTimeFormatter dtf2 =
DateTimeFormatter.ofPattern("YYYY年MM月dd日 HH:mm:ss");
String strDate2 = dtf2.format(ldt);
System.out.println(strDate2);
将字符串转换回来 (报错)
DateTimeFormatter dtf2 =
DateTimeFormatter.ofPattern("YYYY年MM月dd日 HH:mm:ss");
LocalDateTime newDate = ldt.parse("2019年11月03日 23:57:21", dtf2);
System.out.println(newDate);
时区的使用
获取所有时区
@Test
public void test8(){
Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
availableZoneIds.forEach(System.out::println);
}
获取当前日本东京的时间
@Test
public void test9(){
LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Asia/Tokyo"));
System.out.println(ldt);
}