首页  >  文章  >  Java  >  Java 本地日期时间

Java 本地日期时间

WBOY
WBOY原创
2024-08-30 15:52:00800浏览

java中的LocalDateTime在输出屏幕上显示本地日期和时间。显示时间的默认格式为 YYYY-MM-DD-hh-mm-ss-zzz。显示日期和时间的不同因素是年、月、日、小时、分钟、秒和纳秒。可以将日期和时间加上特定的天数,再减去一定的天数,最后就可以非常顺利地产生输出了。 LocalDateTime 类是最终类;因此,不允许扩展该类。 LocalDateTime 类是一个基于值的类,用于使用 equals() 检查两个日期和时间是否彼此相等。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

Java LocalDateTime 的语法

Java LocalDateTime 类是 java.time 包的一部分。我们可以通过以下方式创建该类的java实例。

以下是语法。

import java.time.LocalDateTime;

我们还可以将值传递给 LocalDateTime 类中的 of()。

以下语法如下:

LocalDateTime Idt= LocalDateTime.of(2011,15,6,6,30,50,100000);

我们还可以使用 parse() 并以字符串表示形式传递时间值。

LocalDateTime Idt= LocalDateTime.parse("2011-11-10T22:11:03.46045");

此外,我们可以通过传递 ID 和 Zone ID 信息来使用 ofInstant()。

LocalDateTime Idt= LocalDateTime.ofInstant(Instant.now(), ZoneId.SystemDefault());

Java中LocalDateTime的方法

Java LocalDateTime 类中有不同的方法。

  • 字符串格式(DateTimeFormatter formatter):用于使用指定的格式化程序格式化日期和时间。
  • LocalDateTime minusDays(long days): 用于使用特定日期格式化日期和时间,这些日期将从相应的日期中减去。
  • LocalDateTime plusDays(long days): 用于在当前日期上添加一定的天数,然后分别打印输出。
  • int get(TemporalField field): 用于获取 int 整数格式的日期和时间值。
  • static LocalDateTime now(): 我们使用此方法从当前时区检索默认日期和时间,该时区作为默认时区。

Java LocalDateTime 示例

下面给出了提到的示例:

示例#1

在第一个编码示例中,我们将见证java中使用format()来格式化特定数量的代码。

代码:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeExample
{
public static void main(String[] args)
{
LocalDateTime now = LocalDateTime.now();
System.out.println("Before doing Formatting: " + now);
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String fDTime = now.format(format);
System.out.println("After doing Formatting: " + fDTime);
}
}

输出:

如示例输出所示,我们可以看到格式化前后的日期和时间都已显示。

Java 本地日期时间

示例#2

在第二个程序中,我们将看到 minusDays() 的工作原理,即某个日期减去该日期的某些天数,然后最终打印特定的输出。

代码:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeExample2
{
public static void main(String[] args)
{
LocalDateTime dt1 = LocalDateTime.of(2018, 2, 14, 15, 22);
LocalDateTime dt2 = dt1.minusDays(100);
System.out.println("Before Formatting: " + dt2);
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");
String fDTime = dt2.format(format);
System.out.println("After Formatting: " + fDTime );
}
}

在上面的代码中,我们减去了一定的天数;在本例中,它是从 2018 年 2 月 14th 日期开始的 100,如代码中所示。我们在格式化前和格式化后都得到了答案,如下所示。

输出:

Java 本地日期时间

示例 #3

plusDays() 函数与 minusDays() 函数非常相似,唯一的区别是它在当前日期上添加天数,而不是减去特定的天数。因此,在此编码示例中,我们将在现有日期和时间的基础上添加一定的天数。

代码:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeExample3
{
public static void main(String[] args) {
LocalDateTime dt1 = LocalDateTime.of(2018, 1, 8, 12, 34);
LocalDateTime dt2 = dt1.plusDays(60);
System.out.println("Before Formatting: " + dt2);
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");
String fDTime = dt2.format(format);
System.out.println("After Formatting: " + fDTime );
}
}

在此示例代码中,我们在提供的默认日期中将日期指定为 2018 年 1 月 8。我们看到增加的天数,即 60 天。当我们向代码添加 60 天时,我们看到日期发生了变化,已经是 2018 年 3 月 9 了。时间保持不变。只是天数发生了变化,将日期从 1 月 8 日移至 3 月 9 日。

输出:

Java 本地日期时间

示例#4

在这个编码示例中,我们将看到 get() 的功能,我们将分别获取日期和时间的整数值。我们还将看到一个编码示例来正确说明该程序。

代码:

import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
public class LocalDateTimeExample4
{
public static void main(String[] args)
{
LocalDateTime b = LocalDateTime.of(2018, 3, 10, 14, 36);
System.out.println(b.get(ChronoField.DAY_OF_WEEK));
System.out.println(b.get(ChronoField.DAY_OF_YEAR));
System.out.println(b.get(ChronoField.DAY_OF_MONTH));
System.out.println(b.get(ChronoField.HOUR_OF_DAY));
System.out.println(b.get(ChronoField.MINUTE_OF_DAY));
}
}

示例代码按时间顺序给出了星期几、一年中的某一天、一个月中的某一天、一天中的小时和一天中的分钟。程序调用 ChronoField 包以确保其正确运行并根据需要生成特定输出。

输出:

Java 本地日期时间

Example #5

In this coding example, we will see the now() in the LocalDateTime class in Java programming language. This program will return the current date and time along with the seconds in the program.

Code:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeExample5
{
public static void main(String[] args)
{
LocalDateTime dt1 = LocalDateTime.now();
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String fDTime = dt1.format(format);
System.out.println(fDTime);
}
}

Output:

The output clearly displays the date and time of the current time zone in the format of hh-mm-ss, providing a clear representation.

Java 本地日期时间

Conclusion

This article has seen several programs illustrating all the methods and functions inside the LocalDateTime class. Also, we know the syntax of the LocalDateTime class that is present. Beyond this, we also notice the output of several programs. In aeronautical engineering, professionals frequently utilize the LocalDateTime class to maintain and monitor an aircraft’s current date and time, observing any changes in time zones and their impact on the watch time.

以上是Java 本地日期时间的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:Java OffsetDateTime下一篇:Java Duration