java_基础_日期格式yyyy-MM-dd’T’HH:mm:ss.SSSZ

转载自: https://blog.youkuaiyun.com/stromcruise/article/details/72911157

yyyy-MM-dd’T’HH:mm:ss.SSSZ

后面的三个SSS指的是毫秒,Z代表的时区,中间的T代表可替换的任意字符。

下面看例子:
例子一:

    @Test
        public void testTime() throws ParseException{
           Date date = new Date(); 
           SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
           SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'测试'HH:mm:ss.SSSZ"); 
           String str = df.format(date);
           String str1 = df1.format(date);     
           System.out.println(str);
           System.out.println(str1);     
        }

测试结果

    2017-06-08T10:41:06.261+0800
    2017-06-08测试10:41:06.261+0800

例子二:

将2017-05-18T10:26:10.488Z转化为yyyy-MM-dd HH:mm:ss或者yyyyMMddHHmmss的格式

    @Test
        public void testTime1() throws ParseException{
           String dateStr = "2017-05-18T10:26:10.488Z";
           SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS",Locale.ENGLISH);//输入的被转化的时间格式
           SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//需要转化成的时间格式
           SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMddHHmmss");
           Date date1 = dff.parse(dateStr);     
           String str1 = df1.format(date1);
           String str2 = df2.format(date1);
           System.out.println("str1 is "+str1);
           System.out.println("str2 is "+str2);
        }

运行结果:

    str1 is 2017-05-18 10:26:10
    str2 is 20170518102610

以上均亲测: 童叟无欺。

### 将 `yyyy-MM-dd'T'HH:mm:ss.SSSZ` 格式的时间字符串解析为日期时间对象 为了将特定格式的时间字符串解析为 Java 中的日期时间对象,可以使用 `SimpleDateFormat` 或者更现代的方式使用 `DateTimeFormatter` 和 `ZonedDateTime` 类。以下是两种方法的具体实现: #### 方法一:使用 `SimpleDateFormat` ```java import java.text.ParseException; import java.text.SimpleDateFormat; public class Main { public static void main(String[] args) throws ParseException { String timeString = "2023-10-05T14:30:00.123+08:00"; SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX"); // 解析字符串为 Date 对象 java.util.Date date = sdfInput.parse(timeString); System.out.println(date); // 如果需要进一步处理或显示不同格式,则可继续转换 SimpleDateFormat sdfOutput = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String formattedDate = sdfOutput.format(date); System.out.println(formattedDate); } } ``` 这种方法适用于较旧版本的 JDK,在新项目中建议采用更新的方法。 #### 方法二:使用 `DateTimeFormatter` 和 `ZonedDateTime` 对于支持 Java 8 及以上版本的应用程序来说,推荐使用 `java.time` 包下的类来进行操作,因为这些类提供了更好的线程安全性和更多的功能特性。 ```java import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class Main { public static void main(String[] args) { String timeString = "2023-10-05T14:30:00.123+08:00"; // 创建与给定模式相匹配的 DateTimeFormatter 实例 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); // 使用 ZonedDateTime 来表示带有时区偏移量的时间戳 ZonedDateTime zdt = ZonedDateTime.parse(timeString, formatter); System.out.println(zdt); // 输出其他格式的结果 System.out.println(zdt.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); } } ``` 这种方式不仅能够正确地处理带有 UTC 偏移量的时间串,而且还可以方便地将其转化为不同的输出格式[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值