- 热门类别
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 精选阅读材料
- UPSC IAS 考试笔记
- 适合开发人员的最佳实践
- 问答
- 编写高效的简历
- 人力资源面试问题
- 计算机术语表
- 名人录
用不同格式显示日历年日期的 Java 程序
在本文中,我们将了解如何以不同格式显示日历年的日期。Java 没有内置的日期类,但我们可以导入 java.time 包来使用日期和时间 API。该包包括许多日期和时间类。
以下是相同的演示 −
假设我们的输入是 −
Run the program
预期输出为 −
The first date format is:2022-03-17T23:37:37.623304800 The second date format is:17/03/2022 The third date format is:Thursday, 17 Mar 2022
算法
Step 1 - START Step 2 - Declare an object of LocalDateTime namely date. Step 3 - Define the values. Step 4 - Define different date time formats using DateTimeFormatter objects Step 5 - Display the different date time formats Step 6 - Stop
示例 1
在这里,我们在“main”函数下将所有操作捆绑在一起。
import java.time.*; import java.time.format.DateTimeFormatter; public class Demo { public static void main(String[] args){ System.out.println("The required packages have been imported"); LocalDateTime date = LocalDateTime.now(); System.out.println("A LocalDateTime object has been defined"); System.out.println("\nThe first date format is:" +date); DateTimeFormatter date_format_1 = DateTimeFormatter.ofPattern("dd/MM/yyyy"); String formattedDate_1 = date.format(date_format_1); System.out.println("\nThe second date format is:" +formattedDate_1); DateTimeFormatter date_format_2 = DateTimeFormatter.ofPattern("EEEE, dd MMM yyyy"); String formattedDate_2 = date.format(date_format_2); System.out.println("\nThe third date format is:" +formattedDate_2); } }
输出
The required packages have been imported A LocalDateTime object has been defined The first date format is:2022-03-17T23:37:37.623304800 The second date format is:17/03/2022 The third date format is:Thursday, 17 Mar 2022
示例 2
在这里,我们将操作封装到函数中,展示面向对象的编程。
import java.time.*; import java.time.format.DateTimeFormatter; public class Demo { static void print_date_format(LocalDateTime date){ DateTimeFormatter date_format_1 = DateTimeFormatter.ofPattern("dd/MM/yyyy"); String formattedDate_1 = date.format(date_format_1); System.out.println("\nThe second date format is:" +formattedDate_1); DateTimeFormatter date_format_2 = DateTimeFormatter.ofPattern("EEEE, dd MMM yyyy"); String formattedDate_2 = date.format(date_format_2); System.out.println("\nThe third date format is:" +formattedDate_2); } public static void main(String[] args){ System.out.println("The required packages have been imported"); LocalDateTime date = LocalDateTime.now(); System.out.println("A LocalDateTime object has been defined"); System.out.println("\nThe first date format is:" +date); print_date_format(date); } }
输出
The required packages have been imported A LocalDateTime object has been defined The first date format is:2022-03-17T23:37:37.623304800 The second date format is:17/03/2022 The third date format is:Thursday, 17 Mar 2022
广告