Java格式化日期 微秒


本文主要讲述Java日期格式化及格式化日期到微秒

Date、LocalDateTime格式化微秒值

java代码TestTime.java如下

package com.dongao.test;

import com.dongao.project.common.util.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class TestTime {

    public static void main(String[] args) {
        String str = "2022-07-04 23:57:29.696";
        Date date = stringToDate(str, "yyyy-MM-dd HH:mm:ss.SSS");
        System.out.println("date:["+date+"]");
        LocalDateTime datetime = stringToDateTime(str, "yyyy-MM-dd HH:mm:ss.SSS");
        System.out.println("datetime:["+datetime+"]");
    }

    public static Date stringToDate(String inStr, String dateFormat) {
        try {
            return getDateFormat(dateFormat).parse(inStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static LocalDateTime stringToDateTime(String inStr, String dateFormat) {
        try {
            LocalDateTime localDateTime = LocalDateTime.parse(inStr, getDateTimeFormat(dateFormat));
            return localDateTime;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static SimpleDateFormat getDateFormat(String dateFormat) {
        return new SimpleDateFormat(dateFormat);
    }

    public static DateTimeFormatter getDateTimeFormat(String dateFormat) {
        return DateTimeFormatter.ofPattern(dateFormat);
    }
}

格式化结果执行

通过执行结果可以看到用SimpleDateFormat对含有微秒值的时间格式在字符串转Date时除了会出现精度丢失的情况,部分时间还会出现转换错误的情况,而用DateTimeFormatter对含有微妙值的时间格式字符串转LocalDateTime则一切正常。
但是一般业务不会用到时间格式的毫秒或者说微秒值,如果真的用到的话建议用LocalDateTime存储,Mysql需要用datetime(6)这样就可以保存微秒值的时间,如图

Date、LocalDateTime互转

在不考虑微秒或者毫秒时间精度丢失的情况下,Date、LocalDateTime可以相互转,main函数增加代码

        Date toDate = toDate(datetime);
        System.out.println("toDate:["+toDate+"]");
        LocalDateTime toLocalDateTime = toLocalDateTime(date);
        System.out.println("toLocalDateTime:["+toLocalDateTime+"]");
        LocalDate toLocalDate = toLocalDate(date);
        System.out.println("toLocalDate:["+toLocalDate+"]");

整个类增加方法

    public static Date toDate(LocalDateTime localDateTime) {
        Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        return Date.from(instant);
    }

    public static LocalDateTime toLocalDateTime(Date date) {
        Instant instant = date.toInstant();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        return localDateTime;
    }

    public static LocalDate toLocalDate(Date date) {
        Instant instant = date.toInstant();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        LocalDate localDate = localDateTime.toLocalDate();
        return localDate;
    }

转换之后结果如图

参考文章:https://developer.aliyun.com/article/982671

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

csdn565973850

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值