Java LocalDateTime class is part of Java 8 Date API.

Java LocalDateTime类是Java 8 Date API的一部分。

Java LocalDateTime (Java LocalDateTime)

  1. Java LocalDateTime is an immutable class, so it’s thread safe and suitable to use in multithreaded environment.Java LocalDateTime是一个不可变的类 ,因此它是线程安全的 ,适合在多线程环境中使用。
  2. LocalDateTime provides date and time output in the format of YYYY-MM-DD-hh-mm-ss.zzz, extendable to nanosecond precision. So we can store “2017-11-10 21:30:45.123456789” in LocalDateTime object.

    LocalDateTime以YYYY-MM-DD-hh-mm-ss.zzz格式提供日期和时间输出,可扩展到纳秒精度。 因此我们可以将“ 2017-11-10 21:30:45.123456789”存储在LocalDateTime对象中。

  3. Just like the LocalDate class, LocalDateTime has no time zone data. We can use LocalDateTime instance for general purpose date and time information.就像LocalDate类一样,LocalDateTime也没有时区数据。 我们可以使用LocalDateTime实例获取通用日期和时间信息。
  4. LocalDateTime class implements Comparable, ChronoLocalDateTime, Temporal, TemporalAdjuster, TermporalAccessor and Serializable interfaces.LocalDateTime类实现Comparable , ChronoLocalDateTimeTemporalTemporalAdjusterTermporalAccessorSerializable接口。
  5. LocalDateTime is a final class, so we can’t extend it.LocalDateTime是最终类,因此我们无法扩展它。
  6. LocalDateTime is a value based class, so we should use equals() method to check if two instances of LocalDateTime are equal or not.LocalDateTime是一个基于值的类,因此我们应该使用equals()方法检查LocalDateTime的两个实例是否相等。
  7. LocalDateTime class is a synergistic ‘combination’ of LocalDate and LocalTime with the contatenated format as shown in the figure above.LocalDateTime类是LocalDate和LocalTime的协同“组合”,其格式受污染,如上图所示。

导入Java LocalDateTime类 (Importing Java LocalDateTime Class)

Java LocalDateTime class is part of java.time package. So we can import it like following:

Java LocalDateTime类是java.time包的一部分。 因此我们可以像下面这样导入它:

import java.time.LocalDateTime;

创建LocalDateTime实例 (Creating LocalDateTime instance)

There are following ways to create LocalDateTime object.

有以下几种创建LocalDateTime对象的方法。

  1. Invoking the static now() method that returns the current date-time from the system clock in the default time-zone.

    LocalDateTime ldt = LocalDateTime.now();

    调用static now()方法,该方法从系统时钟以默认时区返回当前日期时间。

  2. By passing year, month, day, hour, minute, second and nanosecond values to of() method. This method is overloaded and one of them takes argument at LocalDate and LocalTime.
    LocalDateTime ldt = LocalDateTime.of(2017,11,6,6,30,40,50000);ldt = LocalDateTime.of(LocalDate.now(), LocalTime.now());

    通过将年,月,日,小时,分钟,秒和纳秒值传递给of()方法。 此方法已重载,其中一个在LocalDate和LocalTime接受参数。

  3. Using parse() method and passing date time string representation.
    LocalDateTime ldt = LocalDateTime.parse("2017-11-10T22:11:03.460446");

    使用parse()方法并传递日期时间字符串表示形式。

  4. Using ofInstant() by passing Instant and ZoneId information.
    LocalDateTime ldt = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());

    通过传递即时和ZoneId信息来使用ofInstant()

从LocalDateTime实例检索日期时间信息 (Retrieving Date Time Information from LocalDateTime instance)

Let’s look into some of the LocalDateTime methods to retrieve date time information.

让我们看一下一些LocalDateTime方法来检索日期时间信息。

  1. getHour(): returns the int hour contained within the LocalDateTime object, from 0 to 23.getHour() :返回LocalDateTime对象中包含的int小时,从0到23。
  2. getMinute(): returns the int minute information, from 0 to 59.getMinute() :返回整数分钟信息,从0到59。
  3. getSecond(): returns the int second contained within the LocalDateTime object, from 0 to 59.getSecond() :返回LocalDateTime对象中包含的int秒,从0到59。
  4. getNano(): returns the int nanosecond from the LocalDateTime object, ranging from 0 to 999,999,999.getNano() :从LocalDateTime对象返回int纳秒,范围从0到999,999,999。

Apart from above, LocaDate class stock methods such as getYear(), getMonth(), getDayOfMonth(), getDayOfWeek(), getDayOfYear() are also applicable to LocalDateTime class. Let’s look at these methods through an example.

