Java 8日期时间API教程:LocalDateTime

该博文是Java 8中引入的有关Date Time API的系列教程的一部分。在本博文中,我将介绍LocalDateTime类中可用的一些方法。

LocalDateTime是一个不可更改的线程安全对象,它表示ISO-8601日历系统中没有时区的日期时间,例如2014-03-30T02:51:21。 通常以年-月-日-时-分-秒表示,并且提供高达纳秒范围的精度。

  • 对于月份,有效值范围是1 – 28/31
  • 对于小时,有效值范围是0 – 23。
  • 分钟和秒的有效值范围是0 – 59。
  • 对于毫微秒,有效值范围是0 – 999999999。

可以通过以下几种方法创建LocalDateTime对象。

LocalDateTime dateTime = LocalDateTime.now();//returns the system date time in the default timezone.
System.out.println("dateTime from the system is:"+dateTime);//creates a date time with the values provided as parameters in the sequence year, month, day of the month, hour of the day, minute of the hour. The second and nano second are set to 0. 
LocalDateTime ldtm = LocalDateTime.of(2014, 3, 30, 12, 30);
System.out.println("dateTime created by passing in year, month,day, hour, minute is:"+ldtm.toString());//creates a date time with the values provided as parameters in the sequence year, month, day of the month, hour of the day, minute of the hour, seconds of the minute. The nano second is set to 0.
LocalDateTime ldts = LocalDateTime.of(2014, 3, 30, 12, 30,23);
System.out.println("dateTime created by passing in year, month,day, hour, minute, seconds is :"+ldts.toString());//creates a date time with the values provided as parameters in the sequence year, month, day of the month, hour of the day, minute of the hour, seconds of the minute and nano second of the second.
LocalDateTime ldtns = LocalDateTime.of(2014, 3, 30, 12, 30,23,12000);
System.out.println("dateTime created by passing in year, month,day, hour, minute, seconds, nano seconds is :"+ldtns.toString());//creates a date time with the values provided as parameters in the sequence year, month value from the Month enum, day of the month, hour of the day, minute of the hour. The second and nano second are set to 0.
LocalDateTime ldtem = LocalDateTime.of(2013, Month.APRIL, 1, 12, 30);
System.out.println("The date time object created by passing in year, month from enum, day, hour , minute is : "+ldtem.toString());//creates a date time with the values provided as parameters in the sequence year, month value from the Month enum, day of the month, hour of the day, minute of the hour, seconds of the minute. The nano second is set to 0.
LocalDateTime ldtes = LocalDateTime.of(2013, Month.APRIL, 1, 12, 30,42);
System.out.println("The date time object created by passing in year, month from enum, day, hour , minute,second is : "+ldtes.toString());//creates a date time with the values provided as parameters in the sequence year, month value from the Month enum, day of the month, hour of the day, minute of the hour, seconds of the minute and nano seconds of the seconds.
LocalDateTime ldtens = LocalDateTime.of(2013, Month.APRIL, 1, 12, 30,42,12000);
System.out.println("The date time object created by passing in year, month from enum, day, hour , minute,seconds, ns is : "+ldtens.toString());

有几种方法可以获取字段的值,例如月,年,周几,月,秒,分钟,纳秒。

int dayOfMonth = dateTime.getDayOfMonth();//returns the day of the month.
System.out.println("day Of Month is :"+dayOfMonth);
DayOfWeek dow = dateTime.getDayOfWeek();//returns DayOfWeek enum.
System.out.println("dow:"+dow);
int dayOfYear = dateTime.getDayOfYear();//returns the day of the year.
System.out.println("day Of Year is :"+dayOfYear);
int hour = dateTime.getHour();//returns hour of the day.
System.out.println("hour:"+hour);
int monthValue = dateTime.getMonthValue();//returns the number of the month in the year.
System.out.println("month of the date in number is:"+monthValue);
Month month = dateTime.getMonth();// returns the month enum for the month of the year.
System.out.println("month of the date is :"+month);
int sec = dateTime.getSecond();// returns seconds field value for the date time.
System.out.println("Seconds of the date time is:"+sec);
int nano = dateTime.getNano();();// returns nano seconds field value for the date time.
System.out.println("nano seconds of the date time is:"+nano);
int year = dateTime.getYear();();// returns year field value for the date time.
System.out.println("year of the date is :"+year);

