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!

Leigh
  • 28,765
  • 10
  • 55
  • 103
diego10
  • 533
  • 2
  • 11
  • 18
  • you can follow the link of Stack overflow : http://stackoverflow.com/questions/1305350/how-to-get-the-current-date-and-time-of-your-timezone-in-java – user112233 Apr 12 '13 at 11:47

6 Answers6

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.

Community
  • 1
  • 1
thalador
  • 834
  • 8
  • 23
  • 2
    Thank you! but z adds +0200 in the end. Is there a way to have "+02:00"? I need the ":" in the timezone.. – diego10 Apr 12 '13 at 11:56
  • 1
    @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](http://joda-time.sourceforge.net/) if you want superior date handling. – Matt Johnson-Pint Apr 12 '13 at 14:05
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
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
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.

swist
  • 1,111
  • 8
  • 18
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..

tulu
  • 73
  • 1
  • 8
  • 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 – Deepak G M Jan 04 '21 at 17:12
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.

mohdnaveed
  • 506
  • 5
  • 13
-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);
kark
  • 4,763
  • 6
  • 30
  • 44