noNameYet
noNameYet

Reputation: 655

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[])
{
    DateFormat dateFormatter = 
               DateFormat.getDateTimeInstance
                 (DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault());

    TimeZone.setDefault(TimeZone.getDefault());

    SimpleDateFormat simpleDateFormatter = new SimpleDateFormat("dd/MM/yyyy");
    SimpleDateFormat simpleTimeFormatter = new SimpleDateFormat("hh:mm:ss a");

    Date today = new Date();
    String localeFormattedInTime = dateFormatter.format(today);

    try
    {
        Date parsedDate = dateFormatter.parse(localeFormattedInTime);
        System.out.println("Locale:" + localeFormattedInTime);
        System.out.println("After parsing a date: " + parsedDate);

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

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

        //// Locale to UTC converting

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

        String utcDate = simpleDateFormatter.format(today);
        String utcTime = 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

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

        Date getDate = simpleDateFormatter.parse(utcDate);
        Date getTime = simpleTimeFormatter.parse(utcTime);

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

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

I am sending local date & time to the web service, and then when require I need to retrieve the UTC date & time and then convert into locale date & time (i.e. user's local settings).

Sample Output:

Locale:11/9/12 8:15 PM
After parsing a date: Fri Nov 09 20:15:00 SGT 2012
Today's only date: 09/11/2012
Today's only time: 08:15:30 PM
Convert into UTC's date: 09/11/2012
Convert into UTC's only time: 12:15:30 PM
Get local date: 09/11/2012
Get local time: 12:15:30 PM

After Saksak & ADTC answers:

For the code fragment, UTC date & time (what is actually coming as GMT-5 because database may be in USA) is the input, and I want to get local date & time as output. But this following segment is still giving GMT-5 time.

SimpleDateFormat simpleDateTimeFormatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");

....

Date inDateTime = simpleDateTimeFormatter.parse(intent.getExtras().getString("inTime")); Date outDateTime = simpleDateTimeFormatter.parse(intent.getExtras().getString("outTime"));

simpleDateTimeFormatter.setTimeZone(TimeZone.getDefault()); simpleDateTimeFormatter.setTimeZone(simpleDateTimeFormatter.getTimeZone());

//TimeZone tzTimeZone = TimeZone.getDefault();

//System.out.println("Current time zone: " + tzTimeZone.getDisplayName());

String getLocalInTimeString = simpleDateTimeFormatter.format(inDateTime); 

String getLocalOutTimeString = simpleDateTimeFormatter.format(outDateTime);

My question: getLocalInTimeString & getLocalOutTimeString still showing GMT-5 timing. What's wrong here? Do I need to set any other things?

Upvotes: 0

Views: 10397

Answers (2)

Moh Sakkijha
Moh Sakkijha

Reputation: 2705

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

Date getDate = simpleDateFormatter.parse(utcDate);
Date getTime = 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 :

Date getDate = simpleDateFormatter.parse(utcDate);
Date getTime = 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 :

public static void main(String[] args) {
    DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault());
    TimeZone.setDefault(TimeZone.getDefault());
    SimpleDateFormat simpleDateFormatter = new SimpleDateFormat("dd/MM/yyyy");
    SimpleDateFormat simpleTimeFormatter = new SimpleDateFormat("hh:mm:ss a");
    Date today = new Date();
    String localeFormattedInTime = dateFormatter.format(today);
    try {
        Date parsedDate = dateFormatter.parse(localeFormattedInTime);
        System.out.println("Locale:" + localeFormattedInTime);
        System.out.println("After parsing a date: " + parsedDate);

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

        String date = simpleDateFormatter.format(today);
        String time = 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"));

        String utcDate = simpleDateFormatter.format(today);
        String utcTime = 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 Locale
        Date getDate = simpleDateFormatter.parse(utcDate);
        Date getTime = 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());

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

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

}

Upvotes: 4

ADTC
ADTC

Reputation: 10086

Your problem is in lines 54 and 55:

    Date getDate = simpleDateFormatter.parse(utcDate);
    Date getTime = 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!

    SimpleDateFormat simpleDateTimeFormatter
            = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
    Date getDateTime
            = simpleDateTimeFormatter.parse(utcDate + " " + utcTime);
    //use above line if you have the date and time as separate strings
    simpleDateTimeFormatter.setTimeZone(TimeZone.getDefault());
    String getLocalDateTime = 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 to local 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:
    Get local date2: 10/11/2012 08:00:00 AM
    Get local time2: 01/01/1970 10: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.

Upvotes: 4

Related Questions