Created
March 19, 2020 12:00
-
-
Save bmnepali/f381ede72949c2fcbce60ba4cff254d4 to your computer and use it in GitHub Desktop.
Convert date from one timezone to another
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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