除上述之外,LocaDate类的常用方法(例如getYear(),getMonth(),getDayOfMonth(),getDayOfWeek(),getDayOfYear())也适用于LocalDateTime类。 让我们通过一个例子来看看这些方法。

Java LocalDateTime示例 (Java LocalDateTime Example)

package com.journaldev.java;import java.time.LocalDateTime;public class JavaLocalDateTimeExample {public static void main(String[] args) {LocalDateTime ldt = LocalDateTime.of(2017, 11, 6, 6, 30, 40, 50000);System.out.println("Hour = " + ldt.getHour());System.out.println("Minute = " + ldt.getMinute());System.out.println("Second = " + ldt.getSecond());System.out.println("Nano = " + ldt.getNano());System.out.println("Year = " + ldt.getYear());System.out.println("Month = " + ldt.getMonth());System.out.println("Date = " + ldt.getDayOfMonth());}}

Java LocalDateTime方法–加号和减号 (Java LocalDateTime methods – plus and minus)

LocalDateTime class has many methods to plus and minus date time components. Note that it returns a copy of the LocalDateTime object with the change, no change happens in the specified object. Some of these methods are:

LocalDateTime类具有许多用于加减日期时间的方法。 请注意,它会返回具有更改的LocalDateTime对象的副本,在指定的对象中不会发生任何更改。 其中一些方法是:

  1. plusHours(), plusMinutes(), plusSeconds() and plusNanos() for adding time component in LocalDateTime object.plusHours(),plusMinutes(),plusSeconds()和plusNanos()用于在LocalDateTime对象中添加时间分量。
  2. minusHours(), minusMinutes(), minusSeconds() and minusNanos() for subtracting time component in LocalDateTime object.minusHours(),minusMinutes(),minusSeconds()和minusNanos()用于减去LocalDateTime对象中的时间分量。
  3. plusYears(), plusMonths(), plusWeeks() and plusDays() for adding date component in LocalDateTime object.plusYears(),plusMonths(),plusWeeks()和plusDays()用于在LocalDateTime对象中添加日期组件。
  4. minusYears(), minusMonths(), minusWeeks() and minusDays() for subtracting date component in LocalDateTime object.minusYears(),minusMonths(),minusWeeks()和minusDays()用于减去LocalDateTime对象中的日期分量。

All the above method take long argument, their names clearly suggests what they are used for.

以上所有方法都经过很长时间的争论,它们的名称清楚地表明了它们的用途。

Here is an example for LocalDateTime plus and minus methods.

这是LocalDateTime的加号和减号方法的示例。

