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 .