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

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

Java中的 SimpleDateFormat toPattern() 方法及示例

SimpleDateFormat 类toPattern()方法用于返回日期格式的模式。

句法:

public String toPattern()

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

返回值:该方法返回描述此日期格式的模式字符串。

下面的程序说明了 SimpleDateFormat 的 toPattern() 方法的工作:

示例 1:

Java
// Java code to illustrate toPattern() method
 
import java.text.*;
import java.util.Calendar;
 
public class SimpleDateFormat_Demo {
    public static void main(String[] args)
        throws InterruptedException
    {
        SimpleDateFormat SDformat
            = new SimpleDateFormat();
 
        // Initializing Calendar object
        Calendar cal = Calendar.getInstance();
 
        // Getting the Current Date
        String Todaysdate
            = SDformat.format(cal.getTime());
 
        // Displaying the date
        System.out.println("Current Date: "
                           + Todaysdate);
 
        // Using toPattern() method
        // to Print the Date Pattern
        System.out.println("The Date Pattern- "
                           + SDformat.toPattern());
    }
}


输出:
Current Date: 1/29/19 6:05 AM
The Date Pattern- M/d/yy h:mm a