浅析Java中的时间处理:Date.compareTo比较时间、LocalDateTime 的基本使用

一、Date.compareTo比较时间

DateFormat dateFormat = DateFormat.getDateInstance();
Date oldTime = dateFormat.parse("2021-04-07 19:50:11");
Date newTime = new Date();
int result = oldTime.compareTo(newTime);
System.out.println(result);  // -1

  时间相等,结果为 0;旧比新,结果为 -1;新比旧,结果为 1。

二、LocalDateTime 基本介绍

  在需求上有一个有效期,需要当前时间 + 有效期天数,得到过期时间,代码如下,所以顺带学习了一下  LocalDateTime 的基本用法。

Date.from(LocalDateTime.now().plusDays(market.getValidDay()).toInstant(ZoneOffset.of("+8")))

  从名字上可以看出来java.time.LocalDateTime是一个可以表示日期时间的对象,代表日期时间,通常被视为年 - 月 - 日 - 时 - 分 - 秒,也提供了其他日期和时间字段,例如星期。LocalDateTime的时间精度为纳秒精度(1秒=1000毫秒,1毫秒=1000微秒,1微秒=1000纳秒)。

  打开jdk8的api文档,找到java.time.LocalDateTime,分别了解它的构造方法、静态方法及对象方法等 。

  java.time.LocalDateTime没有公开的构造方法,需要通过静态方法创建java.time.LocalDateTime对象。

 1、时区  ——  根据时区创建日期时间对象

  通过时区的 id 可确定一个时区,我们常用时区:

Asia/Shanghai:亚洲上海

America/Los_Angeles:美国/洛杉矶

  基于时区创建LocalDateTime的方法

//基于默认时区创建
LocalDateTime now = LocalDateTime.now();
// 基于亚洲/上海(东八区)
LocalDateTime localDateTime = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));

  ZoneId.of(“Asia/Shanghai”)表示得到一个时区ID(对象),也可以写为如下方式:

//基于亚洲/上海(东八区)
ZoneId zoneId = ZoneId.of("Asia/Shanghai");
LocalDateTime localDateTime = LocalDateTime.now(zoneId);

三、LocalDateTime 基本使用

1、时间初始化

//获取当前时间
LocalDateTime nowTime= LocalDateTime.now();

//自定义时间 of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)
LocalDateTime endTime = LocalDateTime.of(2020, 5, 20, 5, 20, 10,00);
System.out.println("设定时间"+endTime);

2、时间比较

//比较   现在的时间 比 设定的时间 之前  返回的类型是Boolean类型
boolean isBefore = nowTime.isBefore(endTime);
//比较 现在的时间 和 设定的时候 相等 返回类型是Boolean类型 boolean euqal =nowTime.equals(endTime);
//比较 现在的时间 比 设定的时间 之后 返回的类型是Boolean类型 boolean isAfter = nowTime.isAfter(endTime);

3、时间获取

// 获取当前时间
int year = nowTime.getYear();
int monthValue = nowTime.getMonthValue();
int dayofMonth = nowTime.getDayOfMonth();
int hour = nowTime.getHour();
int minute = nowTime.getMinute();
int second = nowTime.getSecond();
System.out.println("获得时间:" + year + "" +monthValue+""+dayofMonth+"" + hour+""+minute+""+second+"");// 打印当前时间的
DayOfWeek dayofWeek = nowTime.getDayOfWeek();//获取星期几
System.out.println("获得星期:"+dayofWeek);
int dayofYear = nowTime.getDayOfYear();//获取当前日子为年的第几天
System.out.println("全年:"+dayofYear+"");

4、时间操作

// 获取一年之前.minusYears(1) 这个 1 代表一年之前,如果是2就是两年之前
LocalDateTime minusYears = nowTime.minusYears(1);
LocalDateTime plusYears = nowTime.plusYears(1);//一年之后
LocalDateTime minusMonths = nowTime.minusMonths(1);//一个月前
LocalDateTime plusMonths = nowTime.plusMonths(1);//一个月后
LocalDateTime BeforeWeek = nowTime.minusWeeks(1);//一个星期前
LocalDateTime AfterWeek = nowTime.plusWeeks(1);//一个星期后
LocalDateTime minusDays = nowTime.minusDays(1);//一天前
LocalDateTime plusDays = nowTime.plusDays(1);//一天后
LocalDateTime minusHours = nowTime.minusHours(1);//一个小时前
LocalDateTime plusHours = nowTime.plusHours(1);//一个小时后
LocalDateTime minusMinutes = nowTime.minusMinutes(1);//一分钟前
LocalDateTime plusMinutes = nowTime.plusMinutes(1);//一分钟后
LocalDateTime minusSeconds = nowTime.minusSeconds(1);//一秒前
LocalDateTime plusSeconds = nowTime.plusSeconds(1);//一秒后
LocalDateTime minusTime = nowTime.minus(23, ChronoUnit.MONTHS);//当前时间减少23个月

5、时间做差

//计算时间差
Duration duration = Duration.between(nowTime,endTime);//时间差
long durationdays = duration.toDays(); //相差的天数long durationhours = duration.toHours();//相差的小时数long durationminutes = duration.toMinutes();//相差的分钟数long durationmillis = duration.toMillis();//相差毫秒数long durationnanos = duration.toNanos();//相差的纳秒数

6、Period时间差

Period period = Period.between(nowTime.toLocalDate(),endTime.toLocalDate());
int periodyear = period.getYears();int periodmonth = period.getMonths();
System.out.println("period相差"+periodmonth+"");
int perioddays= period.getDays();

7、获得特殊时间

//根据需求需要取得当天的零点
LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);//当天零点  
System.out.println(today_start);
LocalDateTime today_end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);//获取当天结束时间
System.out.println(today_end);

8、LocalDataTime转化

//LocalDataTIme转String
String localDataTimeString= nowTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//String转化为LocalDataTime LocalDateTime StringLocalDataTime
= LocalDateTime.parse("2017-09-28 17:07:05",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

9、Data转化

//Date的转化
Date date = Date.from(nowTime.atZone(ZoneId.systemDefault()).toInstant());
//转化为LocalDataTime
LocalDateTime dateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
//转为为String
String dataString
= nowTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

 

posted @ 2021-08-09 23:06  古兰精  阅读(4180)  评论(0编辑  收藏  举报