LocalDateTime基本使用(包括Data转化)

时间初始化

//获取当前时间
LocalDateTime nowTime= LocalDateTime.now();
System.out.println("现在时间"+nowTime);
//自定义时间 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);

时间比较

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

 时间获取

// 获取当前时间
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+"天");

时间操作

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

时间做差

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

Period时间差 

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

获得特殊时间

//根据需求需要取得当天的零点
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);

LocalDataTime转化

//LocalDataTIme的转化
String localDataTimeString= nowTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
LocalDateTime StringLocalDataTime = LocalDateTime.parse("2017-09-28 17:07:05",DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));//String转化为LocalDataTime
System.out.println("LocalDateTime转成String类型的时间:"+localDataTimeString);
System.out.println("String类型的时间转成LocalDateTime:"+StringLocalDataTime);

Data转化

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

### 回答1: LocalDateTime和Date之间的转换可以通过以下方式实现: 1. LocalDateTime转Date: ``` LocalDateTime localDateTime = LocalDateTime.now(); Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); ``` 2. Date转LocalDateTime: ``` Date date = new Date(); LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); ``` ### 回答2: LocalDateTime和Date的转化可以通过以下方法进行: 1. LocalDateTime转Date:可以使用java.util.Date类中的from()方法,将LocalDateTime对象转化为Date对象。具体步骤如下: ```java LocalDateTime localDateTime = LocalDateTime.now(); Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); ``` 2. Date转LocalDateTime:可以使用java.util.Date类中的toInstant()方法,将Date对象转化为Instant对象,然后再通过Instant对象的atZone()方法指定时区,得到对应的ZonedDateTime对象,最后使用ZonedDateTime对象的toLocalDateTime()方法得到LocalDateTime对象。具体步骤如下: ```java Date date = new Date(); LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); ``` 需要注意的是,LocalDateTime是Java 8引入的新的日期时间类,精确到纳秒级别,而Date是Java早期版本提供的日期时间类,精确到毫秒级别。转化过程中,可能会有精度的损失。同时,LocalDateTime类与时区无关,而Date默认使用系统默认时区。 另外,推荐在使用时尽可能使用新的Java 8日期时间类(LocalDateTimeLocalDate、LocalTime等),因为它们更加灵活,提供了更多的方法和功能,并且在并发环境下也更加安全。如果必须要使用旧的Date类,可以在需要时进行转化。 ### 回答3: LocalDateTime 是 Java 8 中的一个日期时间类,用于表示不带时区信息的日期时间。而 Date 是 Java 中的旧日期时间类,用于表示有时区信息的日期时间。本文将介绍 LocalDateTime 和 Date 之间的转化。 要将 LocalDateTime 转换为 Date,可以使用以下代码: ``` LocalDateTime localDateTime = LocalDateTime.now(); ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault()); Date date = Date.from(zonedDateTime.toInstant()); ``` 首先,通过 LocalDateTime 的 now() 方法获取当前的本地日期时间。然后,使用 atZone() 方法将 LocalDateTime 对象与系统默认时区关联起来,构建一个 ZonedDateTime 对象。最后,通过 toInstant() 方法将 ZonedDateTime 转换为 Instant 对象,并使用 Date 的 from() 方法将 Instant 转换为 Date 对象。 要将 Date 转换为 LocalDateTime,可以使用以下代码: ``` Date date = new Date(); Instant instant = date.toInstant(); LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); ``` 首先,创建一个 Date 对象。然后,使用 toInstant() 方法将 Date 转换为 Instant 对象。最后,使用 LocalDateTime 的 ofInstant() 方法将 Instant 转换为 LocalDateTime 对象,需要传入 Instant 对象和系统默认时区。 通过这些转化方式,我们可以在 LocalDateTime 和 Date 之间进行转化,以便在不同的日期时间处理场景中使用
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值