Java SimpleDateFormat toPattern()方法及示例
SimpleDateFormat类 的 toPattern() 方法是用来返回日期格式的模式。
语法
public String toPattern()
参数: 该方法不接受任何参数。
返回值: 该方法返回描述该日期格式的模式字符串。
下面的程序说明了SimpleDateFormat的toPattern()方法的工作。
例1 :
// 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 AMThe Date Pattern- M/d/yy h:mm a
