当前位置: 首页 > news >正文

广州b2b网站建设公司天津seo结算

广州b2b网站建设公司,天津seo结算,网页前端设计用什么软件,汉中建设工程招标网官网开发中常常需要用到时间,随着jdk的发展,对于时间的操作已经摒弃了之前的Date等方法,而是采用了LocalDateTime方法,因为LocalDateTime是线程安全的。 下面我们来介绍一下LocalDateTime的使用。 时间转换 将字符串转换为时间格式…

开发中常常需要用到时间,随着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()));
}

运行结果:

http://www.khdw.cn/news/27207.html

相关文章:

  • 枣庄联通网站备案5118素材网站
  • 中国做的手机系统下载网站如何在互联网上做推广
  • 虚拟机安装 wordpress湖州网站seo
  • 垃圾网站怎么做的广州百度竞价开户
  • 沈阳做网站的北京seo推广服务
  • 网站建设与维护 东博东莞网站seo公司
  • 网站栏目页优化企业网搭建
  • 温州网站制作百度搜索量最大的关键词
  • 重庆微信网站开发公关键词搜索点击软件
  • 沈阳网站建设招标公司制作网站用什么软件
  • 网站建设中界面模板网站建设总结
  • 网站制作目的百度seo详解
  • 建设银行的网站查询密码深圳网站优化哪家好
  • 做网站单线程CPU和多线程cpuseo在线培训
  • 宿州市住房建设委员会网站开发网站的流程
  • 如何查询网站的访问量无锡网站建设
  • 网站需要每个城市做推广吗新手怎么做网络推广
  • 甘肃网站建设费用淘宝seo软件
  • 做片子 我们是认真的网站seo优化 搜 盈seo公司
  • 泗水县建设局的网站怎么打开谷歌搜索优化seo
  • 设计师网站家装torrentkitty磁力搜索引擎
  • 旅游公司网站建设成都黑帽seo
  • 打开web浏览器seo怎么做最佳
  • 做app网站需要什么技术支持教育培训网站官网
  • wordpress漂浮表单泰州网站排名seo
  • 武汉网站建设组织小广告设计
  • 企业网站建设的作用乐云seo
  • 咸鱼app引导页面设计模板seo是搜索引擎吗
  • 框架做网站指的是seo关键词有话要多少钱
  • 国外网站国内做好还是国外做windows优化大师卸载