有几种minusXXX方法可以从日期中减去字段的值。

//creates a new date time copy after subtracting 10 days from the date time.
LocalDateTime ldtmd = dateTime.minusDays(10);System.out.println("date time after subtracting days is : "+ldtmd.toString());
//creates a new date time copy after subtracting 10 hours from the date time.
LocalDateTime ldtmh = dateTime.minusHours(10);
System.out.println("date time after subtracting hours is : "+ldtmh.toString());
//creates a new date time copy after subtracting 21 minutes from the date time.
LocalDateTime ldtmm = dateTime.minusMinutes(21);
System.out.println("date time after subtracting minutes is : "+ldtmm.toString());
//creates a new date time copy after subtracting 2 months from the date time.
LocalDateTime ldtmmm = dateTime.minusMonths(2);
System.out.println("date time after subtracting months is : "+ldtmmm.toString());
//creates a new date time copy after subtracting 3 years from the date time.
LocalDateTime ldtmy = dateTime.minusYears(3);
System.out.println("date time after subtracting years is : "+ldtmy.toString());
//creates a new date time copy after subtracting 32 weeks from the date time.
LocalDateTime ldtmw = dateTime.minusWeeks(32);
System.out.println("date time after subtracting weeks is : "+ldtmw.toString());
//creates a new date time copy after subtracting 1200 seconds from the date time.
LocalDateTime ldtms = dateTime.minusSeconds(1200);
System.out.println("date time after subtracting secs is : "+ldtms.toString());
//creates a new date time copy after subtracting 12000 nano seconds from the date time.
LocalDateTime ldtmn = dateTime.minusNanos(12000);
System.out.println("date time after subtracting nanos is : "+ldtmn.toString());

类似地,还有plusXXX方法可以将值添加到可以创建LocalDateTime对象的日期时间的字段中。

//creates a new date time copy after adding 10 days to the date time. 
LocalDateTime ldtpd = dateTime.plusDays(10);
System.out.println("date time after adding days is : "+ldtpd.toString());
//creates a new date time copy after adding 100 hours to the date time. 
LocalDateTime ldtph = dateTime.plusHours(100);
System.out.println("date time after adding hours is : "+ldtph.toString());
//creates a new date time copy after adding 190 minutes to the date time. 
LocalDateTime ldtpmm = dateTime.plusMinutes(190);
System.out.println("date time after adding minutes is : "+ldtpmm.toString());
//creates a new date time copy after adding 32 months to the date time. 
LocalDateTime ldtpdm = dateTime.plusMonths(32);
System.out.println("date time after adding months is : "+ldtpdm.toString());
//creates a new date time copy after adding 120000 nano seconds to the date time. 
LocalDateTime ldtpn = dateTime.plusNanos(120000);
System.out.println("date time after adding nanos is : "+ldtpn.toString());
//creates a new date time copy after adding 1200 seconds to the date time. 
LocalDateTime ldtps = dateTime.plusSeconds(1200);
System.out.println("date time after adding seconds is : "+ldtps.toString());
//creates a new date time copy after adding 24 weeks to the date time. 
LocalDateTime ldtpw = dateTime.plusWeeks(24);
System.out.println("date time after adding weeks is : "+ldtpw.toString());
//creates a new date time copy after adding 3 years to the date time. 
LocalDateTime ldtpy = dateTime.plusYears(3);
System.out.println("date time after adding years is : "+ldtpy.toString());

有几种withXXX方法可以将值设置为特定字段。

