Java8时间转换
2024-10-21 10:33:35
===java8中时间的各种转换(LocalDateTime)===
1.将LocalDateTime转为自定义的时间格式的字符串
public static String getDateTimeAsString(LocalDateTime localDateTime, String format) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
return localDateTime.format(formatter);
}
2.将long类型的timestamp转为LocalDateTime
public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
Instant instant = Instant.ofEpochMilli(timestamp);
ZoneId zone = ZoneId.systemDefault();
return LocalDateTime.ofInstant(instant, zone);
}
3.将LocalDateTime转为long类型的timestamp
public static long getTimestampOfDateTime(LocalDateTime localDateTime) {
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
return instant.toEpochMilli();
}
4.将某时间字符串转为自定义时间格式的LocalDateTime
public static LocalDateTime parseStringToDateTime(String time, String format) {
DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
return LocalDateTime.parse(time, df);
}
链接:https://blog.csdn.net/wsywb111/article/details/79815481
最新文章
- Search for a Range
- __thread关键字[转]
- unity3d 特殊文件夹和脚本编译顺序
- poj蚂蚁问题
- mongoexport导出数据
- Android实时获取音量(单位:分贝)
- 安卓---apk反编译
- PAT乙级1004. 成绩排名 (20)
- highstock
- 开发快速定位需求(Coding之前的工作)
- shell脚本解压多个jar包
- windows端运行.sh脚本
- Kibana查询
- (转)Java动态追踪技术探究
- 招聘移动APP、接口、自动化、性能和安全方面的兼职测试讲师
- JAVA中接口与抽象类
- Thread.currentThread().getContextClassLoader().getResourceAsStream
- php把数据转换为json格式
- 使用terraform-provider-s3 操作minio
- R语言编程艺术(3)R语言编程基础