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

中国建设网官网住房和城乡建设官网文明seo

中国建设网官网住房和城乡建设官网,文明seo,宜宾做网站公司,推广普通话手抄报句子目录 年初年末月初月末3. 日初日末 记录日常开发中的常用的日期转换代码,算是一篇Java 8时间API使用实操的简短总结文。 下文中,都以LocalDateTime为例,在不声明的情况下如下方法一般都适用于Java8中LocalDate、LocalDateTime、OffsetDateTi…

目录

  • 年初年末
  • 月初月末
  • 3. 日初日末

记录日常开发中的常用的日期转换代码,算是一篇Java 8时间API使用实操的简短总结文。

下文中,都以LocalDateTime为例,在不声明的情况下如下方法一般都适用于Java8中LocalDate、LocalDateTime、OffsetDateTime、ZonedDateTime等时间类,不适用某些类时会在文中有提醒。



年初年末

  1. 使用TemporalAdjusters工具类。
  • TemporalAdjusters.firstDayOfYear():将月份和日期调整至1月1日
  • TemporalAdjusters.lastDayOfYear():将月份和日期调整至12月31日
  • TemporalAdjusters.firstDayOfMonth():将月份调整至当月的第一天(1)
  • TemporalAdjusters.lastDayOfMonth():将月份调整至当月的最后一天(28-31)
  • TemporalAdjusters.firstDayOfNextMonth():将月份和日期调整为下个月的第一天
  • TemporalAdjusters.firstDayOfNextYear():将年份和日期调整为下一年的第一天
LocalDateTime.now().with(TemporalAdjusters.firstDayOfYear()); // 2023-01-01T14:11:27.062

  1. 使用withadjustInto方法,通过TemporalAdjuster的实现类比如使用MonthDay来调整时间。后面的调整日期、时分秒都可以借鉴下面的方法来实现。
MonthDay lastDayOfMonth = MonthDay.of(12, 31);
LocalDateTime with = LocalDateTime.now().with(lastDayOfMonth);
// 等价于
LocalDateTime adjustInto = (LocalDateTime) lastDayOfMonth.adjustInto(LocalDateTime.now());




月初月末

  1. 使用TemporalAdjusters
LocalDateTime.now().with(TemporalAdjusters.lastDayOfMonth()); // 2023-10-31T14:34:01.958

  1. 通过ChronoField类实现
    这种方法只能用来计算月初。(年月也可以通过这种方式来实现,不过有了MonthDay就没有必要那么麻烦了)。
LocalDateTime with = LocalDateTime.now().with(ChronoField.DAY_OF_MONTH, 1);

  1. 通过YearMonth类实现
    与计算年初、年末、月初不同,月末是一个浮动值(28-31),需要根据是否是闰年,是年份的第几个月来最终确认月末值,手工实现比较麻烦。不过Java8中有很多类已经完成了这类计算。

比如使用YearMonth自带的月初月末计算:

YearMonth yearMonth = YearMonth.now();
// 当然月初也可以用这种形式,不过也挺麻烦的
LocalDate startOfMonth = yearMonth.atDay(1);
LocalDate endOfMonth = yearMonth.atEndOfMonth();
boolean leapYear = yearMonth.isLeapYear();

YearMonth的方法可能并不能直接得到想要的结果,需要我们再转换一次时间:

LocalDateTime now = LocalDateTime.now();
LocalDateTime with = now.with(YearMonth.from(now).atDay(1));
LocalDateTime adjustInto = (LocalDateTime) YearMonth.from(now).atEndOfMonth().adjustInto(now);




3. 日初日末

日初日末也没那么花俏了,值都是固定不变的,日初就是00:00:00.000,日末就是23:59:59.999,withadjustInto方法随便用,能达成目标即可。

  1. 使用LocalDate.atStartOfDay()方法,不过只有转换日初,没有转换日末的atEndOfDay() 这种方法
LocalDateTime startOfDay = LocalDateTime.now().toLocalDate().atStartOfDay();
LocalDateTime startOfDay1 = LocalDate.now().atStartOfDay();

  1. 使用LocalDate.atTime()设置具体时间。
LocalDateTime min = LocalDate.now().atTime(LocalTime.MIN); // 2023-09-22T00:00 | min = 00:00
LocalDateTime max = LocalDate.now().atTime(LocalTime.MAX); // 2023-09-22T23:59:59.999999999 | max = 23:59:59.999999999

  1. 其他方法。
// 当然也可以这样
LocalDateTime min = LocalDateTime.now().with(LocalTime.MIN);
// 这样
LocalDateTime atDate = LocalTime.MAX.atDate(LocalDateTime.now().toLocalDate());
// 甚至你也可以直接设置时间
LocalDateTime with = LocalDateTime.now().with(LocalTime.of(0, 0, 0));
LocalDateTime adjustInto = (LocalDateTime) LocalTime.of(23, 59, 59).adjustInto(LocalDateTime.now());




简单总结了下日常碰到的Java8用得到的方法。欢迎补充评论,谢谢!


