当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。