使用JKD8提供的时间工具 获取当年的开始时间 和去年开始时间
LocalDateTime thisYear = LocalDateTime.of(LocalDate.now().getYear(), 1, 1, 0, 0, 0);
LocalDateTime lastYearStr = LocalDateTime.of(LocalDate.now().minusYears(1).getYear(), 1, 1, 0, 0, 0);
运行结果:
将时间戳转换为日期格式: 第一个是String类型的时间戳 第二个是Long类型
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(Long.parseLong("1647310688")), ZoneId.systemDefault());
LocalDateTime localDate = LocalDateTime.ofInstant(Instant.ofEpochSecond(1647310688L), ZoneId.systemDefault());
运行结果:
public static void main(String[] args) {
//今年
LocalDateTime thisYear = LocalDateTime.of(LocalDate.now().getYear(), 1, 1, 0, 0, 0);
System.out.println("thisYear = " + thisYear);
//去年
LocalDateTime lastYearStr = LocalDateTime.of(LocalDate.now().minusYears(1).getYear(), 1, 1, 0, 0, 0);
System.out.println("lastYearStr = " + lastYearStr);
LocalDateTime localDateTime1 = LocalDateTime.ofInstant(Instant.ofEpochSecond(1647307415L), ZoneId.systemDefault());
System.out.println("localDateTime1 = " + localDateTime1);
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(Long.parseLong("1647310688")), ZoneId.systemDefault());
System.out.println(localDateTime);
LocalDateTime localDate = LocalDateTime.ofInstant(Instant.ofEpochSecond(1647310688L), ZoneId.systemDefault());
System.out.println(localDate);
Long timestamp = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
System.out.println(timestamp);
}
|