📌  相关文章
📜  Java DateFormat getInstance() 方法及示例

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

Java DateFormat getInstance() 方法及示例

DateFormat 类的getInstance()方法将返回具有默认语言环境的默认格式样式的日期和时间格式化程序。

句法:

public static final DateFormat getInstance()

参数:此方法不需要任何参数。

返回值:此方法将返回特定格式的日期格式。

示例 1:

Java
// Java program to illustrate
// GetInstance() method
  
// importing the required packages
import java.text.DateFormat;
import java.util.Date;
  
class Testclass {
    public static void main(String[] args)
    {
  
        // initializing the Date
        Date d = new Date();
  
        // initializing the DateFormat using getInstance()
        DateFormat df = DateFormat.getInstance();
  
        // printing the DateFormat return value as object
        System.out.println("DateFormat Object : " + df);
  
        // formatting the current date into a string
        String str = df.format(d);
  
        // printing the current date
        System.out.println("Current date : " + str);
    }
}


Java
// Java program to illustrate
// GetInstance() method
  
// importing the required packages
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
  
class Testclass {
    public static void main(String[] args)
    {
        // initializing the Date
        Date d = new Date();
  
        // initializing the DateFormat using getInstance()
        DateFormat df = DateFormat.getInstance();
  
        // printing the DateFormat return value as object
        System.out.println("DateFormat Object : " + df);
  
        // formatting the current date into a string
        String str = df.format(d);
  
        // printing the current date
        System.out.println("Current date : " + str);
  
        // initializing the SimpleDateFormat
        SimpleDateFormat sdf
            = new SimpleDateFormat("MM-dd-yyyy");
  
        // formatting the SimpleDateFormatr into a string
        String st = sdf.format(d);
  
        // printing the formatted value
        System.out.println("Formatted Date : " + st);
    }
}


输出
DateFormat Object : java.text.SimpleDateFormat@c88bcc54
Current date : 12/15/21, 8:23 AM

我们还可以使用 SimpleDateFormat 类以所需的格式显示日期。

下面是显示使用 SimpleDateFormat 的示例

示例 2:

Java

// Java program to illustrate
// GetInstance() method
  
// importing the required packages
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
  
class Testclass {
    public static void main(String[] args)
    {
        // initializing the Date
        Date d = new Date();
  
        // initializing the DateFormat using getInstance()
        DateFormat df = DateFormat.getInstance();
  
        // printing the DateFormat return value as object
        System.out.println("DateFormat Object : " + df);
  
        // formatting the current date into a string
        String str = df.format(d);
  
        // printing the current date
        System.out.println("Current date : " + str);
  
        // initializing the SimpleDateFormat
        SimpleDateFormat sdf
            = new SimpleDateFormat("MM-dd-yyyy");
  
        // formatting the SimpleDateFormatr into a string
        String st = sdf.format(d);
  
        // printing the formatted value
        System.out.println("Formatted Date : " + st);
    }
}
输出
DateFormat Object : java.text.SimpleDateFormat@c88bcc54
Current date : 12/15/21, 8:26 AM
Formatted Date : 12-15-2021