SimpleDateFormat – ISO8601 format

I have done some date format operations in Java and got to know about the Zulu time which is nothing but the UTC time.

Now, I have a date and want to format that into “yyyy-MM-dd’T’HH:mm:ss.SSSZ” format. Here Z stands for zulu timezone.

Refer the below program and the output of those.


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;


public class Main {

    public static void main(String[] args) {

        //ISO8601 format
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

        //EST time
        Calendar calendar1 = Calendar.getInstance();
        System.out.println("calendar1::" + calendar1.getTimeZone());
        System.out.println("\n");
        System.out.println("calendar1.getTime()::" + dateFormat.format(calendar1.getTime()));
        System.out.println("\n");
        //PDT time
        Calendar calendar2 = Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles"));
        dateFormat.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
        System.out.println("calendar2::" + calendar2.getTimeZone());
        System.out.println("calendar2.getTime()::" + dateFormat.format(calendar2.getTime()));

    }
}


calendar1::sun.util.calendar.ZoneInfo[id="America/New_York",offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/New_York,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]


calendar1.getTime()::2017-06-02T17:03:04.643-0400


calendar2::sun.util.calendar.ZoneInfo[id="America/Los_Angeles",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]
calendar2.getTime()::2017-06-02T14:03:04.645-0700

calendar1 uses the default timezone and in this case its EST time. So when i am doing the format, the output string is “2017-06-02T16:53:24.122-0400”. Here -0400 represents the time difference between the current timezone(EST) and UTC(Zulu) time.

calendar2 uses the PDT time and also set the same timezone to SimpleDateFormat as well. So the output string is “2017-06-02T13:53:24.124-0700”. Here the -0700 represents the time difference between the PDT timezone and UTC(Zulu) time.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s