diego10
diego10

Reputation: 573

Java Datetime with Timezone format

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!

Upvotes: 5

Views: 70675

Answers (6)

mohdnaveed
mohdnaveed

Reputation: 516

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.

Upvotes: 0

swist
swist

Reputation: 1171

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.

Upvotes: 2

thalador
thalador

Reputation: 834

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.

Upvotes: 12

tulu
tulu

Reputation: 73

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..

Upvotes: 1

kark
kark

Reputation: 4853

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

Upvotes: -1

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136002

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

Upvotes: 4

Related Questions