JAVA8 时间格式转换

2021-03-17 03:26

阅读:705

DateTimeFormatter formatter= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//对象转字符串
String timeStr = formatter.format(LocalDateTime.now());
System.out.println(timeStr);

//字符串转对象
LocalDateTime time = LocalDateTime.parse("2020-10-11 12:23:34",formatter);
System.out.println(time);

//对象转long(秒数)
Long seconds = time.toEpochSecond(ZoneOffset.of("+8"));
System.out.println(seconds);

//long转对象
LocalDateTime time2 = LocalDateTime.ofEpochSecond(1602390214,0,ZoneOffset.of("+8"));
System.out.println(time2);


评论


亲,登录后才可以留言!