📜  以不同格式显示日历年日期的Java程序

📅  最后修改于: 2022-05-13 01:55:06.433000             🧑  作者: Mango

以不同格式显示日历年日期的Java程序

由于不同的国家确实选择不同的格式。所以这里的目标只是打印不同年份的日历日期。全球通用的符号表示法是:

Symbolic NotationDepicts
yyear
Mmonth in year 
dday in month 
Eday 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。

ClassDescription
ClockA clock providing access t the current instant, date, and time using a time zone
DurationA time-based amount of time, such as ‘34.5 seconds’
InstantAn instantaneous point on the time-line
LocalDateA date without a time zine, calendar year such as 2007-12-03 
LocalTimeA time without a time zone in the calendar system
MonthDayA month day in the calendar system
OffsetTimeA time with an offset from Greenwich in the calendar system
PeriodA data-based amount of time in the calendar system 
YearA year in the calendar system
ZonedDateTimeA date with a time zone in a calendar system
ZoneOffsetA 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 – 用于显示和解析日期时间对象的格式化程序。

这里将处理 3 种类型的格式:

  1. yyyy-MM-dd
  2. 日/月/年
  3. 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