當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java SimpleDateFormat toLocalizedPattern()用法及代碼示例


SimpleDateFormat類的toLocalizedPattern()方法用於返回描述此日期格式的本地化模式格式化的字符串。換句話說,將特定日期轉換為本地模式,例如M /d /yy h:mm a。

用法:

public String toLocalizedPattern()

參數:該方法不帶任何參數。


返回值:該方法返回日期格式化程序的本地化模式String。

以下示例程序旨在說明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


相關用法


注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 SimpleDateFormat toLocalizedPattern() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。