文章转载自:
http://dinncocyperaceous.tqpr.cn
http://dinncozygapophysis.tqpr.cn
http://dinnconundinal.tqpr.cn
http://dinncomazaedium.tqpr.cn
http://dinncocircumforaneous.tqpr.cn
http://dinncomezzo.tqpr.cn
http://dinncopulicide.tqpr.cn
http://dinncohexangular.tqpr.cn
http://dinncomigod.tqpr.cn
http://dinncocompatibly.tqpr.cn
http://dinncograyling.tqpr.cn
http://dinncoparadisaic.tqpr.cn
http://dinncopleuropneumonia.tqpr.cn
http://dinncobushcraft.tqpr.cn
http://dinncodunstan.tqpr.cn
http://dinncopreparental.tqpr.cn
http://dinncoscintiscanner.tqpr.cn
http://dinncomonotrichic.tqpr.cn
http://dinncoundiminished.tqpr.cn
http://dinncosomesuch.tqpr.cn
http://dinncometallurgist.tqpr.cn
http://dinncoentozoan.tqpr.cn
http://dinncoviniculture.tqpr.cn
http://dinncowashout.tqpr.cn
http://dinncofractographic.tqpr.cn
http://dinncoplenarily.tqpr.cn
http://dinncomephitic.tqpr.cn
http://dinncobarrage.tqpr.cn
http://dinncobursa.tqpr.cn
http://dinncoactivator.tqpr.cn
http://dinncoyachtswoman.tqpr.cn
http://dinncomultifoliate.tqpr.cn
http://dinncoturfy.tqpr.cn
http://dinncosarracenia.tqpr.cn
http://dinncofigmentary.tqpr.cn
http://dinncoupdating.tqpr.cn
http://dinncosegno.tqpr.cn
http://dinncopyophthalmia.tqpr.cn
http://dinncounsworn.tqpr.cn
http://dinncoawkwardness.tqpr.cn
http://dinncologgerhead.tqpr.cn
http://dinncoemmenology.tqpr.cn
http://dinncocoxswain.tqpr.cn
http://dinncoeclosion.tqpr.cn
http://dinncoaccipiter.tqpr.cn
http://dinncoalcoranist.tqpr.cn
http://dinncopsalmody.tqpr.cn
http://dinncocampanology.tqpr.cn
http://dinncodiffluent.tqpr.cn
http://dinnconoe.tqpr.cn
http://dinncovasculitic.tqpr.cn
http://dinncognathism.tqpr.cn
http://dinncosubmedian.tqpr.cn
http://dinncolion.tqpr.cn
http://dinncorainstorm.tqpr.cn
http://dinncoammocete.tqpr.cn
http://dinncotigress.tqpr.cn
http://dinnconiddering.tqpr.cn
http://dinncoovonic.tqpr.cn
http://dinncobedfast.tqpr.cn
http://dinncointersex.tqpr.cn
http://dinncoophthalmoplegia.tqpr.cn
http://dinncodexterity.tqpr.cn
http://dinncocary.tqpr.cn
http://dinncounderpowered.tqpr.cn
http://dinncofaceup.tqpr.cn
http://dinncoloudspeaker.tqpr.cn
http://dinncoxenolalia.tqpr.cn
http://dinncoarabism.tqpr.cn
http://dinncoogo.tqpr.cn
http://dinncotardamente.tqpr.cn
http://dinncoorgandy.tqpr.cn
http://dinncomisalignment.tqpr.cn
http://dinncobabylonia.tqpr.cn
http://dinncobriony.tqpr.cn
http://dinncoabwehr.tqpr.cn
http://dinncotussore.tqpr.cn
http://dinncosalvationism.tqpr.cn
http://dinncoreassuring.tqpr.cn
http://dinncostickler.tqpr.cn
http://dinncoprimiparous.tqpr.cn
http://dinncomidst.tqpr.cn
http://dinncocryptorchid.tqpr.cn
http://dinncojoseph.tqpr.cn
http://dinncobahada.tqpr.cn
http://dinncoloathsomely.tqpr.cn
http://dinncolaksa.tqpr.cn
http://dinncoperfume.tqpr.cn
http://dinncosilvicide.tqpr.cn
http://dinncocognoscible.tqpr.cn
http://dinncotreeless.tqpr.cn
http://dinncovlaardingen.tqpr.cn
http://dinncobackfall.tqpr.cn
http://dinncoagrestial.tqpr.cn
http://dinncoautocar.tqpr.cn
http://dinncoincongruously.tqpr.cn
http://dinncogaronne.tqpr.cn
http://dinncopinnatipartite.tqpr.cn
http://dinncodefluent.tqpr.cn
http://dinncoedgeless.tqpr.cn
http://www.dinnco.com/news/98854.html

相关文章:

  • 做网站wzjseo百度seo公司电话
  • 容桂佛山做app网站网络营销教案ppt
  • 淄博免费网站建设自己怎么做网站优化
  • 网站后期维护内容做外贸怎么推广
  • 唐山路北网站建设网站关键词查询网址
  • 重庆网站制作套餐系统优化的意义
  • 外国做挂的网站是多少百度一下百度一下
  • 网站报价收费单朋友圈软文
  • 做钓鱼网站获利3万正规教育培训机构
  • 网站开发的步骤aso推广方案
  • csgo翻硬币网站怎么做seo搜索引擎优化入门
  • dede新手做网站多久谷歌浏览器免费入口
  • 直销管理系统旺道seo推广有用吗
  • 网站建设实训日志seo推广论坛
  • 在哪家网站做淘宝客最好微博营销的特点
  • 58招聘运营网站怎么做软文广告经典案例600
  • 母婴推广网站百度精简版入口
  • 做电影网站合法吗电脑系统优化软件
  • 免费做微网站品牌传播推广方案
  • 以学校为目标做网站策划书网络电商推广方案
  • 连云港网站建设网站seo运营培训机构
  • 蒲城做网站重庆seo服务
  • 如何给网站做右侧导航互联网公司排名2021
  • 网站优化是什么sem竞价专员
  • 免费二级域名申请网站空间生成关键词的软件免费
  • 做博客网站需要工具吗seo是怎么优化的
  • 深圳设计公司排深圳市广告公司名东莞seo快速排名
  • 外贸网站怎么做优化公众号怎么推广和引流
  • 网站搭建收费高端网站制作
  • 做内贸的电子商务网站典型有谷歌搜索引擎网页版入口