Java convert date and time between timezone
package com.w3schools;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class ConvertTimeZoneTest {
private static final String DATE_FORMAT = "dd-M-yyyy hh:mm:ss a";
public static void main(String args[]) throws InterruptedException, ParseException{
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
String dateInString = "09-04-2018 11:55:35 AM";
Date date = formatter.parse(dateInString);
TimeZone tz = TimeZone.getDefault();
//From TimeZone Asia/Kolkata
System.out.println("TimeZone : " + tz.getID() + " - " + tz.getDisplayName());
System.out.println("TimeZone : " + tz);
System.out.println("Date (Singapore) : " + formatter.format(date));
//To TimeZone America/New_York
SimpleDateFormat sdfAmerica = new SimpleDateFormat(DATE_FORMAT);
TimeZone tzInAmerica = TimeZone.getTimeZone("America/New_York");
sdfAmerica.setTimeZone(tzInAmerica);
String sDateInAmerica = sdfAmerica.format(date); // Convert to String first
Date dateInAmerica = formatter.parse(sDateInAmerica); // Create a new Date object
System.out.println("nTimeZone : " + tzInAmerica.getID() + " - "
+ tzInAmerica.getDisplayName());
System.out.println("TimeZone : " + tzInAmerica);
System.out.println("Date (New York) (String) : " + sDateInAmerica);
System.out.println("Date (New York) (Object) : " + formatter.format(dateInAmerica));
}
} |
package com.w3schools;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class ConvertTimeZoneTest {
private static final String DATE_FORMAT = "dd-M-yyyy hh:mm:ss a";
public static void main(String args[]) throws InterruptedException, ParseException{
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
String dateInString = "09-04-2018 11:55:35 AM";
Date date = formatter.parse(dateInString);
TimeZone tz = TimeZone.getDefault();
//From TimeZone Asia/Kolkata
System.out.println("TimeZone : " + tz.getID() + " - " + tz.getDisplayName());
System.out.println("TimeZone : " + tz);
System.out.println("Date (Singapore) : " + formatter.format(date));
//To TimeZone America/New_York
SimpleDateFormat sdfAmerica = new SimpleDateFormat(DATE_FORMAT);
TimeZone tzInAmerica = TimeZone.getTimeZone("America/New_York");
sdfAmerica.setTimeZone(tzInAmerica);
String sDateInAmerica = sdfAmerica.format(date); // Convert to String first
Date dateInAmerica = formatter.parse(sDateInAmerica); // Create a new Date object
System.out.println("nTimeZone : " + tzInAmerica.getID() + " - "
+ tzInAmerica.getDisplayName());
System.out.println("TimeZone : " + tzInAmerica);
System.out.println("Date (New York) (String) : " + sDateInAmerica);
System.out.println("Date (New York) (Object) : " + formatter.format(dateInAmerica));
}
}
Output:
TimeZone : Asia/Calcutta - India Standard Time
TimeZone : sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,
dstSavings=0,useDaylight=false,transitions=6,lastRule=null]
Date (Singapore) : 09-4-2018 11:55:35 AM
TimeZone : America/New_York - Eastern Standard Time
TimeZone : sun.util.calendar.ZoneInfo[id="America/New_York",offset=-18000000,dstSavings=3600000,
useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/New_York,
offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,
startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,
endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]
Date (New York) (String) : 09-4-2018 02:25:35 AM
Date (New York) (Object) : 09-4-2018 02:25:35 AM |
TimeZone : Asia/Calcutta - India Standard Time
TimeZone : sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,
dstSavings=0,useDaylight=false,transitions=6,lastRule=null]
Date (Singapore) : 09-4-2018 11:55:35 AM
TimeZone : America/New_York - Eastern Standard Time
TimeZone : sun.util.calendar.ZoneInfo[id="America/New_York",offset=-18000000,dstSavings=3600000,
useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/New_York,
offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,
startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,
endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]
Date (New York) (String) : 09-4-2018 02:25:35 AM
Date (New York) (Object) : 09-4-2018 02:25:35 AM