LocalDateTime ldtmddm = dateTime.withDayOfMonth(10); // sets the day of the month to 10.
System.out.println("date time after adding modifying day of the month is : "+ldtmddm.toString());
LocalDateTime ldtmddy = dateTime.withDayOfYear(12);//sets the day of year to 12.
System.out.println("date time after adding modifying day of the year is : "+ldtmddy.toString());
LocalDateTime ldtmdh = dateTime.withHour(12);//sets the hour of the day to 12. The other date time fields are not modified.
System.out.println("date time after adding modifying hour is : "+ldtmdh.toString());
LocalDateTime ldtmdmm = dateTime.withMinute(12);//sets the minute of the hour to 12. The other date time fields are not modified.
System.out.println("date time after adding modifying minutes is : "+ldtmdmm.toString());
LocalDateTime ldtmdm = dateTime.withMonth(4);//sets the month to 4. The other date time fields are not modified.
System.out.println("date time after adding modifying month is : "+ldtmdm.toString());
LocalDateTime ldtmdy = dateTime.withYear(2011);//sets the year to 2012. The other date time fields are not modified.
System.out.println("date time after adding modifying year  is : "+ldtmdy.toString());
LocalDateTime ldtmds = dateTime.withSecond(12);//sets the seconds of the minute to 12. The other date time fields are not modified.
System.out.println("date time after adding modifying seconds is : "+ldtmds.toString());
LocalDateTime ldtmdn = dateTime.withNano(120000);//sets the nano seconds of seconds to 120000. The other date time fields are not modified.
System.out.println("date time after adding modifying nanos is : "+ldtmdn.toString());

如果只想从该日期时间中检索日期或时间,则可以使用以下方法。

LocalDate ld = dateTime.toLocalDate();//gets the date value(LocalDate) from the date time. A LocalDate with same year, month and day as this LocalDateTime object will be returned.
System.out.println("The date field from the date time object is : "+ld.toString());
LocalTime lt = dateTime.toLocalTime();// gets the time value(LocalTime) from the date time. returns a LocalTime with the same hour, minute, second and nanosecond as this date-time.
System.out.println("The time field from the date time object is : "+lt.toString());

结论

LocalDateTime有一些操作日期和时间的选项,下面对其中一些进行了描述。

翻译自: https://www.javacodegeeks.com/2014/04/java-8-date-time-api-tutorial-localdatetime.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/363905.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

消息队列使用的四种场景介绍(转)

消息队列中间件是分布式系统中重要的组件,主要解决应用耦合,异步消息,流量削锋等问题 实现高性能,高可用,可伸缩和最终一致性架构 使用较多的消息队列有ActiveMQ,RabbitMQ,ZeroMQ,Ka…

一步一步SharePoint 2007之三十一:实现文档Event Handler(3)——附加Handler程序

摘要  本篇文章将介绍实现文档Event Handler的第三部分——附加Handler程序。正文  下面将记录每一步的操作过程。  1、首先打开我的网站,依次点击Document Center、Documents,进入Documents列表页面。  2、在Documents列表界面中点击Settings&a…

oracle数据库swap占用率高,物理内存空余很多,swap被持续占用的问题

本帖最后由 wyanghu 于 2012-8-18 10:21 编辑这套生产环境跑在vmware esx虚拟机上,现在遇到的问题是:操作系统内存空余很大,但swap被持续占用,直到swap被占完,估计系统就崩溃了。下面是我的操作系统参数及oracle参数&a…

Day2 HTML基本标签元素

Day2 HTML基本标签元素 HTML: 超文本标记语言(HyperText Mark-up Language ) 1.作用:写网页结构    2.HTML不区分大小写,建议小写   3.文件后缀 .html 或者 .htm   4.html由浏览器解析执行. 由上往下,由左往右 1) HTML标…

C# 如何跨平台调用C++的函数指针!

函数指针搞C的人应该都知道,效率高,易用性强,隐蔽代码等。在C里面调用C写的dll的函数指针那是在容易不过了。使用C#就稍微麻烦点了!那怎么掉呢?通过上面的第一篇文章我们知道应该使用委托 delegate。如果再高级点&…

Java hashCode() 和 equals()的若干问题解答

本章的内容主要解决下面几个问题: 1 equals() 的作用是什么? 2 equals() 与 的区别是什么? 3 hashCode() 的作用是什么? 4 hashCode() 和 equals() 之间有什么联系? https://www.cnblogs.com/skywang12345/p/3324958.…

Builder模式和Spring框架

介绍 每当对象同时具有强制属性和可选属性时,我都喜欢使用构建器模式 。 但是构建对象通常是Spring框架的责任,因此让我们看看如何同时使用基于Java和XML的Spring配置来使用它。 建造者的例子 让我们从下面的Builder类开始。 public final class Confi…

