获取今日零点时间和23.59.59末点时间
LocalDateTime startTime=LocalDateTime.of(LocalDate.now(), LocalTime.MIN); //开始时间
LocalDateTime endTime=LocalDateTime.of(LocalDate.now(), LocalTime.MAX); //结束时间
long zero = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();//时间戳
坑:方法年月日计算是分开计算的
//例如: localDate:2021-08-01 localDate1:2022-10-02
Period period = Period.between(localDate, localDate1);
System.out.println(period.getYears()); // 1 2022-2021
System.out.println(period.getMonths()); // 2 10-08
System.out.println(period.getDays()); // 1 02-01
// 可以使用 localDate 方法,直接计算
localDate.toEpochDay() - localDate1.toEpochDay() //来计算天数
// 时间戳LocalData
final long currentTimeMillis = System.currentTimeMillis(); //当前时间戳
//当前时间戳 转LocalDate
LocalDate localDate = Instant.ofEpochMilli(currentTimeMillis).atZone(ZoneOffset.ofHours(8)).toLocalDate();
final long endTime= 1635570545000L; // 2021-10-30 13:09:05
// endTime 转LocalDate
LocalDate localDate1 = Instant.ofEpochMilli(endTime).atZone(ZoneOffset.ofHours(8)).toLocalDate();
// 比较当前的LocalDate 与 // 2021-10-30 13:09:05 时间戳 之间数据
Period period = Period.between(localDate, localDate1);
// 判断 月份差
System.out.println(period.getMonths()); // 3
// 判断 日差 年差 等等
其他操作:
时间戳与LocalDate互转
1 .时间戳转LocalDate
@Test
public void localDateTest1(){
//获得时间戳
long milliseconds = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
// 将时间戳转为当前时间
LocalDate localDate = Instant.ofEpochMilli(milliseconds).atZone(ZoneOffset.ofHours(8)).toLocalDate();
// 2020-02-03
System.out.println(localDate);
}
@Test
public void localDateTest2(){
//获得时间戳
long seconds = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).getEpochSecond();
// 将时间戳转为当前时间
LocalDate localDate = Instant.ofEpochSecond(seconds).atZone(ZoneOffset.ofHours(8)).toLocalDate();
// 2020-02-03
System.out.println(localDate);
}
LocalDate 转 时间戳
@Test
public void localDateTest3(){
LocalDate localDate = LocalDate.now();
//获得时间戳
long seconds = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().getEpochSecond();
// 1580659200
System.out.println(seconds);
}
@Test
public void localDateTest4(){
LocalDate localDate = LocalDate.now();
//获得时间戳
long seconds = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
// 1580659200000
System.out.println(seconds);
}
LocalDateTime 转 时间戳
毫秒级别
@Test
public void localTimeTest3(){
// 获得当前时间
LocalDateTime localDateTime = LocalDateTime.now();
// 将当前时间转为时间戳
long milliseconds = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
System.out.println(milliseconds);// 1627626298150
}
秒级别
@Test
public void localTimeTest1(){
// 获得当前时间
LocalDateTime localDateTime = LocalDateTime.now();
// 将当前时间转为时间戳
long second = localDateTime.toEpochSecond(ZoneOffset.ofHours(8));
System.out.println(second); // 1580706475
}
秒级别
@Test
public void localTimeTest2(){
// 获得当前时间
LocalDateTime localDateTime = LocalDateTime.now();
// 将当前时间转为时间戳
long second = localDateTime.toInstant(ZoneOffset.ofHours(8)).getEpochSecond();
System.out.println(second);// 1580707001
}
时间戳 转LocalDateTime
秒级别
@Test
public void localTimeTest4(){
//获得时间戳
long second = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).getEpochSecond();
// 将时间戳转为当前时间
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(second, 0, ZoneOffset.ofHours(8));
// 2020-02-03T13:30:44
System.out.println(localDateTime);
}
// 方式二 本次获取的时间搓将是毫秒级别故要除以1000
public void localTimeTest5(){
//获得时间戳
long milliseconds = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
// 将时间戳转为当前时间
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(milliseconds/1000, 0, ZoneOffset.ofHours(8));
// 2020-02-03T13:35:53
System.out.println(localDateTime);
}
// 方式三本方式精确值是毫秒级别,故得到的结果会存在三位小数点;
@Test
public void localTimeTest6(){
//获得时间戳
long milliseconds = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
// 将时间戳转为当前时间
LocalDateTime localDateTime = Instant.ofEpochMilli(milliseconds).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
// 2020-02-03T13:38:35.799
System.out.println(localDateTime);
}
LocalDateTime与Date互转
Date转LocalDateTime
方式一得出结果是有小数点,毫秒级精确
@Test
public void DateTest1(){
// 创建时间
Date date = new Date();
// 将时间转为 LocalDateTime
LocalDateTime localDateTime = date.toInstant().atOffset(ZoneOffset.ofHours(8)).toLocalDateTime();
// 2020-02-03T14:07:49.833
System.out.println(localDateTime);
}
@Test
public void DateTest2(){
// 创建时间
Date date = new Date();
// 将时间转为 秒级时间戳
long second = date.toInstant().atOffset(ZoneOffset.ofHours(8)).toEpochSecond();
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(second, 0, ZoneOffset.ofHours(8));
// 2020-02-03T14:11:39
System.out.println(localDateTime);
}
LocalDateTime 转 Date
@Test
public void DateTest3(){
//当前时间
LocalDateTime localDateTime = LocalDateTime.now();
// 获得 Instant
Instant instant = Instant.ofEpochSecond(localDateTime.toEpochSecond(ZoneOffset.ofHours(8)));
// 获得 Date
Date date = Date.from(instant);
// Mon Feb 03 14:16:27 CST 2020
System.out.println(date);
}
@Test
public void DateTest4(){
//当前时间
LocalDateTime localDateTime = LocalDateTime.now();
// 获得 Instant
Instant instant = localDateTime.atZone(ZoneOffset.ofHours(8)).toInstant();
// 获得 Date
Date date = Date.from(instant);
// Mon Feb 03 14:20:32 CST 2020
System.out.println(date);
}
LocalDate与Date互转
LocalDate 转 Date
@Test
public void DateTest5(){
//当前时间
LocalDate localDate = LocalDate.now();
// 获得 Instant
Instant instant = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant();
// 获得 Date
Date date = Date.from(instant);
// Mon Feb 03 00:00:00 CST 2020
System.out.println(date);
}
@Test
public void DateTest6(){
// 获得 date
Date date = new Date();
// 获得 LocalDate
LocalDate localDate = date.toInstant().atOffset(ZoneOffset.ofHours(8)).toLocalDate();
// 2020-02-03
System.out.println(localDate);
}
LocalDateTime格式化
LocalDateTime 转字符串
@Test
public void format1(){
// 获得 localDateTime
LocalDateTime localDateTime = LocalDateTime.now();
// 指定模式
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH/mm/ss");
// 将 LocalDateTime 格式化为字符串
String format = localDateTime.format(dateTimeFormatter);
// 2020/02/03 14/38/54
System.out.println(format);
}
字符串 转LocalDateTime
@Test
public void format2(){
String time = "2020/02/03 14/38/54";
// 指定模式
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH/mm/ss");
// 将字符串格式化为 LocalDateTime
LocalDateTime localDateTime = LocalDateTime.parse(time, dateTimeFormatter);
// 2020-02-03T14:38:54
System.out.println(localDateTime);
}