以不同格式显示日历年日期的Java程序
由于不同的国家确实选择不同的格式。所以这里的目标只是打印不同年份的日历日期。全球通用的符号表示法是:Symbolic Notation Depicts y year M month in year d day in month E day of week
概念:无论何时归结为日期和时间,主要目标都是Date Class 。它 需要导入到我们的程序中。
Date Class 是在 JDK 1.0 版的Java中引入的,但后来在 JDK 1.1 中通过引入 Calendar Class 进行了改进。它弃用了大多数日历类,因为 6 个构造函数中有 4 个被标记为已弃用,并且其中的大多数方法都被标记为已弃用。这两个 JDK 现在已经过时,因此被称为遗留 API。最后,在 2014 年 JDK8 中引入了新的日期/时间 API。它包括Java.time 包,它现在是日期、时间、瞬间和持续时间的主要 API。Class Description Clock A clock providing access t the current instant, date, and time using a time zone Duration A time-based amount of time, such as ‘34.5 seconds’ Instant An instantaneous point on the time-line LocalDate A date without a time zine, calendar year such as 2007-12-03 LocalTime A time without a time zone in the calendar system MonthDay A month day in the calendar system OffsetTime A time with an offset from Greenwich in the calendar system Period A data-based amount of time in the calendar system Year A year in the calendar system ZonedDateTime A date with a time zone in a calendar system ZoneOffset A time zone offset from Greenwich
包Java.time.in JDK 8
我们可以通过不同的方式在Java中获取当前日期和时间。 Java没有内置的 Date 类,但我们可以导入Java.time 包来处理时间和日期 API。一些类如下:
- Java.time.LocalDate – 表示日期(年、月、日(yyyy-MM-dd))。
- Java.time.LocalDateTime – 表示日期和时间 (yyyy-MM-dd-HH-mm-ss-ns)。
- Java.time.format.DateTimeFormatter – 用于显示和解析日期时间对象的格式化程序。
When there is a need to display date and time in a different format, the ofPattern() method is used to accept all sorts of values. There are numerous formats that can be invoked later on can be mixed and match letters to achieve the required pattern.
这里将处理 3 种类型的格式:
- yyyy-MM-dd
- 日/月/年
- dd MMM yyyy
下面是不同日期格式的Java程序。
格式一:yyyy-MM-dd
Java
// Java Program to Display Dates in Different Format
// Importing Classes/Files
import java.io.*;
// Importing speccificaly Time Class and functionalities
import java.time.*;
class GFG {
// Main driver method
public static void main(String[] args)
{
// Create an object of LoacalDate type
LocalDate date = LocalDate.now();
// .now() method to tore the current date
// Print current date
System.out.println(date);
}
}
Java
// Java Program to Display Dates in Different Format
// Importing generic Classes/Files
import java.io.*;
// Importing specific Date and Time Classes/Files
import java.time.*;
import java.time.format.DateTimeFormatter;
class GFG {
// Main driver method
public static void main(String[] args)
{
// Create date time object and store current time in
// default format yy-mm-dd
LocalDateTime date = LocalDateTime.now();
// Creating DateTimeFormatter object
// to specify date format
DateTimeFormatter myDateFormat
= DateTimeFormatter.ofPattern("dd/MM/yyyy");
// Change date into your format and store it in
// string object
String formattedDate = date.format(myDateFormat);
// Print formatted date
System.out.println(formattedDate);
}
}
Java
// Java Program to Display Dates in Different Format
// Importing generic Classes/Files
import java.io.*;
// Importing Date and time specific Classes
import java.time.*;
import java.time.format.DateTimeFormatter;
class GFG {
// Main driver method
public static void main(String[] args)
{
// Create date time object and store current time in
// default format yy-mm-dd
LocalDateTime date = LocalDateTime.now();
// Specify the date format
// Note: more than 3 characters result in full name
DateTimeFormatter myDateFormat
= DateTimeFormatter.ofPattern(
"EEEE, dd MMM yyyy");
// e.g- MMM = Oct and MMMM = October
// Change date into req format and store it in
// string object
String formattedDate = date.format(myDateFormat);
// Printing formatted date
System.out.println(formattedDate);
}
}
输出:
2020-11-03
格式 2:DD/MM/YY
Java
// Java Program to Display Dates in Different Format
// Importing generic Classes/Files
import java.io.*;
// Importing specific Date and Time Classes/Files
import java.time.*;
import java.time.format.DateTimeFormatter;
class GFG {
// Main driver method
public static void main(String[] args)
{
// Create date time object and store current time in
// default format yy-mm-dd
LocalDateTime date = LocalDateTime.now();
// Creating DateTimeFormatter object
// to specify date format
DateTimeFormatter myDateFormat
= DateTimeFormatter.ofPattern("dd/MM/yyyy");
// Change date into your format and store it in
// string object
String formattedDate = date.format(myDateFormat);
// Print formatted date
System.out.println(formattedDate);
}
}
输出:
03/11/2020
格式 3:日,dd MMM yyyy
Java
// Java Program to Display Dates in Different Format
// Importing generic Classes/Files
import java.io.*;
// Importing Date and time specific Classes
import java.time.*;
import java.time.format.DateTimeFormatter;
class GFG {
// Main driver method
public static void main(String[] args)
{
// Create date time object and store current time in
// default format yy-mm-dd
LocalDateTime date = LocalDateTime.now();
// Specify the date format
// Note: more than 3 characters result in full name
DateTimeFormatter myDateFormat
= DateTimeFormatter.ofPattern(
"EEEE, dd MMM yyyy");
// e.g- MMM = Oct and MMMM = October
// Change date into req format and store it in
// string object
String formattedDate = date.format(myDateFormat);
// Printing formatted date
System.out.println(formattedDate);
}
}
输出:
Tuesday, 03 Nov 2020