Skip to content

Instantly share code, notes, and snippets.

@bmnepali
Created March 19, 2020 12:00
Show Gist options
  • Save bmnepali/f381ede72949c2fcbce60ba4cff254d4 to your computer and use it in GitHub Desktop.
Save bmnepali/f381ede72949c2fcbce60ba4cff254d4 to your computer and use it in GitHub Desktop.
Convert date from one timezone to another
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
/**
*
* Java program to display a date in different timezone in Java. Internally Java
* stores date as millisecond passed since 01-01-1970 00:00 GMT, which can be
* converted and display in any timezone using SimpleDateFormat in Java. In
* this Java program we will display the same date in two timezones, Indian time (IST)
* and PST, America/Los_Angeles .
*
*/
public class TimezoneConversionExample {
public static void main(String args[]) {
//capturing today's date
Date today = new Date();
//displaying this date on IST timezone
DateFormat df = new SimpleDateFormat("dd-MM-yy HH:mm:SS z");
df.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
String IST = df.format(today);
System.out.println("Date in Indian Timezone (IST) : " + IST);
//dispalying date on PST timezone
df.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
String PST = df.format(today);
System.out.println("Date in PST Timezone : " + PST);
}
}
// Output:
// Date in Indian Timezone (IST) : 06-11-12 07:39:61 IST
// Date in PST Timezone : 05-11-12 18:09:61 PST
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment