格式化时间字符串yyyy-MM-dd和YYYY-MM-dd的区别,HH:mm:ss和hh:mm:ss的区别,格式化LocalDateTime.now()和Date,时间戳和时间互转
发布日期:2021-11-02 12:48:57
浏览次数:323
分类:精选文章
本文共 4086 字,大约阅读时间需要 13 分钟。
时间格式化的时候HH:mm:ss和hh:mm:ss的区别
-
hh:mm:ss 按照12小时制的格式进行字符串格式化
如果时间处于00:00:00——12:59:59,则返回的字符串正常
如果时间处于13:00:00——23:59:59,则返回的字符串是实际时间-12小时后的值,也就是说比真实的时间少了12个小时。
例如:14:00:00进行格式化后的字符串为“2:00:00”
-
HH:mm:ss
按照24小时制的格式进行字符串格式化
当时间为任意一个区间,则返回的字符串都是正常的。
yyyy-MM-dd和YYYY-MM-dd的区别
import java.text.SimpleDateFormatimport java.time.LocalDateimport java.time.format.DateTimeFormatterDateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")DateTimeFormatter simpleDateFormat2 = DateTimeFormatter.ofPattern("YYYY-MM-dd")LocalDate localDate = LocalDate.of(2019,12,31)String qqq = dateTimeFormatter.format(localDate)String www = simpleDateFormat2.format(localDate)System.out.println(qqq)System.out.println(www)
解释一波: yyyy-MM-dd是真真正正的年份,但是YYYY-MM-dd代表的是基于周的年份日期,也就是说一周处在跨年中(比如这一周的周3是2019年,周4是2020年,这一周就是跨年周),那么采用YYYY-MM-dd就会把这周归入到下一年.也就会造成显示错误.这其实不是bug,就是这样设计的.所以日常在使用格式化时间日期的时候采用yyyy开头的比较好
还有大写的DD代表的是处于这一年中的哪一天,而不是处于这个月的哪一天
Date
字符串转Date
package com.test.dateFormat; import java.text.ParseException;import java.text.SimpleDateFormat; import org.junit.Test; public class String2Date { @Test public void test() throws ParseException { String string = "2016-10-24 21:59:06"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.parse(string)); }}
Date转字符串
package com.test.dateFormat; import java.text.SimpleDateFormat;import java.util.Date; import org.junit.Test; public class Date2String { @Test public void test() { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); System.out.println(sdf.format(date)); sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(date)); sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); System.out.println(sdf.format(date)); }}
字符串转LocalDateTime,LocalDateTime转Date
字符串转LocalDateTime
String timeStr = "2019-08-26 18:03:33";DateTimeFormatter timeDtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//字符串转LocalDateTimeLocalDateTime localDateTime = LocalDateTime.parse(timeStr, timeDtf);//LocalDateTime转DateZoneId zone = ZoneId.systemDefault();Instant instant = localDateTime.atZone(zone).toInstant();Date date = Date.from(instant);
LocalDateTime转为自定义的时间格式的字符串
public static String getDateTimeAsString(LocalDateTime localDateTime, String format) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format); return localDateTime.format(formatter);}
将long类型的timestamp转为LocalDateTime(时间戳转LocalDateTime)
方法1
public static LocalDateTime getDateTimeOfTimestamp(long timestamp) { Instant instant = Instant.ofEpochMilli(timestamp); ZoneId zone = ZoneId.systemDefault(); return LocalDateTime.ofInstant(instant, zone);}
方法2,指定时区
Instant类的ofEpochMilli()方法使用从1970-01-01T00:00:00
的纪元作为参数传递的毫秒数来帮助获取Instant,即传入的参数是从1970-01-01T00:00:00
开始的毫秒值。 import java.time.Instantimport java.time.LocalDateTimeimport java.time.ZoneOffset//获得时间戳//long milliseconds = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();long milliseconds = 1616052960373// 将时间戳转为当前时间LocalDateTime localDateTime = Instant.ofEpochMilli(milliseconds) .atZone(ZoneOffset.ofHours(8)) .toLocalDateTime();System.out.println(localDateTime);println(ZoneOffset.ofHours(8))关于
ZoneId
println(ZoneOffset.ofHours(8))println(ZoneId.of(ZoneId.systemDefault() as String))println(ZoneId.of("Asia/Shanghai"))
时间戳转Date
import java.text.SimpleDateFormatSimpleDateFormat simpleDateFormat = new SimpleDateFormat('yyyy-MM-dd HH:mm:ss')println(simpleDateFormat.format(new Date(1616052960373l)))
将LocalDateTime转为long类型的timestamp(LocalDateTime转时间戳)
public static long getTimestampOfDateTime(LocalDateTime localDateTime) { ZoneId zone = ZoneId.systemDefault(); Instant instant = localDateTime.atZone(zone).toInstant(); return instant.toEpochMilli();}
将某时间字符串转为自定义时间格式的LocalDateTime
public static LocalDateTime parseStringToDateTime(String time, String format) { DateTimeFormatter df = DateTimeFormatter.ofPattern(format); return LocalDateTime.parse(time, df);}
转载地址:https://blog.csdn.net/weixin_43944305/article/details/103922671 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月06日 03时59分47秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
SpringBoot快速入门_CodingPark编程公园
2021-06-29
命名实体识别_CodingPark编程公园
2021-06-29
Ajax+SpringBoot注册登录实战_CodingPark编程公园
2021-06-29
光纤猫连接路由器_CodingPark编程公园
2021-06-29
信息抽取_CodingPark编程公园
2021-06-29
如何理解输入流输出流?_CodingPark编程公园
2021-06-29
Python类|实例|方法|继承_专题_CodingPark编程公园
2021-06-29
机器学习心得_CodingPark编程公园
2021-06-29
自然语言工程师心得概述_CodingPark编程公园
2021-06-29
for循环那点事儿_CodingPark编程公园
2021-06-29
Int -> List | List -> Int _ CodingPark编程公园
2021-06-29
如何在junit中使用SpringFramework的Ioc容器
2021-06-29
一个案例教你理解Spring面向切面编程(Spring Aop)
2021-06-29
手把手教你整合SSM框架
2021-06-29
自己造个简单数据校验的注解@Value和@Mail
2021-06-29
Poj百练 4148:生理周期 (分类:枚举)
2021-06-29
Java如何读写注册表
2021-06-29
java如何利用模板文件生成word文档
2021-06-29
java读写xlsx格式的MS Excel文件
2021-06-29
vue的一些基础知识点
2021-06-29