Skip to content

Instantly share code, notes, and snippets.

@huangsijun17
Created October 11, 2024 09:16
Show Gist options
  • Save huangsijun17/fdea5dc79363a1db56dbd1fe0bd78e37 to your computer and use it in GitHub Desktop.
Save huangsijun17/fdea5dc79363a1db56dbd1fe0bd78e37 to your computer and use it in GitHub Desktop.
Get UTC time, local time and time zone through java
/**
* 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