Java SimpleDateFormat toLocalizedPattern()方法及示例

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 calendar 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 calendar 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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程