I am trying to convert time from timezone to another using fullname timezone. Below is the java api i have used.
TimeZone.setDefault(TimeZone.getTimeZone("Africa/Algiers"));
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
String dateInString = "09:44:00";
TimeZone tzGMT01 = TimeZone.getTimeZone("Africa/Algiers");
formatter.setTimeZone(tzGMT01);
Date dateInAmerica = formatter.parse(dateInString);
String fromInput = formatter.format(dateInAmerica);
TimeZone tzGMT03 = TimeZone.getTimeZone("Asia/Bahrain");
formatter.setTimeZone(tzGMT03);
String toInput = formatter.format(dateInAmerica);
Time ts = java.sql.Time.valueOf(toInput);
The expected output for time value of 09:44:00 converted to "Asia/Bahrain" time is 11:44:00. However the output is 13:44:00.
Can someone please guide what is wrong in the above statements.
Your problem is that a Java Date is a full date value, not just a time value. When you don't give year/month/day values, they default to 1970/01/01.
The world was different back in 1970. Algiers was using UTC+0 timezone, and Bahrain was using UTC+4, which is unlike today, where Algiers is using UTC+1 timezone, and Bahrain is using UTC+3.
Ignoring date will also cause issues for timezones that has Daylight Savings Time.
As you can see, date matters.
Here is code to show it:
TimeZone tzAlgiers = TimeZone.getTimeZone("Africa/Algiers");
TimeZone tzBahrain = TimeZone.getTimeZone("Asia/Bahrain");
SimpleDateFormat fmtTimeAlgiers = new SimpleDateFormat("HH:mm:ss");
fmtTimeAlgiers.setTimeZone(tzAlgiers);
SimpleDateFormat fmtDatetimeAlgiers = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
fmtDatetimeAlgiers.setTimeZone(tzAlgiers);
SimpleDateFormat fmtFullAlgiers = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z (Z)");
fmtFullAlgiers.setTimeZone(tzAlgiers);
SimpleDateFormat fmtFullBahrain = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z (Z)");
fmtFullBahrain.setTimeZone(tzBahrain);
Date date1970 = fmtTimeAlgiers.parse("09:44:00");
System.out.println(fmtTimeAlgiers.format(date1970) + " " + tzAlgiers.getID());
System.out.println(fmtFullAlgiers.format(date1970) + " " + tzAlgiers.getID());
System.out.println(fmtFullBahrain.format(date1970) + " " + tzBahrain.getID());
System.out.println();
Date date2016 = fmtDatetimeAlgiers.parse("2016-01-01 09:44:00");
System.out.println(fmtDatetimeAlgiers.format(date2016) + " " + tzAlgiers.getID());
System.out.println(fmtFullAlgiers.format(date2016) + " " + tzAlgiers.getID());
System.out.println(fmtFullBahrain.format(date2016) + " " + tzBahrain.getID());
System.out.println();
Output
09:44:00 Africa/Algiers
1970-01-01 09:44:00 CET (+0000) Africa/Algiers
1970-01-01 13:44:00 AST (+0400) Asia/Bahrain
2016-01-01 09:44:00 Africa/Algiers
2016-01-01 09:44:00 CET (+0100) Africa/Algiers
2016-01-01 11:44:00 AST (+0300) Asia/Bahrain
The timezone names CET and AST are actually wrong for 1970. Algiers was using GMT, and Bahrain was using GST (Gulf Standard Time).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With