-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChangeTimezone.java
45 lines (39 loc) · 1.4 KB
/
ChangeTimezone.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.javamultiplex.datetime;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
//How to change timezone in Java?
public class ChangeTimezone {
public static void main(String[] args) throws ParseException {
//Get Current date.
Date date=new Date();
//Get default Time zone.
TimeZone timezone=TimeZone.getDefault();
System.out.println("Time Zone : "+timezone.getID() + " | "+timezone.getDisplayName() );
/*
* d -> Day of month
* M -> Month of year
* Y -> Year
* h -> Hour (1-12)
* m -> minute
* s -> second
* a -> AM/PM
* z -> Time zone
*/
DateFormat format=new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a z");
String dateInString=format.format(date);
System.out.println("Current date in IST : " +dateInString);
timezone=TimeZone.getTimeZone("Europe/London");
System.out.println("Time Zone : "+timezone.getID() + " | "+timezone.getDisplayName() );
format.setTimeZone(timezone);
dateInString=format.format(date);
System.out.println("Current date in GMT : " +dateInString);
timezone=TimeZone.getTimeZone("America/New_York");
System.out.println("Time Zone : "+timezone.getID() + " | "+timezone.getDisplayName() );
format.setTimeZone(timezone);
dateInString=format.format(date);
System.out.println("Current date in EST : " +dateInString);
}
}