5

I am currently using SimpleDateFormat to create a datetime value in the format yyyy-MM-dd HH:mm:ss. Here is my code:

     Date date = new SimpleDateFormat("yy-M-d H:m:ss").parse(datetime);
     String Dtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);

However I would like the format to be this instead:

  2013-04-12T13:38:48+02:00

How can I add the timezone to get the desired format?

Thank you!

1

6 Answers 6

12

Have a look at the API documentation of SimpleDateFormat. There you can see, that you can use the character 'z' for the timezone information.

Example: "yyyy.MM.dd G 'at' HH:mm:ss z" will give you "2001.07.04 AD at 12:08:56 PDT"

To set the timezone on a date have a look at this question and the answers.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! but z adds +0200 in the end. Is there a way to have "+02:00"? I need the ":" in the timezone..
@diego10 - FYI - with or without the :, and with or without the T are both acceptable forms of ISO8601 format. So assuming you want this format to interchange it with something - you should be fine. Still, you might want to look at Joda Time if you want superior date handling.
4

This is XML/XSD dateTime data type format. Use javax.xml.datatype.XMLGregorianCalendar

    XMLGregorianCalendar gc = DatatypeFactory.newInstance().newXMLGregorianCalendar("2013-04-12T13:38:48+02:00");
    System.out.println(gc);

output

2013-04-12T13:38:48+02:00

1 Comment

This is the one. You can use toGregorianCalendar.getTime to get a java.util.Date by the way.
2

If you use the 'z' or 'Z' then still you cannot be sure how your time zone will look like. It depends on your locals and JVM version which of the ISO8601 version will be chosen.

One of the way to have a timezone always in format "+02:00" is to request it. Use the following date format:

DateTimeFormatter formatter = 
    new DateTimeFormatterBuilder()
        .appendPattern("yyyy-MM-dd'T'HH:mm:ss")
        .appendOffset("+HH:MM","+00:00")
        .toFormatter();

And use it always with the ZonedDateTime type. It is thread safe.

Comments

1

I have been looking for exactly the same answer. I have to be backward compatible to the existing date format, which is, like in the example - "2013-04-12T13:38:48+02:00" and my other limitation is that i cannot use any open source libraries other than what is provided with Java 6. Therefore, after not finding the solution here, I came up with this simple conversion technique, which is quite ugly, but i figured out i post it here in case someone needs a quick and dirty solution. If someone knows how to do it better (with the limitations i mentioned), please correct me.

Therefore, to produce the output in the desired format:

private DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

public GregorianCalendar stringToCalendar(String v) 
{
    // 22 is an idx of last ':', you can replace with v.lastIndex(':') to be neat  
    String simpleV = v.substring(0, 22).concat(v.substring(23));   

    GregorianCalendar gc = (GregorianCalendar) df.getCalendar();

    gc.setTime(df.parse(simpleV));
    return gc;
}

public String calendarToString(GregorianCalendar v) 
{
    df.setCalendar(v); 

    String out = df.format(v.getTime());

    return out.substring(0, 22).concat(":").concat(out.substring(22)); // read above on '22'
}   

Like I said, this is far from perfect, but I did not find anything better..

1 Comment

I know this is an age old issue report but if anybody is curious about how to get this done in the format itself, just use a ZZ instead of Z. Using Z gives in the format +XXXX while ZZ will give it in the format +XX:XX
0

There is a simple way of doing it. You can define your format like: String Dtime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX").format(date);

This should do it.

Comments

-1

You easily achieve it by package java.util.Calendar

Try the following code

       import java.util.Calendar; //Import package

        String month_order[]={"Select...","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH) + 1;
        int dom = cal.get(Calendar.DAY_OF_MONTH);
        int doy=cal.get(Calendar.YEAR);
        System.out.println("Current date: "+month_order[month]);
        System.out.println("Current Month:  "+dom);
        System.out.println("Current year: "+doy);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.