YYYY-MM-DDThh:mm:ssTZD的Java SimpleDateFormat [英] Java SimpleDateFormat for YYYY-MM-DDThh:mm:ssTZD

查看:337
本文介绍了YYYY-MM-DDThh:mm:ssTZD的Java SimpleDateFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用的外部API之一

One of the external APIs we use requires

YYYY-MM-DDThh:mm:ssTZD

要传递的格式

XMLGregorianCalendar

对象。我不确定Java中是否有任何支持 T 的东西。我想知道,是否可以在Java中将日期解析为上述格式?他们提供
的有效日期的示例是

object. I am not sure if there is anything in Java that supports "T". I was wondering, if a date can be parsed into above format in Java? An example of a valid date they have provided is

2009-07-16T19:20:30-05:00 ............

"2009-07-16T19:20:30-05:00"............

 Update:

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZ");        
        GregorianCalendar gc = new GregorianCalendar();
        String dateString = sdf.format(gc.getTime());       
        gc.setTime(sdf.parse(dateString));      
        XMLGregorianCalendar date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);

输出:

2014-04- 17T13:11:30.000 + 01:00

2014-04-17T13:11:30.000+01:00

推荐答案

使用 JodaTime的 DateTimeFormat 带有 yyyy-MM-dd'T'HH:mm:ssZ的API 日期模式

String date = "2009-07-16T19:20:30-05:00";
String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
DateTime dateTime = dtf.parseDateTime(date);
System.out.println(dateTime); // 2009-07-16T19:20:30-05:00

这篇关于YYYY-MM-DDThh:mm:ssTZD的Java SimpleDateFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