在Java 8之前,使用对应的日期和时间API是比较麻烦的,Java 8中引入了全新的日期和时间API解决这一问题。本文主要介绍LocalDate、LocalTime、LocalDateTime、Instance、Duration以及Period的使用。
LocalDate
LocalDate该类的实例是一个不可变对象,它只提供了简单的日期,并不含当天的时间信息。另外,它也不附带任何与时区相关的信息。
package chapter12;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoField;
/**
* @author huangyichun
* @date 2018/12/30
*/
public class LocalDateTest {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2018, 12, 30);
int year = date.getYear();
System.out.println("年份 year=" + year);
Month month = date.getMonth();
System.out.println("月份 month=" + month);
int day = date.getDayOfMonth();
System.out.println("日期几 day=" + day);
DayOfWeek dow = date.getDayOfWeek();
System.out.println("星期 dow=" + dow);
int len = date.lengthOfMonth();
System.out.println("本月天数 len=" + len);
boolean leap = date.isLeapYear();
System.out.println("是否是闰年 leap=" + leap);
/**
* 获取当前时间
*/
LocalDate today = LocalDate.now();
System.out.println("当前时间:" + today);
/**
* 使用Parse方法解析一个DateTimeFormatter
*/
LocalDate localDate = LocalDate.parse("2019-01-01");
System.out.println(localDate);
/**
* 传递TemporalField参数读取时间
*/
int year2 = date.get(ChronoField.YEAR);
System.out.println("年份 year2=" + year2);
int month2 = date.get(ChronoField.MONTH_OF_YEAR);
System.out.println("月份 month2=" + month2);
int day2 = date.get(ChronoField.DAY_OF_MONTH);
System.out.println("日期几 day2=" + day2);
}
}
LocalTime
LocalTime该类的实例是一个不可变对象,用于获取一天中的时间。使用方法和LocalDate类似,直接上代码
package chapter12;
import java.time.LocalTime;
/**
* 该类的使用方法和LocalDate类似
* @author huangyichun
* @date 2018/12/30
*/
public class LocalTimeTest {
public static void main(String[] args) {
LocalTime localTime = LocalTime.of(18, 25, 59);
int hour = localTime.getHour();
int minute = localTime.getMinute();
int second = localTime.getSecond();
System.out.println("hour=" + hour + ", minute=" + minute + ", second=" + second);
/**
* 使用Parse方法解析一个DateTimeFormatter
*/
LocalTime time = LocalTime.parse("18:35:29");
System.out.println(time);
}
}
LocalDateTime
该类是LocalDate和LocalTime的合体,它能同时表示日期和时间,当同样不带有时区信息。有多种构造函数,可以通过合并LocalDate和LocalTime对象构造。具体代码如下:
package chapter12;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
/**
* @author huangyichun
* @date 2018/12/30
*/
public class LocalDateTimeTest {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.of(2018, 12, 30, 19, 3, 20);
System.out.println(localDateTime);
LocalDate date = LocalDate.of(2018, 12, 30);
LocalTime time = LocalTime.of(19, 7, 20);
LocalDateTime localDateTime1 = LocalDateTime.of(date, time);
System.out.println(localDateTime1);
LocalDate date1 = localDateTime1.toLocalDate();
LocalTime time1 = localDateTime1.toLocalTime();
System.out.println(date1 + " " + time1);
}
}
Instance 和 Duration
该类设计目的与LocalDateTime不同,主要是便于机器处理时间,例如计算一个程序的运行时间长度。因此结合Duration类,进行讲解。Duration类主要用于以秒和纳秒衡量时间的长短。具体使用如下代码:
package chapter12;
import java.time.Duration;
import java.time.Instant;
import java.util.stream.IntStream;
/**
* @author huangyichun
* @date 2018/12/30
*/
public class InstantTest {
public static void main(String[] args) {
/**
* 计算0 加到1000000和的时间
*/
Instant start = Instant.now();
int sum = IntStream.rangeClosed(0, 1000000).sum();
System.out.println(sum);
Duration duration = Duration.between(start, Instant.now());
System.out.println(duration.toMillis());
}
}
Period
Period类的功能与Instance类似,不过它计算时间差是以年、月或者日的方式。常常和LocalDate结合使用,符合人类的计算时间方式。具体示例如下:
package chapter12;
import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;
/**
* @author huangyichun
* @date 2018/12/30
*/
public class PeriodTest {
public static void main(String[] args) {
Period period = Period.between(LocalDate.of(2017, 12, 1),
LocalDate.of(2018, 3, 20));
System.out.println(period.get(ChronoUnit.MONTHS));
}
}