Created
March 20, 2020 18:11
-
-
Save bmnepali/06ee23ac86c4b2aadabd7d628b7ce111 to your computer and use it in GitHub Desktop.
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.time.LocalTime; | |
import java.time.ZoneId; | |
import java.time.ZoneOffset; | |
import java.time.ZonedDateTime; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.TimeZone; | |
import java.sql.Time; | |
/** | |
* | |
* 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(); | |
DateFormat df = new SimpleDateFormat("HH:mm:SS z"); | |
//displaying this date on IST timezone | |
df.setTimeZone(TimeZone.getTimeZone("Asia/Kathmandu")); | |
String IST = df.format(today); | |
System.out.println("Date in Nepal Timezone (IST) : " + IST); | |
//dispalying date on PST timezone | |
df.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata")); | |
String PST = df.format(today); | |
System.out.println("Date in India PST Timezone : " + PST); | |
//convert the current time into UTC time | |
LocalTime localTime = LocalTime.parse("10:00:00"); // 10 AM | |
ZonedDateTime utcTime = ZonedDateTime | |
.now(ZoneId.of("Asia/Kolkata")) // current date/time in India | |
.with(localTime) // set time to 10 AM | |
.withZoneSameInstant(ZoneOffset.UTC); | |
System.out.println("Date and time in UTC " + utcTime); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment