介绍
鉴于之前的java的时间工具类很不好用,java8给出了localDateTime,localDate,localTime这3个工具类用于处理时间。在mybatis中也可以把date转换为localDateTime,这个可以参照我之前的文章。
localDateTime使用例子
import java.time.*;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.*;
public class TimeUtilsDemo {
public void localDateDemo() {
//LocalDate获取当前日期
LocalDate localDateNow = LocalDate.now();
//设置日期
LocalDate localDate = LocalDate.of(2021, 5, 1);
//获取年
System.out.println(localDate.getYear());
System.out.println(localDate.get(ChronoField.YEAR));
//获取月
System.out.println(localDate.getMonth());
System.out.println(localDate.get(ChronoField.MONTH_OF_YEAR));
//获取日
System.out.println(localDate.getDayOfMonth());
System.out.println(localDate.get(ChronoField.DAY_OF_MONTH));
//获取星期
System.out.println(localDate.getDayOfWeek());
System.out.println(localDate.get(ChronoField.DAY_OF_WEEK));
}
public void localTimeDemo() {
//LocalTime获取当前时间
LocalTime localTimeNow = LocalTime.now();
//设置时间
LocalTime localTime = LocalTime.of(16, 22, 33);
//获取小时
System.out.println(localTime.getHour());
System.out.println(localTime.get(ChronoField.HOUR_OF_DAY));
//获取分
System.out.println(localTime.getMinute());
System.out.println(localTime.get(ChronoField.MINUTE_OF_HOUR));
//获取秒
System.out.println(localTime.getSecond());
System.out.println(localTime.get(ChronoField.SECOND_OF_MINUTE));
}
public void instantDemo() {
// 创建Instant对象
Instant instant = Instant.now();
// 获取秒
System.out.println(instant.getEpochSecond());
// 获取毫秒
System.out.println(instant.toEpochMilli());
}
public void localDateTimeDemo() {
// 获取当前日期时间
LocalDateTime localDateTime = LocalDateTime.now();
LocalDate localDate = LocalDate.of(2021, 5, 1);
LocalTime localTime = LocalTime.of(16, 22, 33);
// 设置日期
LocalDateTime localDateTime1 = LocalDateTime.of(2021, Month.SEPTEMBER, 11, 11, 33, 22);
System.out.println(localDateTime1);
LocalDateTime localDateTime2 = LocalDateTime.of(localDate, localTime);
System.out.println(localDateTime2);
LocalDateTime localDateTime3 = localDate.atTime(localTime);
System.out.println(localDateTime3);
LocalDateTime localDateTime4 = localTime.atDate(localDate);
System.out.println(localDateTime4);
// 获取LocalDate
LocalDate localDate2 = localDateTime.toLocalDate();
System.out.println(localDate2);
// 获取LocalTime
LocalTime localTime2 = localDateTime.toLocalTime();
System.out.println(localTime2);
//获得今天对于当前年&月&天内的天数
System.out.println(localDateTime1.getDayOfMonth());
System.out.println(localDateTime1.getDayOfWeek());
System.out.println(localDateTime1.getDayOfYear());
//修改时间
//增加一年&月&周&日&小时&分钟&秒&纳秒
//天
localDateTime1.plusDays(1);
//小时
localDateTime1.plusHours(1);
//分钟
localDateTime1.plusMinutes(1);
//月
localDateTime1.plusMonths(1);
//纳秒
localDateTime1.plusNanos(1);
//秒
localDateTime1.plusSeconds(1);
//周
localDateTime1.plusWeeks(1);
//年
localDateTime1.plusYears(1);
System.out.println(localDateTime1.plusYears(1));
System.out.println(localDateTime1.plus(1, ChronoUnit.YEARS));
//减少一年
System.out.println(localDateTime1.minusYears(1));
System.out.println(localDateTime1.minus(1, ChronoUnit.YEARS));
//比较2个时间,是否大于,是否小于,是否相等
System.out.println(localDateTime.isAfter(localDateTime1));
System.out.println(localDateTime.isBefore(localDateTime1));
System.out.println(localDateTime.isEqual(localDateTime1));
//比较2个时间相差的值,并且传入对应的差值条件
System.out.println(localDateTime.until(localDateTime1, ChronoUnit.YEARS));
}
// 01. java.util.Date --> java.time.LocalDateTime
public void DateToLocalDateTime() {
java.util.Date date = new java.util.Date();
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
}
// 02. java.util.Date --> java.time.LocalDate
public void DateToLocalDate() {
java.util.Date date = new java.util.Date();
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
LocalDate localDate = localDateTime.toLocalDate();
}
// 03. java.util.Date --> java.time.LocalTime
public void DateToLocalTime() {
java.util.Date date = new java.util.Date();
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
LocalTime localTime = localDateTime.toLocalTime();
}
// 04. java.time.LocalDateTime --> java.util.Date
public void LocalDateTimeToDate() {
LocalDateTime localDateTime = LocalDateTime.now();
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
java.util.Date date = Date.from(instant);
}
// 05. java.time.LocalDate --> java.util.Date
public void LocalDateToDate() {
LocalDate localDate = LocalDate.now();
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
java.util.Date date = Date.from(instant);
}
// 06. java.time.LocalTime --> java.util.Date
public void LocalTimeToDate() {
LocalTime localTime = LocalTime.now();
LocalDate localDate = LocalDate.now();
LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
java.util.Date date = Date.from(instant);
}
}