JAVA get UTC time

In the Java language, you can use the java.util.Calendar class to obtain a local time or a time instance in a specified time zone.

java.util.Calendar cal = java.util.Calendar.getInstance();//Get local time

//java.util.Calendar cal = java.util.Calendar.getInstance(java.util.Locale.CHINA);//Specify the time zone

 

int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);//Get time offset
int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);//Get the daylight saving time difference
cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));//Deduct the time difference from the local time

 

//The time obtained is the UTC standard time.
System.out.println("UTC:"+new Date(cal.getTimeInMillis()));

 

SimpleDateFormat instances, by default, also use the local time zone.

SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss"); dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));

dateFormatGmt.format(...)

 

Example:

System.out.println("UTC: " +cal.getTime()); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("default: " + format.format(cal.getTime())); format.setTimeZone(TimeZone.getTimeZone("GMT")); System.out.println("GMT: " + format.format(cal.getTime())); format.setTimeZone(TimeZone.getTimeZone("GMT-8")); System.out.println("GMT-8: " + format.format(cal.getTime()));

output:

UTC: Wed May 03 05:18:34 CST 2017

default: 2017-05-03 05:18:34

GMT: 2017-05-02 21:18:34

 

GMT-8: 2017-05-02 13:18:34

 

Note: milliseconds are not affected by time zone

 

 

JAVA 8:

The Java 8 date/time API is an implementation of JSR-310 , and its implementation goal is to overcome all the flaws in the old date/time implementation. Some design principles of the new date/time API are:

 

  1. Immutability: In the new date/time API, all classes are immutable, which is good for multithreaded environments.
  2. Separation of Concerns: The new API clearly separates human-readable datetime from machine time (unix timestamp), which is Date, Time, DateTime, unix timestamp, and timezone Different classes are defined.
  3. Clarity: In all classes, methods are explicitly defined to accomplish the same behavior. For example, to get the current instance we can use the now() method, which defines format() and parse() methods in all classes, instead of having a separate class as before. In order to better handle the problem, all classes use the factory pattern and strategy pattern , once you use the method of one of the classes, it is not difficult to work with other classes.
  4. Practical operations: All new date/time API classes implement a set of methods for common tasks such as: adding, subtracting, formatting, parsing, extracting individual parts from a date/time, etc.
  5. Extensibility: The new date/time API works on the ISO-8601 calendar system, but we can also apply it to non-IOS calendars.

Java Date/Time API Package

 1, java.time package: This is the basic package of the new Java Date/Time API, all the main basic classes are part of this package, such as: LocalDate, LocalTime, LocalDateTime, Instant, Period, Duration and so on. All of these classes are immutable and thread-safe, and in the vast majority of cases, these classes can efficiently handle some common requirements.

   eg  LocalDate today = LocalDate.now();// current date

           LocalDate.of(2017, Month.JANUARY, 1);// create date by providing input

LocalDate.now(ZoneId.of("Asia/Kolkata"));// current date in timezone Asia/Kolkata

 

 java.time.LocalTime: LocalTime is an immutable class whose instances represent a time in human-readable format, the default format is hh:mm:ss.zzz.

   eg   LocalTime.now ();

LocalTime.of(12,20,25,40);// create time by providing input

 java.time.LocalDateTime: LocalDateTime is an immutable date-time object that represents a set of date-times, the default format is yyyy-MM-dd-HH-mm-ss.zzz. It provides a factory method that accepts LocalDate and LocalTime input parameters and creates a LocalDateTime instance.

   eg   LocalDateTime.now ();

LocalDateTime.of(LocalDate.now(), LocalTime.now());

 

  java.time.Instant: The Instant class is used in a machine-readable time format , which stores datetimes in the form of Unix timestamps.

eg Instant.now ();

Instant.ofEpochMilli(Instant.now().toEpochMilli());

java.time.chrono package: This package defines some generalized APIs for non-ISO calendar systems. We can extend the AbstractChronology class to create our own calendar system.

java.time.format package: This package contains classes capable of formatting and parsing datetime objects, in the vast majority of cases, we should not use them directly, because the corresponding classes in the java.time package already provide formatting and parsing method.

java.time.temporal package: This package contains some temporal objects that we can use to find out a specific date or time about a date/time object, for example, to find the first or last day of a month. You can easily recognize these methods because they all have the format "withXXX".

 

java.time.zone package: This package contains classes that support different time zones and related rules

 

//utc时间
e.g. ZonedDateTime.now(ZoneOffset.UTC);

Reference: http://www.importnew.com/14140.html

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326462081&siteId=291194637