Java 本地日期时间
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中文网其他相关文章!

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

胶囊是一种三维几何图形,由一个圆柱体和两端各一个半球体组成。胶囊的体积可以通过将圆柱体的体积和两端半球体的体积相加来计算。本教程将讨论如何使用不同的方法在Java中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。
