Java LocalDateTime获取前一天,—-JDK8新时间类的简单使用


JDK8中增加了一系列时间的类,

(据说)是为了干掉过去的Date,Calendar类的,

过去的Date类(据说)有着线程不安全等诸多弊端,

至于我的个人感受就是用起来实在是很麻烦,我一般封装成几个常用的方法以后每次就调方法,再也不想看里面是怎么实现的了.

而发现了LocalDateTime这种新类以后,经过我的简单的试用,觉得完全可以取代掉之前使用时间的一切方法.非常好用,太好用了.

下面是简单的使用教程:

获取当前年/月/日

 

 

获取前一天以前是这么干的

public static String getYesterdayByFormat(String timeFormat){ //获取当前日期 Date date = new Date(); SimpleDateFormat sf = new SimpleDateFormat(timeFormat); //通过秒获取下一天日期 long time = (date.getTime() / 1000) - 60 * 60 * 24;//秒 date.setTime(time * 1000);//毫秒 String yesterday = sf.format(date).toString(); return yesterday; }

现在可以这样

public static String getYesterdayByFormat(String timeFormat){ return LocalDateTime.now().plusDays(1).format(DateTimeFormatter.ofPattern(timeFormat)); }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM