0

My basic query is converting a time in a particular place to the corresponding time in another place . Eg: What is the time in Los-angeles when it is 23:30 in India ? What is the time in Los-angeles when it is 10:00 in Germany ?

The inputs that were given were The target and relative place timezones and the time to be converted .

eg: Input ["America/Los_angeles" , "Germany" , "23:00"]

I tried a variety of approches such as the following :

1) Getting the current time in both the timezones and finding the difference and adding the difference to the time .

2) Using the below code :

String target_timezone = "America/Los_Angeles";
    String rel_timezone ="Germany";
     Calendar rel_calender = new GregorianCalendar(TimeZone.getTimeZone(rel_timezone));

     rel_calender.set(Calendar.HOUR, Integer.parseInt(arr[0]));
     rel_calender.set(Calendar.MINUTE, Integer.parseInt(arr[1]));
     rel_calender.set(Calendar.SECOND, 00);

     Calendar TargetTime = new GregorianCalendar(TimeZone.getTimeZone(target_timezone));
     TargetTime.setTimeInMillis(rel_calender.getTimeInMillis());
     SimpleDateFormat f = new SimpleDateFormat("EEEE,yyyy-MM-dd hh:mm a Z");
     System.out.println("time in  " + target_timezone + "when it is 17:00 in " + rel_timezone + ": " );
     System.out.println((TargetTime.get(Calendar.DST_OFFSET)));
     System.out.print((TargetTime.get(Calendar.HOUR)));
     System.out.print(":" +(TargetTime.get(Calendar.MINUTE)));
     System.out.println(":" +(TargetTime.get(Calendar.SECOND)));
     System.out.println((TargetTime.get(Calendar.YEAR)));

The year comes out wrong .!

Is there a better way to do this ? I have tried various methods of Calendar, SimpleDateFormat and Date in vain .I can use only basic java utilities and not Joda utilities . Thanks in advance .

Rasika Vijay
  • 395
  • 1
  • 7
  • 17
  • In my test the year is 2014 what looks ok. – Jens Sep 11 '14 at 06:21
  • So, what do you expect the output to be, and what do you get as output instead? Why don't you use your SimpleDateFormat? – JB Nizet Sep 11 '14 at 06:26
  • @Jens : Yea sorry the output i am getting is "2014-09-11 10:00 AM " which is wrong . – Rasika Vijay Sep 11 '14 at 06:42
  • @JBNizet : I want the correct time and date in yyyy-MM-dd hh:MM:ss a Z format . If i get the correct answer then i can format using SimpleDateFormat . But I am unable to get correct answer in the first place ! – Rasika Vijay Sep 11 '14 at 06:46
  • @rasikavijay And what is the correct time in your opinion? – Jens Sep 11 '14 at 06:48
  • @Jens :Berlin (Germany - Berlin) Thursday, 11 September 2014, 17:00:00 CEST UTC+2 hours Los Angeles (U.S.A. - California) Thursday, 11 September 2014, 08:00:00 PDT UTC-7 hours Corresponding UTC (GMT) Thursday, 11 September 2014, 15:00:00 This is the answer I was expecting as in the time in US-LA when it is 5 pm in Germany is : – Rasika Vijay Sep 11 '14 at 06:55

3 Answers3

1

You asked:

What is the time in Los-angeles when it is 23:30 in India ? What is the time in Los-angeles when it is 10:00 in Germany ?

It is impossible to answer these questions as stated. While India uses UTC+05:30 year-round, Los Angeles oscillates between UTC-08:00 and UTC-07:00, and Germeny oscillates between UTC+01:00 and UTC+02:00. These changes occur to to daylight saving time.

Also, if you go back into history, you'll find many other changes to these time zones. So even the offsets I just stated aren't necessarily true.

There are only two ways to perform this sort of adjustment:

  • Only work with time zones that have never changed their offset, and there are very few of those if you look across all recorded history.

  • Apply the adjustment on a specific date. For example, you can convert "today" in Los Angeles at 23:30 to any time zone you like.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
0

The best way to convert time is to have them reverted to UTC by applying the offset and then switching them back to the required time by applying that offset.

Example :

11/09/2014 12:00 AM - INDIA - Where the UTC offset for IST is +5:30. You wanted to converted to Central Time (CST), where the UTC offset is -6:00.

Now get the 12:00AM IST equivalent UTC = 6:30 PM UTC

Now add the CST's UTC offset to this UTC time to get the CST equivalent of 12:00AM IST.

Which is 10/09/2014 12:30 AM.

Hope this helps..

0

Use This code:

     Calendar rel_calender = new GregorianCalendar();

     rel_calender.set(Calendar.HOUR_OF_DAY, 17);
     rel_calender.set(Calendar.MINUTE, 00);
     rel_calender.set(Calendar.SECOND, 00);
     rel_calender.get(Calendar.HOUR);

     rel_calender.setTimeZone(TimeZone.getTimeZone(rel_timezone));

If you use your code the rel_calendar hour is set to 19 O'clock' (17+2 the time offset).

Jens
  • 67,715
  • 15
  • 98
  • 113
  • Thanks that worked ! I can now get the date and time in target timezone correctly . But I tried similar approach for day_of_week. It still points to the first timezone dayof weeek. How is this possible ? – Rasika Vijay Sep 11 '14 at 07:39
  • Sorry to post the code as an answer but it was too big to post in comment section – Rasika Vijay Sep 11 '14 at 07:59
  • @rasikavijay So I have it. You can delete the answer. And what is the problem exactly? What you get and what you expect? – Jens Sep 11 '14 at 08:05
  • Sorry my mistake . I got confused between Java calendar api and simple date format . The calendar api gives the int for september as 8 but simpledateformat parses it as august . I thought the input to simple date format was wrong . My bad . Thanks again ! – Rasika Vijay Sep 11 '14 at 08:12
  • This answer is fine, but it does assume that you want the time zone conversion *as of today*, since the `Calendar` is initialized to the current date and time of the local system. Keep in mind also that "today" in the local time zone, might not be the same "today" in the source or target time zones – Matt Johnson-Pint Sep 11 '14 at 19:31