I will be giving input date time for a timezone and the timezone for the input date time and we want the relevant DateTime in the expected timezone.
And here is my method.
convertToTimezone("03/08/2010 20:19:00 PM","Asia/Shanghai","US/Central");
The above time is the time in Asia/Shanghai. We would like to know what is the corresponding time in US/Central.
It's working fine but I am getting a 1-hour difference from the actual time.
Can I know where I am going wrong?
Here is the code:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
public class DateUtil {
    private static String format_date = "MM/dd/yyyy HH:mm:ss a";
    public static void main(String a[]) {
        try {
            String sourceTimezone = "Asia/Shanghai";
            String destTimezone = "US/Central";
            String outputExpectedTimezone = convertToTimezone("03/08/2010 20:19:00 PM", sourceTimezone, destTimezone);
            System.out.println("outputExpectedTimezone :" + outputExpectedTimezone);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    public static String convertToTimezone(String inputDate, String inputDateTimezone, String destinationDateTimezone)
            throws Exception {
        String outputDate = null;
        SimpleDateFormat format = new SimpleDateFormat(format_date);
        format.setTimeZone(TimeZone.getTimeZone(inputDateTimezone));
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(inputDateTimezone));
        calendar.setTime(format.parse(inputDate));
        calendar.add(Calendar.MILLISECOND, -(calendar.getTimeZone().getRawOffset()));
        calendar.add(Calendar.MILLISECOND, -calendar.getTimeZone().getDSTSavings());
        calendar.add(Calendar.MILLISECOND, TimeZone.getTimeZone(destinationDateTimezone).getRawOffset());
        outputDate = format.format(calendar.getTime());
        return outputDate;
    }
}
 
     
    