YYYY-MM-DDThh:mm:ssTZD 的 Java SimpleDateFormat

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23132145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:56:56  来源:igfitidea点击:

Java SimpleDateFormat for YYYY-MM-DDThh:mm:ssTZD

javadate

提问by Abidi

One of the external APIs we use requires

我们使用的外部 API 之一需要

"YYYY-MM-DDThh:mm:ssTZD"

"YYYY-MM-DDThh:mm:ssTZD"

format to be passed in

要传入的格式

XMLGregorianCalendar

XMLGregorianCalendar

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

目的。我不确定 Java 中是否有任何支持"T". 我想知道,是否可以在 Java 中将日期解析为上述格式?他们提供的有效日期的一个例子是

"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);

Output:

输出:

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

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

回答by Abimaran Kugathasan

Use JodaTime'sDateTimeFormatAPI with "yyyy-MM-dd'T'HH:mm:ssZ"date pattern

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

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

回答by Peter Lawrey

You need to put it in single quotes.

你需要把它放在单引号中。

Try

尝试

YYYY-mm-dd'T'hh:MM:ssZ

回答by cbach

Try Java 8 LocalDateTime.parse

试试 Java 8 LocalDateTime.parse

You could also use threetenif you're on an earlier Java Version.

如果您使用的是较早的 Java 版本,也可以使用Threeten

Example: DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ").format(yourLocalDateTime)

例子: DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ").format(yourLocalDateTime)

回答by mstzn

Yes,you can.Try this date formatter.

是的,你可以。试试这个日期格式化程序。

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");