Skip to content
Advertisement

Android Format date with time zone

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.

Advertisement

Answer

You can use Joda Time instead. Its DateTimeFormat has a ZZ format attribute which does what you want.

Link

Big advantage: unlike SimpleDateFormat, DateTimeFormatter is thread safe. Usage:

DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZZ")
    .withLocale(Locale.ENGLISH);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement