日期转换 String,Date,Long

我们经常的一个需求就是把String类型的字符串转化成日期或者时间,同时,我们也需要把日期和时间转变成String字符串用于显示

String->Date

public static final String DATE_FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";  DateTimeFormatter dateTimeFormatter =

DateTimeFormatter.ofPattern(format);
(1) LocalDateTime localDateTime = LocalDateTime.parse(dateTimeStr, dateTimeFormatter);
(2)LocalDate localDate = LocalDate.parse(dateStr, dateTimeFormatter);

Date -> Long

Date.getTime()

Long -> Date

Date.from(Instant.ofEpochMilli(time))

LocalDate,LocalTime和LocalDateTime

LocalDate表示在ISO格式(YYYY-MM-DD)下的不带具体时间的日期。
localTime 在本地时间表示不带日期的时间。
LocalDateTime用于表示日期和时间的组合。

// current time LocalDateTime localDateTime = LocalDateTime.now(); //
Time format DateTimeFormatter dateTimeFormatter =
DateTimeFormatter.ofPattern(“yyyy-MM-dd hh:mm:ss”);
System.out.println(localDateTime.format(dateTimeFormatter));

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值