Java8中如何操作时间
2020-08-07 本文已影响0人
AbstractCulture
日期
日期,即为yyyy-MM-dd
的格式打印的,不含有时间。
当前日期
// 当前日期 yyyy-MM-dd
System.out.println("当前日期: "+LocalDate.now());
now
获取一年的第几天
// 一年中的第几天
LocalDate localDate = LocalDate.ofYearDay(2020, 365);
System.out.println("2020年的第365天: "+localDate);
365
日期工厂
//日期工厂,构造2020-7-30
LocalDate date = LocalDate.of(2020, 7, 30);
System.out.println("日期工厂: "+date);
LocalDate
分割
LocalDate now = LocalDate.now();
int year = now.getYear();
Month month = now.getMonth();
int dayOfYear = now.getDayOfYear();
int dayOfMonth = now.getDayOfMonth();
DayOfWeek dayOfWeek = now.getDayOfWeek();
System.out.printf("当前年份:%d,当前月份:%s,今天是今年的第%d天,今天是这个月的第%d天,今天是%s",year,month,dayOfYear,dayOfMonth,dayOfWeek);
// 还提供了一个通用的get方法,通过传入枚举成员获取你所需要的信息
System.out.println(today.get(ChronoField.DAY_OF_MONTH));
System.out.println(today.get(ChronoField.DAY_OF_YEAR));
System.out.println(today.get(ChronoField.DAY_OF_WEEK));
分割
判断日期是否相等
LocalDate today = LocalDate.now();
LocalDate yesterday = LocalDate.of(2020, 7, 29);
System.out.println("判断日期相等:"+Objects.equals(today,yesterday));
equals
时间
获取一天的开始时间和结束时间
这里还使用了日期格式化的方法
// 开始时间
LocalDateTime startOfDay = today.atStartOfDay();
// 结束时间
LocalDateTime endOfDay = today.atTime(LocalTime.MAX);
// DATE_FORMATTER= "yyyy-MM-dd HH:mm:ss"
System.out.println("今天的开始时间:"+startOfDay.format(DateTimeFormatter.ofPattern(DATE_FORMATTER)));
System.out.println("今天的结束时间:"+endOfDay.format(DateTimeFormatter.ofPattern(DATE_FORMATTER)));
today
获取一个月的开始时间和结束时间
LocalDate firstDate = today.with(TemporalAdjusters.firstDayOfMonth());
LocalDate endDate = today.with(TemporalAdjusters.lastDayOfMonth());
System.out.println("这个月的开始日期:"+firstDate+"\n这个月的结束日期:"+endDate);
todayFirstAndEnd
获取一个月有多少天
int length = endDate.lengthOfMonth();
System.out.println("当前天数:"+length);
length
日期计算:Duration、Period
Duration支持秒和纳秒级别的计算,所以要求传入的参数必须是时间.
LocalDate plus = firstDate.plus(1, ChronoUnit.WEEKS);
System.out.println("一周后:"+plus);
LocalDate plusMonths = firstDate.plus(1, ChronoUnit.MONTHS);
System.out.println("一个月后:"+ plusMonths);
LocalDate plusDays = firstDate.plus(5, ChronoUnit.DAYS);
System.out.println("5天后:"+plusDays);
LocalDate lastWeek = firstDate.plus(-1, ChronoUnit.WEEKS);
System.out.println("一周前:"+lastWeek);
Plus
Duration duration = Duration.between(firstDate.atTime(LocalTime.MIN), plusDays.atTime(LocalTime.MIN));
System.out.println("日期差:"+duration.getSeconds()/3600/24);
duration
Period是做日期计算的,放入日期即可
Period period = Period.between(firstDate, plusDays);
System.out.println("日期差:"+period.getDays());
Period
枚举
long subResult = ChronoUnit.DAYS.between(firstDate, plusDays);
System.out.println("ChronoUnit日期差:"+subResult);
ChronoUnit
时间戳
// 获取时间戳,与北京时间相差8个小时
Instant instant = Instant.now();
System.out.println(instant);
Instant beiJingInstant = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8));
System.out.println(beiJingInstant);
// 计算差值
Duration between = Duration.between(instant, beiJingInstant);
System.out.println(between.toHours());
instant
LocalDate、LocalDateTime与Date的转化
核心思想: 将LocalDate、LocalDateTime转化成时间戳,然后使用Date.from()进行转化.
/**
* 把localDate转化成Date
* @param localDate
* @return
*/
public static Date getDateFromLocalDate(LocalDate localDate){
return Date.from(localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
}
/**
* 把localDateTime转化成Date
* @param localDateTime
* @return
*/
public static Date getDateFromLocalDateTime(LocalDateTime localDateTime){
return Date.from(localDateTime.atZone(ZoneOffset.ofHours(8)).toInstant());
}
Date dateFromLocalDate = LocalDateUtils.getDateFromLocalDate(endDate);
System.out.println(dateFromLocalDate);
Date dateFromLocalDateTime = LocalDateUtils.getDateFromLocalDateTime(firstTime);
System.out.println(dateFromLocalDateTime);
转化
LocalDate、LocalDateTime与时间戳的转化
long date = System.currentTimeMillis();
System.out.println(date);
LocalDate localDate = Instant.ofEpochMilli(date).atZone(ZoneId.systemDefault()).toLocalDate();
LocalDateTime localDateTime = Instant.ofEpochMilli(date).atZone(ZoneId.systemDefault()).toLocalDateTime();
System.out.println(localDate);
System.out.println(localDateTime);
LocalDateTime now = LocalDateTime.now();
long nowInstant = now.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());