php数据库中统计人数用什么方法,在PHP中处理用户统计信息的最佳方法是什么

我如何处理 PHP中的用户统计信息?我可以选择两种明显的方法.两者都有缺陷.>必要时选择MySQL COUNT.这里的缺陷是,如果你要计算很多行,那么它可能会很慢,特别是当你必须在看似每个页面加载时这样做.好处是计数总是正确的.>将用户统计信息存储在统计信息表中.这…

作用域、执行环境、闭包(四)

本文也同步发表在我的公众号“我的天空” 上一期我们已经介绍了闭包,由于闭包可以延长函数内部的变量的生存周期,因此我们可以将不需要暴露在全局的变量封装成函数的内部变量,从而避免代码污染。 譬如要实现一个简单的累加器,为了…

今天发布了一个新的网站矩阵www.wimatrix.cn

关于科技生活新知的,digg类型,欢迎朋友们来访问,并提出宝贵的意见网址是 http://www.wimatrix.cn 转载于:https://www.cnblogs.com/liugod/archive/2007/09/29/910637.html

[Bzoj2243][SDOI2011]染色(线段树树剖)

题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id2243 线段树树链剖分,在线段树需要每次用lt和rt两个数组记录当前区间的左右边界的颜色,向上更新时需要判断左区间的右边界是否和右区间的左边界相等。在剖分求LCA的过程中需要在求…

php static_castunsigned int,C++ static_cast、dynamic_cast、const_cast和reinterpret_cast(四种类型转换运算符)...

上节讲到,隐式类型转换是安全的,显式类型转换是有风险的,C语言之所以增加强制类型转换的语法,就是为了强调风险,让程序员意识到自己在做什么。但是,这种强调风险的方式还是比较粗放,粒度比较大&…

NetBeans IDE 8.0和Java 8的新功能

NetBeans IDE 8.0已发布,还为Java 8技术提供了新功能。 它具有用于与Java SE 8,Java SE Embedded 8和Java ME Embedded 8配合使用的代码分析器和编辑器。IDE还具有新的增强功能,这些功能进一步改善了其对使用PrimeFaces对Maven和Java EE的支持…

AngularJS(九):路由

本文也同步发表在我的公众号“我的天空” AngularJS路由 AngularJS路由可以让我们通过不同的URL访问不同页面(似乎是废话),其价值主要体现在单页面的web应用中(single page web application,SPA)&#xff0…

(转)Oracle中实现行列转换的方法

(转自)http://blog.csdn.net/Torrice/archive/2006/01/25/587986.aspx 我们在写SQL语句的时候经常需要用到行与列的转换问题,对于一个新手来说可能比较困难,其实你只要能够熟练运用Decode和Sum函数,这个问题就迎刃而解. Create table tes…

[C3W2] Structuring Machine Learning Projects - ML Strategy 2

第二周:机器学习策略(2)(ML Strategy(2)) 误差分析(Carrying out error analysis) 你好,欢迎回来,如果你希望让学习算法能够胜任人类能做的任务&a…

mysql语句执行顺序图示

转载于:https://www.cnblogs.com/whalesea/p/10382227.html

玩Java 8 – Lambda和并发

因此Java 8不久前发布,具有许多功能和更改。 我们所有的Java狂热者一直在等待这个历史,从他们最初宣布Java 7的所有强大功能开始一直到最终被取消。 我最近才有时间实际开始给它一个真实的外观,我将我的家庭项目更新到了8个,我不…

用matlab 拟合实数解,求大神指点matlab用拟合的方式解延迟微分方程组参数

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼dy(1)-k*y(1)*y(2);dy(2)Z(3,1)-a*y(2)-q*y(2);dy(3)k*y(1)*y(2)-Z(3,1);dy(4)a*y(2);dy(5)q*y(2);t12345678910111213141516171819202122232425262728293031323334353637383940y208563475657545454535252515150504948484747464545…

AngularJS(三):重复HTML元素、数据绑定

本文也同步发表在我的公众号“我的天空” 重复HTML元素 在前端的页面编写中&#xff0c;我们会经常遇到重复HTML元素&#xff0c;譬如绘制表格、菜单等&#xff0c;如以下代码显示一个简单的li列表&#xff1a; <body> <ul id"ul_cities"> </ul…