Java 8 – Date Time API
Java 8 comes with a much improved and much required change in the way date and time is handled.
The classes that represent the date and time concepts are present in the java.time package.
The important classes in this package are:
- java.time.Period: This class represents the date part of the datetime. It represents the date part in terms of days, months and years. for example 1 year, 2 months and 5 days
- java.time.Duration: This class represents the time part of the datetime. It represents the time part in terms of seconds and nanoseconds. for example ’29 seconds’
- java.time.Instant: This represents a particular instant in the timeline. It stores the number of seconds through epoch time and also has another field that stores the nanoseconds
- java.time.LocalDate: This stores the date part of date-time in terms of years-months-days. It does not store the TimeZone. The class is immutable.
- java.time.LocalTime: This class stores the time part of date time without any TimeZone info.
- java.time.LocalDateTime: This class stores the LocalDate and LocalTime but no TimeZone
- java.time.ZonedDateTime: This class stores the LocalDateTime and the TimeZone info as a ZoneOffset object. The class has access to ZoneRules which can be used to convert to local time
- java.time.ZoneOffset:This stores the time zone offset from UTC. The zone rules are stored in ZoneId.
- java.time.OffsetDateTime:This stores the local datetime with the offset. This class does not have information about Zone Rules.
1 package java8.datetime; 2 3 import java.time.LocalDate; 4 import java.time.LocalTime; 5 6 /** 7 * Java 8 – Date Time API 8 * LocalTime:当前时间 9 * LocalDate:当前日期 10 */ 11 public class LocalTimeDemo3 { 12 public static void main(String[] args) { 13 LocalTime localTime = LocalTime.now(); 14 System.out.println("当前时间:" + localTime); 15 16 LocalDate localDate = LocalDate.now(); 17 System.out.println("当前日期:" + localDate); 18 } 19 }
LocalDateTime
1 package java8.datetime; 2 3 import java.time.DayOfWeek; 4 import java.time.LocalDateTime; 5 import java.time.ZoneId; 6 import java.time.temporal.ChronoField; 7 8 /** 9 * Java 8 – Date Time API 10 * LocalDateTime 11 * url:http://www.studytrails.com/java/java8/java8_date_and_time/ 12 */ 13 public class LocalDateTimeDemo2 { 14 public static void main(String[] args) { 15 // create a LocalDateTime 16 LocalDateTime localDateTime = LocalDateTime.now(); 17 System.out.println("新建LocalDateTime对象:" + localDateTime); // prints 2018-02-12T15:08:44.116 18 19 // Convert LocalDateTime to datetime in particular zone 20 System.out.println("指定时区:" + localDateTime.atZone(ZoneId.of("America/New_York"))); 21 22 // Get the day of week from DateTime 23 System.out.println("星期几:" + DayOfWeek.from(localDateTime)); // prints SUNDAY. 24 25 // Get the day of year from DateTime 26 System.out.println("当年第几天:" + localDateTime.get(ChronoField.DAY_OF_YEAR)); // prints 43 27 // The other fields that can be returned are MINUTE_OF_HOUR, MINUTE_OF_DAY, HOUR_OF_AMPM, 28 // HOUR_OF_DAY, AMPM_OF_DAY, DAY_OF_WEEK, DAY_OF_MONTH, DAY_OF_YEAR, MONTH_OF_YEAR, 29 // YEAR, OFFSET_SECONDS (offset from UTC). 30 31 // 获得当前日期 32 System.out.println("当前日期:" + localDateTime.toLocalDate()); 33 // prints 2014-09-29 34 35 // 获取当前时间 36 System.out.println("当前时间:" + localDateTime.toLocalTime()); 37 // prints 22:26:30.146 38 39 // Create a LocalDateTime from year, month, day, hour, min 40 System.out.println("实例化LocalDateTime by Int:" + LocalDateTime.of(2014, 10, 1, 10, 0)); 41 // prints 2014-10-01T10:00 42 43 // Create LocalDateTime by parsing a string 44 LocalDateTime parsedLocalDateTime = LocalDateTime.parse("2014-01-01T11:00"); 45 System.out.println("实例化LocalDateTime by String:" + parsedLocalDateTime); 46 47 System.out.println("当前小时点:" + localDateTime.getHour()); 48 49 50 } 51 }
Instant
1 package java8.datetime; 2 3 import java.time.Instant; 4 import java.time.temporal.ChronoUnit; 5 6 /** 7 * Java 8 – Date Time API 8 * Instant 9 * url:http://www.studytrails.com/java/java8/java8_date_and_time/ 10 */ 11 public class InstantDemo1 { 12 public static void main(String[] args) { 13 //Creating a local date 14 Instant now = Instant.now(); 15 //2018-02-12T06:29:01.493Z 16 System.out.println("当前时间:" + now); 17 18 // The epoch seconds is the number of seconds since 1970-01-01T00:00:00Z 19 System.out.println("当前秒数:" + now.getEpochSecond()); 20 21 // 调整时间 22 // the plus function allows adding time intervals. 23 // The time intervals can be NANOS, MICROS, MILLIS, SECONDS, MINUTES, HOURS, HALF_DAYS, DAYS 24 Instant tomorrow = now.plus(2, ChronoUnit.DAYS); 25 System.out.println("明天时间:" + tomorrow); 26 // The minus function allows subtracting time from an instant. 27 Instant yesterday = now.minus(1, ChronoUnit.DAYS); 28 System.out.println("昨天时间:" + yesterday); 29 30 // The compare function can be used to compare two dates. 31 // It returns -1 if the date that is passed is after , 1 if it is before 32 System.out.println("今天和明天比较:" + now.compareTo(tomorrow));// prints -1 33 34 // check if one instant is after another 35 System.out.println("今天是否在昨天后面:" + now.isAfter(yesterday));// prints true 36 37 } 38 }