package com.journaldev.java;import java.time.LocalDateTime;public class JavaLocalDateTimeExample {public static void main(String[] args) {LocalDateTime ldt = LocalDateTime.of(2017, 11, 10, 6, 0);System.out.println(ldt);//adding 1 year, 1 month, 1 week and 1 dayLocalDateTime ldt1 = ldt.plusYears(1).plusMonths(1).plusWeeks(1).plusDays(1);System.out.println(ldt1);// subtracting 1 year, 1 month, 1 week and 1 dayLocalDateTime ldt2 = ldt1.minusYears(1).minusMonths(1).minusWeeks(1).minusDays(1);System.out.println(ldt2);//adding 1 hour, 1 minute, 1 second and 100 nanosLocalDateTime ldt3 = ldt2.plusHours(1).plusMinutes(1).plusSeconds(1).plusNanos(100);System.out.println(ldt3);// subtracting 1 hour, 1 minute, 1 second and 100 nanosLocalDateTime ldt4 = ldt3.minusHours(1).minusMinutes(1).minusSeconds(1).minusNanos(100);System.out.println(ldt4);}
}

Output of above program is:

上面程序的输出是:

2017-11-10T06:00
2018-12-18T06:00
2017-11-10T06:00
2017-11-10T07:01:01.000000100
2017-11-10T06:00

As you can see that if the second and nano values are 0, then it’s not getting returned when we call LocalDateTime toString() method.

如您所见,如果第二个和nano的值为0,则在调用LocalDateTime toString()方法时不会返回它。

Java LocalDateTime至今 (Java LocalDateTime to Date)

We should avoid using legacy java.util.Date class, but if required then we can convert LocalDateTime to Date using below code.

我们应该避免使用旧版java.util.Date类,但是如果需要,我们可以使用以下代码将LocalDateTime转换为Date。

LocalDateTime ldt = LocalDateTime.of(2017, 11, 10, 6, 0);
Date date = Date.from(ldt.toInstant(ZoneOffset.UTC));
System.out.println(date); // prints "Fri Nov 10 11:30:00 IST 2017"

Note that above output is because the LocalDateTime doesn’t have timezone information, so we need to provide timezone to offset and convert to local timezone.

请注意,上面的输出是因为LocalDateTime没有时区信息,因此我们需要提供时区以进行偏移并转换为本地时区。

That’s all for java LocalDateTime class.

Java LocalDateTime类就这些了。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/16913/java-localdatetime

Java LocalDateTime相关推荐

  1. Java LocalDateTime 和 Date 互转

    Java LocalDateTime 和 Date 互转 1 LocalDateTime -> Date 2 Date -> LocalDateTime 3 str -> Local ...

  2. Java LocalDateTime类| 带示例的getDayOfWeek()方法

    LocalDateTime类getDayOfWeek()方法 (LocalDateTime Class getDayOfWeek() method) getDayOfWeek() method is ...

  3. Java LocalDateTime类| 带示例的getMinute()方法

    LocalDateTime类getMinute()方法 (LocalDateTime Class getMinute() method) getMinute() method is available ...

  4. Java LocalDateTime类| 带示例的compareTo()方法

    LocalDateTime类compareTo()方法 (LocalDateTime Class compareTo() method) compareTo() method is available ...

  5. Java LocalDateTime类| 带示例的getDayOfYear()方法

    LocalDateTime类getDayOfYear()方法 (LocalDateTime Class getDayOfYear() method) getDayOfYear() method is ...

  6. java LocalDateTime 加时间,计算两个时间的差

    LocalDateTime localDateTime = LocalDateTime.now();localDateTime= localDateTime.plusDays(1);//加一天 计算两 ...

  7. Java LocalDateTime给当前时间加半小时

    前言 现在有这样一个需求,需要将当前时间加上半小时返回给前端,在Java8之后推荐用LocalDateTime来替换Date,它提供了丰富的方法给我们来调用,那我们来实现一下 实现 如图所示,我们使用 ...

  8. Java——LocalDateTime

    JAVA8新特性中添加的时间处理类,相比与以前使用的Date和Canlendar类,该类在时间处理上更加的简单方便.下面记录一些常用的方法. 目录 一.获取LocalDateTime类型的时间(静态方 ...

  9. JAVA LocalDateTime 获取当前年份的开始时间和去年开始时间

    使用JKD8提供的时间工具 获取当年的开始时间 和去年开始时间 LocalDateTime thisYear = LocalDateTime.of(LocalDate.now().getYear(), ...

最新文章

  1. 使用VS 自带的打包工具,制作winform安装项目
  2. 项目展示文案生成设计
  3. 水晶报表主子报表分页问题
  4. Python: adb自动化ce'shi
  5. 冰豹lua驱动设置_通过编写“猜数字”游戏学习 Lua | Linux 中国
  6. 最终计算供应链管理生产计划排程逻辑管理
  7. 没有逾期过借呗为什么被关了?
  8. 解决文件下载在火狐浏览器出现中文文件名乱码的方法
  9. 使用PDF-XChange Editor为PDF文件添加签名(图片+签名)
  10. php读取doc pdf文件,PHP读取创建txt,doc,xls,pdf类型文件
  11. 正态分布下贝叶斯决策的特例(三)
  12. docker搭建pgadmin并挂载
  13. swoole学习之: MQTT (物联网) 服务器
  14. 三菱FX5U,机床X轴Y轴工作台定位控制程序 使用三菱J4-A系列伺服驱动器绝对位置系统,程序大小27000多步
  15. 《医院信息系统(HIS)软件基本功能规范》98版 [赏析]
  16. 解决为什么电脑连接不上网络,应该如何查找问题
  17. 计算机错误651是什么故障,宽带连接错误显示代码651是什么原因 宽带连接错误651的解决方法...
  18. 实时操作系统和分时操作系统的区别
  19. 路由器设置服务器用什么协议,路由器配置基础知识
  20. [附源码]Python计算机毕业设计办公用品管理系统Django(程序+LW)

热门文章

  1. HDU 3420 Bus Fair [补]
  2. nginx做代理上网
  3. GridView动态添加模版列
  4. [转载] comma.ai自动驾驶代码浅析及实践
  5. 项目案例模板之jdbc两种连接方式
  6. css part 2
  7. IDEA初使用:解决搜狗输入法不跟随BUG
  8. IOS --- OC与Swift混编(转)
  9. 利用UICollectionView实现瀑布流
  10. hdu 3255 Farming(扫描线)