Java中的 SimpleDateFormat applyPattern() 方法及示例
SimpleDateFormat 类的applyPattern()方法用于将给定的已定义模式设置为日期格式。它只是将特定日期和时间转换为用户定义的特定格式,例如 dd/ MM/ yyyy HH:mm Z 或 MM/ dd/ yyyy HH:mm Z。
句法:
public void applyPattern(String pattern)
参数:该方法采用一个字符串类型的参数模式,并引用此日期格式的新日期和时间模式。
返回值:该方法返回void类型。
下面的程序说明了 SimpleDateFormat 的 applyPattern() 方法的工作:
示例 1:
Java
// Java code to illustrate
// applyPattern() 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 the calendar Object
Calendar cal = Calendar.getInstance();
// Using the below pattern
String new_pat = "dd/ MM/ yyyy HH:mm Z";
// Use of applyPattern() method
SDFormat.applyPattern(new_pat);
// Displaying Current date and time
String curr_date
= SDFormat.format(cal.getTime());
System.out.println("The Current Date: "
+ curr_date);
// Displaying the pattern
System.out.println("Applied Pattern: "
+ SDFormat.toPattern());
}
}
Java
// Java code to illustrate
// applyPattern() 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 the calendar Object
Calendar cal = Calendar.getInstance();
// Using the below pattern
String new_pat = "MM/ dd/ yyyy HH:mm Z";
// Use of applyPattern() method
SDFormat.applyPattern(new_pat);
// Displaying Current date and time
String curr_date
= SDFormat.format(cal.getTime());
System.out.println("The Current Date: "
+ curr_date);
// Displaying the pattern
System.out.println("Applied Pattern: "
+ SDFormat.toPattern());
}
}
输出:
The Current Date: 29/ 01/ 2019 07:22 +0000
Applied Pattern: dd/ MM/ yyyy HH:mm Z
示例 2:
Java
// Java code to illustrate
// applyPattern() 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 the calendar Object
Calendar cal = Calendar.getInstance();
// Using the below pattern
String new_pat = "MM/ dd/ yyyy HH:mm Z";
// Use of applyPattern() method
SDFormat.applyPattern(new_pat);
// Displaying Current date and time
String curr_date
= SDFormat.format(cal.getTime());
System.out.println("The Current Date: "
+ curr_date);
// Displaying the pattern
System.out.println("Applied Pattern: "
+ SDFormat.toPattern());
}
}
输出:
The Current Date: 01/ 29/ 2019 07:22 +0000
Applied Pattern: MM/ dd/ yyyy HH:mm Z