Android Format date with time zone

fargath picture fargath · Jan 4, 2013 · Viewed 25.5k times · Source

I need to format the date into a specific string.

I used SimpleDateFormat class to format the date using the pattern "yyyy-MM-dd'T'HH:mm:ssZ" it returns current date as
"2013-01-04T15:51:45+0530" but I need as
"2013-01-04T15:51:45+05:30".

Below is the coding used,

Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.ENGLISH);      
Log.e(C.TAG, "formatted string: "+sdf.format(c.getTime()));

Output: formatted string: 2013-01-04T15:51:45+0530

I need the format as 2013-01-04T15:51:45+05:30 just adding the colon in between gmt time.

Because I'm working on Google calendar to insert an event, it accepts only the required format which I have mentioned.

Answer

Grimmy picture Grimmy · Aug 5, 2015

You can also use "ZZZZZ" instead of "Z" in your pattern (according to documentation). Something like this

    Calendar c = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ", Locale.ENGLISH);      
    Log.e(C.TAG, "formatted string: "+sdf.format(c.getTime()));