Java LocalDateTime

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.
    Java LocalDateTime

    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类实现ComparableChronoLocalDateTimeTemporalTemporalAdjusterTermporalAccessorSerializable接口。
  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 day
		LocalDateTime ldt1 = ldt.plusYears(1).plusMonths(1).plusWeeks(1).plusDays(1);
		System.out.println(ldt1);
		
		// subtracting 1 year, 1 month, 1 week and 1 day
		LocalDateTime ldt2 = ldt1.minusYears(1).minusMonths(1).minusWeeks(1).minusDays(1);
		System.out.println(ldt2);
		
		//adding 1 hour, 1 minute, 1 second and 100 nanos
		LocalDateTime ldt3 = ldt2.plusHours(1).plusMinutes(1).plusSeconds(1).plusNanos(100);
		System.out.println(ldt3);
				
		// subtracting 1 hour, 1 minute, 1 second and 100 nanos
		LocalDateTime 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值