概述
Date类增强
8.1 Clock
8.2 LocalTime
8.3 LocalDate
8.4 LocalDatetime
实例:
//获取当前时间对应的毫秒数
原来:System.currentTimeMillis()
现在:clock.millis()
//创建LocalDate
LocalDate localDate1 = LocalDate.now();
LocalDate localDate2 = LocalDate.of(1989, Month.APRIL, 10);
//获取年月日星期几
localDate1.getYear()
localDate1.get(ChronoField.YEAR);
localDate1.getMonth();
localDate1.get(ChronoField.MONTH_OF_YEAR);
localDate1.getDayOfMonth();
localDate1.get(ChronoField.DAY_OF_MONTH);
localDate1.getDayOfWeek();
localDate1.get(ChronoField.DAY_OF_WEEK);
LocalTime localTime1 = LocalTime.now();
LocalTime localTime2 = LocalTime.of(12, 23, 34);
localTime1.getHour();
localTime1.get(ChronoField.HOUR_OF_DAY);
localTime1.getMinute();
localTime1.get(ChronoField.MINUTE_OF_HOUR);
localTime1.getSecond();
localTime1.get(ChronoField.SECOND_OF_MINUTE);
LocalDateTime localDateTime2 = LocalDateTime.of(1989, Month.APRIL, 10, 15, 34, 45);
/**
* 以下为LocalDate、LocalTime、LocalDateTime的相互转换,我感觉大有用处
*/
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime3 = LocalDateTime.of(localDate,localTime);
LocalDateTime localDateTime4 = localDate.atTime(localTime);
LocalDateTime localDateTime5 = localTime.atDate(localDate);
LocalDate localDate1 = localDateTime2.toLocalDate();
String s1 = localDate1.format(DateTimeFormatter.BASIC_ISO_DATE);
LocalDate localDate2 = LocalDate.parse(s1, DateTimeFormatter.BASIC_ISO_DATE);
String s2 = localDate1.format(DateTimeFormatter.ISO_LOCAL_DATE);
LocalDate localDate3 = LocalDate.parse(s2, DateTimeFormatter.ISO_LOCAL_DATE);
String s3 = localDate1.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
LocalDate localDate4 = LocalDate.parse(s3, DateTimeFormatter.ofPattern("dd/MM/yyyy"));
LocalTime localTime1 = localDateTime2.toLocalTime();
LocalDateTime localDateTime1 = LocalDateTime.now();
localDateTime1 = localDateTime1.plusYears(1);
localDateTime1 = localDateTime1.plus(1, ChronoUnit.YEARS);
localDateTime1 = localDateTime1.minusMonths(1);
localDateTime1 = localDateTime1.minus(1,ChronoUnit.MONTHS);
localDateTime1 = localDateTime1.withYear(2023);
localDateTime1 = localDateTime1.with(ChronoField.YEAR, 2025);
//获取充值次日零点
public static String getNextDay(String feeTime) {
StringBuffer nextDay = new StringBuffer();
LocalDate localDate = LocalDate.parse(feeTime,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
LocalDate plusLocalDate = localDate.plusDays(1);
nextDay.append(plusLocalDate).append(" 00:00:00");
return nextDay.toString();
}
//判断当前时间是否在充值次日零点后
public static boolean isAfter(String feeNextDay,String now) {
boolean isAfter = false;
LocalDateTime feeNextDayParse = LocalDateTime.parse(feeNextDay, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
LocalDateTime nowParse = LocalDateTime.parse(now, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
isAfter = nowParse.isAfter(feeNextDayParse);
return isAfter;
}
//判断是否是同一天
public static boolean isSameDay(String oneTime,String anotherTime) {
boolean isSame = false;
LocalDate feeNextDayParse = LocalDate.parse(oneTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
LocalDate nowParse = LocalDate.parse(anotherTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
isSame = feeNextDayParse.isEqual(nowParse);
return isSame;
}
//获取两个日期相差多少天
public static long getDayDiff(String fromDay,String endDay) {
long dayDiff = 0L;
LocalDate fromDayParse = LocalDate.parse(fromDay, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
LocalDate endDayParse = LocalDate.parse(endDay, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
// dayDiff = ChronoUnit.DAYS.between(fromDayParse, endDayParse);
dayDiff = endDayParse.toEpochDay() - fromDayParse.toEpochDay();
return dayDiff;
}
最后
以上就是直率马里奥为你收集整理的java8新特性-Date的全部内容,希望文章能够帮你解决java8新特性-Date所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复