首页 > 编程语言 >Java格式化日期 微秒

Java格式化日期 微秒

时间:2022-12-09 14:01:17浏览次数:39  
标签:格式化 java import return LocalDateTime Date Java 微秒 public


Java格式化日期 微秒

  • ​​Date、LocalDateTime格式化微秒值​​
  • ​​Date、LocalDateTime互转​​


本文主要讲述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);
}
}

格式化结果执行

Java格式化日期 微秒_System


通过执行结果可以看到用SimpleDateFormat对含有微秒值的时间格式在字符串转Date时除了会出现精度丢失的情况,部分时间还会出现转换错误的情况,而用DateTimeFormatter对含有微妙值的时间格式字符串转LocalDateTime则一切正常。

但是一般业务不会用到时间格式的毫秒或者说微秒值,如果真的用到的话建议用LocalDateTime存储,Mysql需要用datetime(6)这样就可以保存微秒值的时间,如图

Java格式化日期 微秒_java_02

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;
}

转换之后结果如图

Java格式化日期 微秒_时间格式_03


标签:格式化,java,import,return,LocalDateTime,Date,Java,微秒,public
From: https://blog.51cto.com/u_10917175/5925154

相关文章

  • Java项目开发小tips
    1、idea对于JS代码的兼容性较差,编写了js代码但是有时候不能正常加载。解决方法:(1)idea缓存清理;  (2)clear-install;先clear,清理完成之后再install。  (3)rebuild重......
  • 小新学Java15-【字节流、字符流】
    一、IO概述1.1什么是IO1.2IO的分类1.3IO的流向说明图解1.4顶级父类们二、字节流2.1一切皆为字节一切文件数据(文本、图片、视频等)在存储时,都是以二进制数字的形......
  • java排序算法
    1.冒泡排序法冒泡排序,轮询两个相邻的数据进行比较,如果条件成立,则数据相互转换。直到数据转换完毕。Integer[]strr={7,5,4,8,6,9,2,3,1,0};for(inti=0;i<strr.l......
  • 关于java程序OOM的优化
    在很多时候,我们使用循环,在循环体中处理逻辑使用的了大量内存,最终导致程序OOM。对此,经过一些测试,最终找出优化方案。策略将不使用的对象赋值为null主动调用GC测试......
  • javascript-代码随想录训练营day24
    77.组合题目链接:https://leetcode.cn/problems/combinations/题目描述:给定两个整数n和k,返回范围[1,n]中所有可能的k个数的组合。你可以按任何顺序返回答案......
  • java-net-php-python-jsp汽车租赁管理系统计算机毕业设计程序
    OverridetheentrypointofanimageIntroducedinGitLabandGitLabRunner9.4.Readmoreaboutthe extendedconfigurationoptions.Beforeexplainingtheav......
  • 从零开始学Java系列之如何使用记事本编写java程序
    前言在上一篇文章中,壹哥给大家介绍了Java中的标识符及其命名规则、规范,Java里的关键字和保留字,以及Java中的编码规范。我们在之前编写案例时,使用的开发工具都是windows自......
  • 08JavaScript之JavaScript操作DOM对象方法
    通过元素类型的方法来操作:document.getElementById();//id名,在实际开发中较少使用,选择器中多用classid一般只用在顶级层存在不能太过依赖iddocument.getElementsByTagName......
  • java基础题目(面试有可能出现)
    1、面向对象(OO)的优点A.与人类的思维习惯一致B.信息隐藏,提高了程序的可维护性和安全性C.提高了程序的可重用行2.通常什么情况下使用数组?使用数组的好处是什么?当需要储存或......
  • 【Spring】web开发 javaConfig方式 图解
    spring3.2之后开始支持java配置方式开发web项目,不使用web.xml,但需要在servlet3.0环境,一般tomcat7会支持,6不行下图中:MyAppInitializer和SpringServletContainerInitializer是......