📜  Java中的 DateFormat format() 方法及示例

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

Java中的 DateFormat format() 方法及示例

Java.text 包中的 DateFormat 类是一个抽象类,用于格式化和解析任何语言环境的日期。它允许我们将日期格式化为文本并将文本解析为日期。 DateFormat 类提供了许多功能来获取、格式化、解析默认日期/时间。 DateFormat 类扩展了 Format 类,这意味着它是 Format 类的子类。由于 DateFormat 类是一个抽象类,因此它可以用于日期/时间格式化子类,它以独立于语言的方式格式化和解析日期或时间。

Java中 DateFormat 类的format()方法用于将给定日期格式化为日期/时间字符串。基本上,该方法用于将此日期和时间转换为特定格式,即 mm/dd/yyyy。

句法:

public final String format(Date date)

参数:该方法采用Date对象类型的一个参数date,指的是要产生其字符串输出的日期。

返回类型:以 mm/dd/yyyy字符串格式返回日期或时间。

示例 1:

Java
// Java Program to Illustrate format() Method
// of DateTime Class
 
// Importing required classes
import java.text.*;
import java.util.Calendar;
 
// Main class
// DateFormat_Demo
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Initializing the first formatter
        DateFormat DFormat = DateFormat.getDateInstance();
 
        // Initializing the calender Object
        Calendar cal = Calendar.getInstance();
 
        // Displaying the actual date
        System.out.println("The original Date: "
                           + cal.getTime());
 
        // Converting date using format() method
        String curr_date = DFormat.format(cal.getTime());
 
        // Printing the formatted date
        System.out.println("Formatted Date: " + curr_date);
    }
}


Java
// Java Program to Illustrate format() Method
// of DateTime Class
 
// Importing required classes
import java.text.*;
import java.util.*;
 
// Main class
// DateFormat_Demo
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Initializing the first formatter
        DateFormat DFormat = DateFormat.getDateTimeInstance(
            DateFormat.LONG, DateFormat.LONG,
            Locale.getDefault());
 
        // Initializing the calender Object
        Calendar cal = Calendar.getInstance();
 
        // Displaying the actual date
        System.out.println("The original Date: "
                           + cal.getTime());
 
        // Converting date using format() method and
        // storing date in a string
        String curr_date = DFormat.format(cal.getTime());
 
        // Printing the formatted date on console
        System.out.println("Formatted Date: " + curr_date);
    }
}


输出:
The original Date: Wed Mar 27 11:12:29 UTC 2019
Formatted Date: Mar 27, 2019

示例 2:

Java

// Java Program to Illustrate format() Method
// of DateTime Class
 
// Importing required classes
import java.text.*;
import java.util.*;
 
// Main class
// DateFormat_Demo
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Initializing the first formatter
        DateFormat DFormat = DateFormat.getDateTimeInstance(
            DateFormat.LONG, DateFormat.LONG,
            Locale.getDefault());
 
        // Initializing the calender Object
        Calendar cal = Calendar.getInstance();
 
        // Displaying the actual date
        System.out.println("The original Date: "
                           + cal.getTime());
 
        // Converting date using format() method and
        // storing date in a string
        String curr_date = DFormat.format(cal.getTime());
 
        // Printing the formatted date on console
        System.out.println("Formatted Date: " + curr_date);
    }
}
输出
The original Date: Tue Jan 11 05:42:29 UTC 2022
Formatted Date: January 11, 2022 at 5:42:29 AM UTC