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

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

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

SimpleDateFormat 类toLocalizedPattern()方法用于返回描述此日期格式的本地化模式格式化字符串。换句话说,特定日期将转换为本地模式,例如M/d/yy h:mm a

句法:

public String toLocalizedPattern()

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

返回值:该方法返回日期格式化程序的本地化模式字符串。

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

示例 1:

// Java code to illustrate
// toLocalizedPattern() method
  
import java.text.*;
import java.util.Calendar;
  
public class SimpleDateFormat_Demo {
    public static void main(String[] args)
        throws InterruptedException
    {
        // Initializing date Formatter
        SimpleDateFormat SDFormat
            = new SimpleDateFormat();
  
        // Initializing the calender Object
        Calendar cal = Calendar.getInstance();
  
        // Displaying the date
        System.out.println("Date: "
                           + SDFormat.format(
                                 cal.getTime()));
  
        // Use of toLocalizedPattern() method
        System.out.println("In localized pattern: "
                           + SDFormat
                                 .toLocalizedPattern());
    }
}
输出:
Date: 1/29/19 8:02 AM
In localized pattern: M/d/yy h:mm a

示例 2:

// Java code to illustrate
// toLocalizedPattern() method
  
import java.text.*;
import java.util.Calendar;
  
public class SimpleDateFormat_Demo {
    public static void main(String[] args)
        throws InterruptedException
    {
        // Initializing date Formatter
        SimpleDateFormat SDFormat
            = new SimpleDateFormat();
  
        // Initializing the calender Object
        Calendar cal = Calendar.getInstance();
  
        // Displaying the date
        System.out.println("Date: "
                           + SDFormat
                                 .format(
                                     cal.getTime()));
  
        // Use of toLocalizedPattern() method
        System.out.println("In localized pattern: "
                           + SDFormat
                                 .toLocalizedPattern());
    }
}
输出:
Date: 1/29/19 12:46 PM
In localized pattern: M/d/yy h:mm a