Created
October 11, 2024 09:16
-
-
Save huangsijun17/fdea5dc79363a1db56dbd1fe0bd78e37 to your computer and use it in GitHub Desktop.
Get UTC time, local time and time zone through java
This file contains hidden or 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
/** | |
* Filename: TimeDemo.java | |
* For Java8: | |
javac TimeDemo.java | |
java TimeDemo | |
* For Java11+: | |
java TimeDemo.java | |
**/ | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.TimeZone; | |
public class TimeDemo { | |
public static void main(String[] args) { | |
Date now = new Date(); | |
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); | |
formatter.setTimeZone(TimeZone.getTimeZone("UTC")); | |
System.out.println("UTC Time: " + formatter.format(now)); | |
formatter.setTimeZone(TimeZone.getDefault()); | |
System.out.println("Local Time: " + formatter.format(now)); | |
System.out.println("Time Zone: " + TimeZone.getDefault().getID()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment