一、LocalDate
1、获取现在时间
代码:
LocalDate now = LocalDate.now();
System.out.println("现在时间为:"+now);
输出结果:
现在时间为:2021-06-15
2、LocalDate转String
代码:
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate now = LocalDate.now();;
String dateStr = now.format(fmt);
System.out.println("现在时间字符串为:"+dateStr);
输出结果:
现在时间字符串为:2021-06-15
3、String转LocalDate
代码:
String str = "2021-06-15";
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.parse(str, fmt);
System.out.println("LocalDate类型输出结果:"+localDate);
输出结果:
LocalDate类型输出结果:2021-06-15
4、LocalDate转LocalDateTime
代码:
LocalDate localDate = LocalDate.now();
LocalDateTime localDateTime1 = localDate.atStartOfDay();
//23时1分2秒
LocalDateTime localDateTime2 = localDate.atTime(23,1,2);
//localTime :时、分、秒、毫秒 : 10:18:43.639
LocalTime localTime = LocalTime.now();
//拼接当前时间
LocalDateTime localDateTime3 = localDate.atTime(localTime);
System.out.println("atStartOfDay:日期后面拼接 00点00分 : "+localDateTime
System.out.println("atTime:参数分别代表 时、分、秒: "+localDateTime2);
System.out.println("atTime:拼接当前localTime : "+localDateTime3);
输出结果:
atStartOfDay:日期后面拼接 00点00分 : 2021-06-16T00:00
atTime:参数分别代表 时、分、秒: 2021-06-16T23:01:02
atTime:拼接当前localTime : 2021-06-16T10:24:21.085
5、LocalDate转date
代码:
LocalDate localDate = LocalDate.now();
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
Date date = Date.from(instant);
System.out.println("Date类型:"+date);
System.out.println("Date类型获取时间戳:"+date.getTime());
输出结果:
Date类型:Tue Jun 15 00:00:00 CST 2021
Date类型获取时间戳:1623686400000
6、date转LocalDate
代码:
Date date = new Date();
LocalDate localDate = Instant.ofEpochMilli(date.getTime())
.atZone(ZoneId.systemDefault()).toLocalDate();
System.out.println("localDate类型输出:"+localDate);
输出结果:
localDate类型输出:2021-06-15
7、LocalDate获取时间戳
代码:
//创建localDate:2021-06-16
LocalDate localDate = LocalDate.now();
//localDate 转 LocalDateTime :2021-06-16T00:00
LocalDateTime localDateTime = localDate.atStartOfDay();
//LocalDateTime epoch-second 值,它是从 1970-01-01T00:00:00Z 开始经过的秒数。
long l1 = localDateTime.toEpochSecond(ZoneOffset.of("+8"));
//toEpochMilli: 自 1970-01-01T00:00:00Z 纪元以来的毫秒数
long l2 = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
System.out.println("秒数打印 : "+l1);
System.out.println("毫秒 : "+l2);
输出结果:
秒数打印 : 1623772800
毫秒 : 1623772800000
二、LocalDateTime
1、获取现在时间
代码:
LocalDateTime now = LocalDateTime.now();
System.out.println("LocalDateTime 类型输出:"+now);
输出结果:
LocalDateTime 类型输出:2021-06-15T18:29:23.215
2、LocalDateTime转String
代码:
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String dateStr = localDateTime.format(fmt);
System.out.println("String类型输出:" + dateStr);
输出结果:
String类型输出:2021-06-15 18:34:16
3、String转LocalDateTime
代码:
String strLocalDate = "2021-06-15 18:00:00";
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String str = strLocalDate;
LocalDateTime localDateTime = LocalDateTime.parse(str, fmt);
System.out.println("LocalDateTime 类型输出:"+localDateTime);
输出结果:
LocalDateTime 类型输出:2021-06-15T18:00
4、LocalDateTime转LocalDate
代码:
LocalDateTime localDateTime = LocalDateTime.now();
LocalDate localDate = localDateTime.toLocalDate();
System.out.println("LocalDate 类型输出:" + localDate);
输出结果:
LocalDate 类型输出:2021-06-16
5、LocalDateTime转date
代码:
LocalDateTime now = LocalDateTime.now();
Date date = Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
System.out.println("Date类型输出:"+date);
输出结果:
Date类型输出:Tue Jun 15 18:32:07 CST 2021
6、date转LocalDateTime
代码:
Date date = new Date();
LocalDateTime localDateTime = Instant.ofEpochMilli(date.getTime())
.atZone(ZoneId.systemDefault()).toLocalDateTime();
System.out.println("Date类型输出:" + date);
输出结果:
Date类型输出:Tue Jun 15 18:39:05 CST 2021
7、LocalDateTime 获取时间戳
代码:
long l = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
//获取毫秒
long l1 = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
System.out.println("秒: "+l);
System.out.println("毫秒: "+l1);
输出结果:
秒: 1623810648
毫秒: 1623810648252