Java中的 SimpleDateFormat applyLocalizedPattern() 方法及示例
SimpleDateFormat 类的applyLocalizedPattern()方法用于将本地化字符串模式应用于此日期格式。该模式可由用户设置。
句法:
public void applyLocalizedPattern(String pattern)
参数:该方法采用一种字符串类型的参数模式,并引用要应用的新日期和时间。
返回值:该方法返回void类型。
下面的程序说明了 SimpleDateFormat 的 applyLocalizedPattern() 方法的工作:
示例 1:
Java
// Java code to illustrate
// applyLocalizedPattern() 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();
// Using the following pattern
String new_pat = "MM / dd / yy HH:mm Z";
// Use of applyLocalizedPattern()
SDFormat.applyLocalizedPattern(new_pat);
System.out.println("Applying the format: "
+ SDFormat
.toLocalizedPattern());
}
}
Java
// Java code to illustrate
// applyLocalizedPattern() method
import java.text.*;
import java.util.*;
public class SimpleDateFormat_Demo {
public static void main(String args[])
throws Exception
{
// Initializing SDF
SimpleDateFormat SDFormat
= new SimpleDateFormat();
// Applying LocalizedPattern
SDFormat.applyLocalizedPattern("dd");
String str = SDFormat.format(new Date());
// Printing todays date
System.out.println("Today is: " + str);
// Applying LocalizedPattern
SDFormat.applyLocalizedPattern("MMM");
String str1 = SDFormat.format(new Date());
// Printing the month
System.out.println("Month is: " + str1);
}
}
输出:
Applying the format: MM / dd / yy HH:mm Z
示例 2:
Java
// Java code to illustrate
// applyLocalizedPattern() method
import java.text.*;
import java.util.*;
public class SimpleDateFormat_Demo {
public static void main(String args[])
throws Exception
{
// Initializing SDF
SimpleDateFormat SDFormat
= new SimpleDateFormat();
// Applying LocalizedPattern
SDFormat.applyLocalizedPattern("dd");
String str = SDFormat.format(new Date());
// Printing todays date
System.out.println("Today is: " + str);
// Applying LocalizedPattern
SDFormat.applyLocalizedPattern("MMM");
String str1 = SDFormat.format(new Date());
// Printing the month
System.out.println("Month is: " + str1);
}
}
输出:
Today is: 30
Month is: Jan