来自JDK8.0新增java.time.Xxx
不变类型,日期与时间区分,有利高效调用
LocalDate LocalTime LocalDateTime
(日期 时间 日期时间)
方法名(创建对象) | 说明 |
public static Xxxx now(); | 静态方法,根据当前时间创建对象 |
public static Xxxx of(…); | 静态方法,指定日期/时间创建对象 |
LocaDate localDate = LocalDate.now();
LocalTime llocalTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
LocalDate localDate = LocalDate.of(2099,11,11);
LocalTime localTime = LocalTime.of(11, 11, 11);
LocalDateTime localDateTime = LocalDateTime.of(2020, 1, 2, 3, 4, 5);
方法 | 说明 |
public int geYear() | 获取年 |
public int getMonthValue() | 获取月份(1-12) |
Public int getDayOfMonth() | 获取月中第几天乘法 |
Public int getDayOfYear() | 获取年中第几天 |
Public DayOfWeek getDayOfWeek() | 获取星期 |
LocalDateTime
(日期时间)转换(日期时间)
方法(创建对象) | 说明 |
public LocalDate toLocalDate() | 转换成一个LocalDate对象 |
public LocalTime toLocalTime() | 转换成一个LocalTime对象 |
方法 | 说明 |
以LocalDate为例 | |
plusDays plusWeeks plusMonths plusYears | 向当前 LocalDate 对象添加 几天、几周、几月、几年 |
minusDays minusWeeks minusMonths minusYears | 从当前 LocalDate 对象减去 几天、 几周、几月、几年 |
withDayOfMonth withDayOfYear withMonth withYear | 将月份天数,年份天数,月份,年份修改为指定的值并返回新的LocalDate对象 |
isBefore isAfter | 比较两个 LocalDate |
Instant(时间戳)
方法(创建对象) | 说明 |
Instant instant =Instant.now(); | 获得时间戳对象 |
Date date = Date.from(instant); | 转换时间戳为日期对象 |
Instant = date.toInstant(); | 转换日期为时间戳对象 |
DateTimeFormatter
(日期时间格式器)
方法(创建对象) | 说明 |
LocalDateTime l = LocalDateTime.now(); | 获得日期时间对象 |
DateTimeFormatter d = DateTimeFormatter.ofPattern ("yyyy-MM-dd HH:mm:ss"); | 定义转换格式 |
String s = l.format(d); | 正反都可以用format |
String s = d.format(l); |
Period(日期间隔)
方法 | 说明 |
getYears()getMonths()getDays() | 只能精确到年 月 日 用于 LocalDate 间比较 |
LocalDate today = LocalDate.now();//获得当前日期对象
LocalDate birthDate = LocalDate.of(1999,1,3);//获得指定日期对象
Period period = Period.between(birthDate, today);//后值减前值
System.out.printf("年龄:%d年%d月%d日", period.getYears(),
period.getMonths(), period.getDays());
Duration(时间间隔)
方法(创建对象) | 说明 |
toDays() toHours() toMinutes() toMillis() toNanos() | 两个时间相差的天数,小时数,分钟数, 毫秒数,纳秒数用于LocalDateTime间比较 |
Duration duration = Duration.between(birthDate, today);//后值减前值
ChronoUnit(全时间比较器)
LocalDateTime today = LocalDateTime.now();//获得此刻时间
LocalDateTime birthDate = LocalDateTime.of(1999,1,3,05,20,009);//获得传入时刻
System.out.println("相差的年数:" + ChronoUnit.YEARS.between
(birthDate, today));
System.out.println("相差的月数:" + ChronoUnit.MONTHS.between
(birthDate, today));
System.out.println("相差的周数:" + ChronoUnit.WEEKS.between
(birthDate, today));
System.out.println("相差的天数:" + ChronoUnit.DAYS.between
(birthDate, today));
System.out.println("相差的时数:" + ChronoUnit.HOURS.between
(birthDate, today));
System.out.println("相差的分数:" + ChronoUnit.MINUTES.between
(birthDate, today));
System.out.println("相差的秒数:" + ChronoUnit.SECONDS.between
(birthDate, today));
System.out.println("相差的毫秒数:" + ChronoUnit.MILLIS.between
(birthDate, today));
System.out.println("相差的微秒数:" + ChronoUnit.MICROS.between
(birthDate, today));
System.out.println("相差的纳秒数:" + ChronoUnit.NANOS.between
(birthDate, today));
System.out.println("相差的半天数:" + ChronoUnit.HALF_DAYS.between
(birthDate, today));
System.out.println("相差的十年数:" + ChronoUnit.DECADES.between
(birthDate, today));
System.out.println("相差的世纪(百年)数:" + ChronoUnit.CENTURIES.between
(birthDate, today));
System.out.println("相差的千年数:" + ChronoUnit.MILLENNIA.between
(birthDate, today));
System.out.println("相差的纪元数:" + ChronoUnit.ERAS.between
(birthDate, today));