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); } }
输出:
如示例输出所示,我们可以看到格式化前后的日期和时间都已显示。
示例#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,如代码中所示。我们在格式化前和格式化后都得到了答案,如下所示。
输出:
示例 #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 日。
输出:
示例#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 包以确保其正确运行并根据需要生成特定输出。
输出:
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.
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中文网其他相关文章!

类加载器通过统一的类文件格式、动态加载、双亲委派模型和平台无关的字节码,确保Java程序在不同平台上的一致性和兼容性,实现平台独立性。

Java编译器生成的代码是平台无关的,但最终执行的代码是平台特定的。1.Java源代码编译成平台无关的字节码。2.JVM将字节码转换为特定平台的机器码,确保跨平台运行但性能可能不同。

多线程在现代编程中重要,因为它能提高程序的响应性和资源利用率,并处理复杂的并发任务。JVM通过线程映射、调度机制和同步锁机制,在不同操作系统上确保多线程的一致性和高效性。

Java的平台独立性是指编写的代码可以在任何安装了JVM的平台上运行,无需修改。1)Java源代码编译成字节码,2)字节码由JVM解释执行,3)JVM提供内存管理和垃圾回收功能,确保程序在不同操作系统上运行。

Javaapplicationscanindeedencounterplatform-specificissuesdespitetheJVM'sabstraction.Reasonsinclude:1)Nativecodeandlibraries,2)Operatingsystemdifferences,3)JVMimplementationvariations,and4)Hardwaredependencies.Tomitigatethese,developersshould:1)Conduc

云计算显着提升了Java的平台独立性。 1)Java代码编译为字节码,由JVM在不同操作系统上执行,确保跨平台运行。 2)使用Docker和Kubernetes部署Java应用,提高可移植性和可扩展性。

Java'splatformindependenceallowsdeveloperstowritecodeonceandrunitonanydeviceorOSwithaJVM.Thisisachievedthroughcompilingtobytecode,whichtheJVMinterpretsorcompilesatruntime.ThisfeaturehassignificantlyboostedJava'sadoptionduetocross-platformdeployment,s

容器化技术如Docker增强而非替代Java的平台独立性。1)确保跨环境的一致性,2)管理依赖性,包括特定JVM版本,3)简化部署过程,使Java应用更具适应性和易管理性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

Atom编辑器mac版下载
最流行的的开源编辑器

Dreamweaver Mac版
视觉化网页开发工具

SublimeText3 Linux新版
SublimeText3 Linux最新版