Skip to content Skip to sidebar Skip to footer

Local Date & Time To Utc And Then Utc To Local Date & Time

I am trying to convert locale time to UTC, and then UTC to locale time. But I am not getting the result. public class DateDemo { public static void main(String args[]) { DateF

Solution 1:

What you need to do to solve your problem is the following, you have your code to convert back to local time in this order :

simpleDateFormatter.setTimeZone(TimeZone.getDefault());
simpleTimeFormatter.setTimeZone(TimeZone.getDefault());

DategetDate= simpleDateFormatter.parse(utcDate);
DategetTime= simpleTimeFormatter.parse(utcTime);

and what you need to do is wait until you parse utcDate, utcTime Strings back to Date Object then set the date formatter time zone to local zone as follows :

DategetDate= simpleDateFormatter.parse(utcDate);
DategetTime= simpleTimeFormatter.parse(utcTime);

simpleDateFormatter.setTimeZone(TimeZone.getDefault());
simpleTimeFormatter.setTimeZone(TimeZone.getDefault());

this should print the correct date/time in local.

Edit: here is the full main method :

publicstaticvoidmain(String[] args) {
    DateFormatdateFormatter= DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault());
    TimeZone.setDefault(TimeZone.getDefault());
    SimpleDateFormatsimpleDateFormatter=newSimpleDateFormat("dd/MM/yyyy");
    SimpleDateFormatsimpleTimeFormatter=newSimpleDateFormat("hh:mm:ss a");
    Datetoday=newDate();
    StringlocaleFormattedInTime= dateFormatter.format(today);
    try {
        DateparsedDate= dateFormatter.parse(localeFormattedInTime);
        System.out.println("Locale:" + localeFormattedInTime);
        System.out.println("After parsing a date: " + parsedDate);

        simpleDateFormatter.setTimeZone(TimeZone.getDefault());
        simpleTimeFormatter.setTimeZone(TimeZone.getDefault());

        Stringdate= simpleDateFormatter.format(today);
        Stringtime= simpleTimeFormatter.format(today);
        System.out.println("Today's only date: " + date);
        System.out.println("Today's only time: " + time);

        //// Locale to UTC converting

        System.out.println("TimeZone.getDefault() >>> " + TimeZone.getDefault());

        simpleDateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
        simpleTimeFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));

        StringutcDate= simpleDateFormatter.format(today);
        StringutcTime= simpleTimeFormatter.format(today);
        System.out.println("Convert into UTC's date: " + utcDate);
        System.out.println("Convert into UTC's only time: " + utcTime);

        //// UTC to locale converting/**
         ** //////EDIT
        */// at this point your utcDate,utcTime are strings that are formated in UTC// so first you need to parse them back to Dates using UTC format not LocaleDategetDate= simpleDateFormatter.parse(utcDate);
        DategetTime= simpleTimeFormatter.parse(utcTime);

        // NOW after having the Dates you can change the formatters timezone to your// local to format them into strings
        simpleDateFormatter.setTimeZone(TimeZone.getDefault());
        simpleTimeFormatter.setTimeZone(TimeZone.getDefault());

        StringgetLocalDate= simpleDateFormatter.format(getDate);
        StringgetLocalTime= simpleTimeFormatter.format(getTime);
        System.out.println("Get local date: " + getLocalDate);
        System.out.println("Get local time: " + getLocalTime);

    } catch (ParseException e) {
        e.printStackTrace();
    }

}

Solution 2:

Your problem is in lines 54 and 55:

DategetDate= simpleDateFormatter.parse(utcDate);
    DategetTime= simpleTimeFormatter.parse(utcTime);

These lines are merely parsing Strings that contain the date and time, but these strings do not have any timezone information:

utcDate = "09/11/2012"utcTime = "12:15:30 PM"

Therefore the parser assumes that the Strings are already in the locale of the timezone you set in lines 51 and 52.

Now think about how to fix it ;) HINT: Make sure the parser is assuming the correct timezone of the time represented by the strings.

PS: [RESOLVED!]I solved the problem but I discovered that the timezone conversion is erratic, for at least where I am. Time is 8:30 pm local. Convert to UTC, 12:30 pm (correct, 8 hr difference). Convert back, it's 8:00 pm (WRONG, eventhough the set timezone is correct - I got the original one and passed it back in - I'm only getting 7.5 hour difference). You should look for more reliable ways unless you can figure out what's going on and how to solve it.

[RESOLUTION:] The problem above was actually because the original code was splitting the date and time into two different parsers. If you use just one parser for both date and time combined you will get the correct date and time in the target locale. So in conclusion the parser is reliable but the way you use it makes a big difference!

SimpleDateFormatsimpleDateTimeFormatter=newSimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
    DategetDateTime= simpleDateTimeFormatter.parse(utcDate + " " + utcTime);
    //use above line if you have the date and time as separate strings
    simpleDateTimeFormatter.setTimeZone(TimeZone.getDefault());
    StringgetLocalDateTime= simpleDateTimeFormatter.format(getDateTime);
    System.out.println("Get local date time: " + getLocalDateTime);

WHY USING TWO SEPARATE PARSERS FOR DATE AND TIME IS UNRELIABLE: As explained above, it's a bad idea to use two separate parsers for date and time parts. Here's why:

Date getDate = simpleDateFormatter.parse(utcDate);
    Date getTime = simpleTimeFormatter.parse(utcTime);
    //Time zone changed tolocal here
    String getLocalDate2 = simpleDateTimeFormatter.format(getDate);
    String getLocalTime2 = simpleDateTimeFormatter.format(getTime);
    System.out.println("Get local date2: " + getLocalDate2);
    System.out.println("Get local time2: " + getLocalTime2);

    OUTPUT:
    Getlocal date2: 10/11/201208:00:00 AM
    Getlocal time2: 01/01/197010:35:10 AM

I get the half hour difference because the default date 01/01/1970 is used in the Date variable storing time (second line). When this is converted to local timezone, the error happens as the formatter bases its conversion on the default date 01/01/1970(where I live, the time difference was +7.5 hours in 1970 - today, it is +8 hours). This is why two separate parsers is not reliable even if you get the right result and you must always use a combined parser that accepts both date and time information.

Post a Comment for "Local Date & Time To Utc And Then Utc To Local Date & Time"