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

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

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

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

包视图:

java.text Package
    DateFormat Class
        getDateTimeInstance() Method

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

句法:

public static final DateFormat getDateTimeInstance()

返回类型:该方法以特定格式返回日期/时间格式化程序。

示例 1:

Java
// Java Program to Illustrate the
// getDateTimeInstance() 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 by
        // creating object of DateFormat class
        DateFormat DFormat = DateFormat.getDateTimeInstance();
 
        System.out.println("Object: " + DFormat);
 
        // Formatting the string using format() method
        String str = DFormat.format(new Date());
 
        // Printing the formatted string time
        System.out.println(str);
    }
}


Java
// Java Program to Illustrate the
// getDateTimeInstance() 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
        SimpleDateFormat SDFormat
            = new SimpleDateFormat("MM/dd/yyyy");
 
        // Displaying the formats
        Date date = new Date();
        String str_Date1 = SDFormat.format(date);
 
        // Displaying the unformatted date
        System.out.println("The Original: " + (str_Date1));
 
        // Initializing the Time formatter
        DateFormat DFormat = DateFormat.getDateTimeInstance();
        System.out.println("Object: " + DFormat);
 
        // Formatting the string
        String str = DFormat.format(new Date());
 
        // Displaying the string time on console
        System.out.println(str);
    }
}


输出
Object: java.text.SimpleDateFormat@f95cf381
Jan 11, 2022, 6:25:40 AM

示例 2:

Java

// Java Program to Illustrate the
// getDateTimeInstance() 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
        SimpleDateFormat SDFormat
            = new SimpleDateFormat("MM/dd/yyyy");
 
        // Displaying the formats
        Date date = new Date();
        String str_Date1 = SDFormat.format(date);
 
        // Displaying the unformatted date
        System.out.println("The Original: " + (str_Date1));
 
        // Initializing the Time formatter
        DateFormat DFormat = DateFormat.getDateTimeInstance();
        System.out.println("Object: " + DFormat);
 
        // Formatting the string
        String str = DFormat.format(new Date());
 
        // Displaying the string time on console
        System.out.println(str);
    }
}
输出
The Original: 01/11/2022
Object: java.text.SimpleDateFormat@f95cf381
Jan 11, 2022, 6:30:09 AM