广州b2b网站建设公司天津seo结算
开发中常常需要用到时间,随着jdk的发展,对于时间的操作已经摒弃了之前的Date等方法,而是采用了LocalDateTime方法,因为LocalDateTime是线程安全的。
下面我们来介绍一下LocalDateTime的使用。
时间转换
将字符串转换为时间格式
前后端交互的过程中,前端传的是字符串类型,那在进行时间比较之前,我们需要将字符串转为我们的时间格式。
其中DateTimeFormatter是在一种时间格式设置的工具
//将字符串转换为时间格式
@Test
public void testToDate(){//时间格式设置DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm");//将字符串转换为时间LocalDateTime localDateTime = LocalDateTime.parse("20230923 23:12", dateTimeFormatter);System.out.println("字符串转换为时间:"+localDateTime);
}
运行结果:
将时间转换为字符串
如何将时间转换为字符串呢?同样地,需要先设置一个转换格式。
//将时间转换为字符串
@Test
public void testToString(){//时间格式设置DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm");//获取当前时间LocalDateTime now = LocalDateTime.now();//将时间转换为字符串String nowString = now.format(dateTimeFormatter);System.out.println("时间转换为字符串:"+nowString);
}
运行结果:
获取当前时间
//@Test
public void testGetNow(){//获取当前时间LocalDateTime now = LocalDateTime.now();System.out.println("当前时间为:"+now);
}
运行结果:
以上就是获取当前的时间,并且输入的格式是ISO 8601的格式。
计算两个时间的差距
Duration是一种计算时间差的一个类,其中有两个入参,开始时间和结束时间。并且在该类中提供了多种方法,满足我们的需求:
//计算两个时间的差距
@Test
public void testTimeBetween(){DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm");LocalDateTime parse = LocalDateTime.parse("20230221 21:16", dateTimeFormatter);//时间差距Duration between = Duration.between(parse,LocalDateTime.now());//将时间差转换为以天为单位long days = between.toDays();//将时间差转为以分钟为单位long minutes = between.toMinutes();//将时间差转为以小时为单位long hours = between.toHours();System.out.println(days);System.out.println(minutes);System.out.println(hours);
}
计算两个日期的差值
当我们在实际的开发中,只涉及到日期的操作,我们也可以使用DateTimeFormatter进行日期转换。获取今天的日期可以使用:
LocalDate now = LocalDate.now();
但是在计算两个日期的差值是不能使用Duration了需要用到Period。但是这个只会计算出天数位的差值,和年、月没有关系。要计算真正的日期差值,可以使用until。
//计算两个日期的差值
@Test
public void testPeriod(){DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");LocalDate parse = LocalDate.parse("20230121", dateTimeFormatter);//时间差距,注意这个只会计算出天数位的查找和月,年没有关系。//Period between = Period.between(parse,LocalDate.now());//两个日期之间相差了多少天//System.out.println(between.getDays());//计算两个日期真正相差多少天long until = parse.until(LocalDate.now(), ChronoUnit.DAYS);System.out.println("相差:"+until);
}
运行结果:
怎样将时间转换为时间戳
我们都知道LocalDateTime是没有带时区的,那我们将它转为时间戳的时候需要带上时区信息。
//将时间转换为时间戳
@Test
public void testToInstant(){LocalDateTime now = LocalDateTime.now();long l = now.toInstant(ZoneOffset.ofHours(8)).toEpochMilli() / 1000;System.out.println("l: " + l);//输出 l: 1677116982Instant instant = now.toInstant(ZoneOffset.ofHours(8));long epochSecond = instant.getEpochSecond();System.out.println("epochSecond:"+epochSecond);//输出 输出 epochSecond: 1677116982//将时间戳转换为时间LocalDateTime dateTime = LocalDateTime.ofEpochSecond(1677116982L, 0 ,ZoneOffset.ofHours(8));System.out.println(dateTime);
}
如上的代码中,toInstant就是将时间转为时间戳的方法,ZoneOffset.ofHours(8)就是时区偏移8小时,就是北京时间。
查看几分钟前的时间
@Test
public void test(){//minutes分钟之前的时间Integer minutes = 3;Instant instant = Instant.now().minus(Duration.ofMinutes(minutes));System.out.println(instant.atZone(ZoneId.systemDefault()));
}
运行结果: