LocaLDate 时间戳相互转换笔记
/**
* 将Long类型的时间戳转换成String 类型的时间格式,时间格式为:yyyy-MM-dd HH:mm:ss
*/
public static String convertTimeToString(Long time){
Assert.notNull(time, "time is null");
DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time),ZoneId.systemDefault()));
}
时间戳与localdate、localdatetime转换
long timestamp = System.currentTimeMillis();
LocalDate localDate = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(0)).toLocalDate();
System.out.println(localDate);
LocalDateTime localDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(0)).toLocalDateTime();
System.out.println(localDateTime);
日期格式处理(2017-01-05T09:02:11+08:00)
String publishDate = "2017-01-05T09:02:11+08:00";
// 【joda-time】支持解析这种格式的字符串,通过toDate方法就能获取java中的Date,如下:
new DateTime(publishDate).toDate();