📌  相关文章
📜  Java中的 DateFormat getDateInstance() 方法及示例

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

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

Java.text 包的 DateFormat 类是一个抽象类,用于格式化和解析任何语言环境的日期。它允许我们将日期格式化为文本并将文本解析为日期。 DateFormat 类提供了许多功能来获取、格式化、解析默认日期/时间。

包视图:

java.text Package
    DateFormat Class
        getDateInstance() Method

getDateInstance() Java中DateFormat 类的方法用于获取日期格式化程序。此日期格式化程序带有默认语言环境的默认格式化样式。

句法:

public static final DateFormat getDateInstance()

返回类型:返回以特定格式格式化的日期。

示例 1:

Java
// Java Program to Illustrate getDateInstance() Method
//  of DateFormat Class
 
// Importing required classes
import java.text.*;
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] argv)
    {
        // Initializing the first formatter
        // using getDateInstance() method
        DateFormat DFormat = DateFormat.getDateInstance();
 
        System.out.println("Object: " + DFormat);
 
        // Formatting the string
        String str = DFormat.format(new Date());
 
        // Printing the string date on console
        System.out.println(str);
    }
}


Java
// Java Program to Illustrate getDateInstance() Method
// of DateFormat Class
 
// Importing required classes
import java.text.*;
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
        throws InterruptedException
    {
 
        // Initializing SimpleDateFormat by
        // creating object of SimpleDateFormat class
        SimpleDateFormat SDFormat
            = new SimpleDateFormat("MM/dd/yyyy");
 
        // Printing the formats
        Date date = new Date();
        String str_Date1 = SDFormat.format(date);
 
        // Displaying  the original date
        System.out.println("The Original: " + (str_Date1));
 
        // Initializing the Date formatter
        DateFormat DFormat = DateFormat.getDateInstance();
        System.out.println("Object: " + DFormat);
 
        // Formatting the string
        String str = DFormat.format(new Date());
 
        // Printing the string date on console
        System.out.println(str);
    }
}


输出:
Object: java.text.SimpleDateFormat@ce9bf0a5
Mar 27, 2019

示例 2:

Java

// Java Program to Illustrate getDateInstance() Method
// of DateFormat Class
 
// Importing required classes
import java.text.*;
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
        throws InterruptedException
    {
 
        // Initializing SimpleDateFormat by
        // creating object of SimpleDateFormat class
        SimpleDateFormat SDFormat
            = new SimpleDateFormat("MM/dd/yyyy");
 
        // Printing the formats
        Date date = new Date();
        String str_Date1 = SDFormat.format(date);
 
        // Displaying  the original date
        System.out.println("The Original: " + (str_Date1));
 
        // Initializing the Date formatter
        DateFormat DFormat = DateFormat.getDateInstance();
        System.out.println("Object: " + DFormat);
 
        // Formatting the string
        String str = DFormat.format(new Date());
 
        // Printing the string date on console
        System.out.println(str);
    }
}
输出
The Original: 01/11/2022
Object: java.text.SimpleDateFormat@ad508834
Jan 11, 2022