Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

date

One of the external APIs we use requires

"YYYY-MM-DDThh:mm:ssTZD"

format to be passed in

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

"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

like image 361
Abidi Avatar asked Apr 17 '14 11:04

Abidi


People also ask

What does YYYY MM DD HH SS SSSZ mean?

Date formatting Dates are formatted using the following format: "yyyy-MM-dd'T'hh:mm:ss'Z'" if in UTC or "yyyy-MM-dd'T'hh:mm:ss[+|-]hh:mm" otherwise. On the contrary to the time zone, by default the number of milliseconds is not displayed.

What can I use instead of SimpleDateFormat?

DateTimeFormatter is a replacement for the old SimpleDateFormat that is thread-safe and provides additional functionality.

What is the use of SimpleDateFormat in Java?

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.

What is the difference between DateFormat and SimpleDateFormat?

The java SimpleDateFormat allows construction of arbitrary non-localized formats. The java DateFormat allows construction of three localized formats each for dates and times, via its factory methods.


2 Answers

Yes,you can.Try this date formatter.

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
like image 145
mstzn Avatar answered Sep 21 '22 18:09

mstzn


You need to put it in single quotes.

Try

YYYY-mm-dd'T'hh:MM:ssZ
like image 38
Peter Lawrey Avatar answered Sep 19 '22 18:09

Peter Lawrey