格式化时间字符串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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:MySQL-this is incompatible with sql_mode=only_full_group_by错误解决方案
下一篇:Jackson使用@JsonInclude配置实现,当controller返回前端的对象里有个属性为null的时候自动忽略这个属性,即不返回该属性

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月06日 03时59分47秒