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

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

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

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

包视图:

java.text Package
    DateFormat Class
        getTimeInstance() Method

getTimeInstance() Java中DateFormat 类的方法用于获取时间格式。这个时间格式化程序带有默认语言环境的默认格式化样式。

句法:

public static final DateFormat getTimeInstance()

参数:该方法不带任何参数。

返回类型:该方法返回以特定格式格式化的时间。

示例 1:

Java
// Java Program to Illustrate getTimeInstance() 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.getTimeInstance();
 
        System.out.println("Object: " + DFormat);
 
        // Formatting the string using format() method
        String str = DFormat.format(new Date());
 
        // Displaying the formatted string time on console
        System.out.println(str);
    }
}


Java
// Java Program to Illustrate getTimeInstance() 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 simple date format mm/dd/yyyy
        // by creating object of SimpleDateFormat class
        SimpleDateFormat SDFormat
            = new SimpleDateFormat("MM/dd/yyyy");
 
        // Displaying the formats
        Date date = new Date();
        String str_Date1 = SDFormat.format(date);
 
        // Displaying original date
        System.out.println("The Original: " + (str_Date1));
 
        // Initializing the Time formatter
        DateFormat DFormat = DateFormat.getTimeInstance();
 
        System.out.println("Object: " + DFormat);
 
        // Formatting the string using format() method
        String str = DFormat.format(new Date());
 
        // Displaying the formatted string time on console
        System.out.println(str);
    }
}


输出
Object: java.text.SimpleDateFormat@8400729
6:39:49 AM

示例 2:

Java

// Java Program to Illustrate getTimeInstance() 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 simple date format mm/dd/yyyy
        // by creating object of SimpleDateFormat class
        SimpleDateFormat SDFormat
            = new SimpleDateFormat("MM/dd/yyyy");
 
        // Displaying the formats
        Date date = new Date();
        String str_Date1 = SDFormat.format(date);
 
        // Displaying original date
        System.out.println("The Original: " + (str_Date1));
 
        // Initializing the Time formatter
        DateFormat DFormat = DateFormat.getTimeInstance();
 
        System.out.println("Object: " + DFormat);
 
        // Formatting the string using format() method
        String str = DFormat.format(new Date());
 
        // Displaying the formatted string time on console
        System.out.println(str);
    }
}
输出
The Original: 01/11/2022
Object: java.text.SimpleDateFormat@8400729
6:45:24 AM