提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
SimpleDateFormat使用自定义格式字符串格式化日期对象
/* from n o w j a v a . c o m*/ import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class LocalizationExamples { public static void main(String[] args) { /** * Run this, and try to guess the pattern that generated every output! */ SimpleDateFormat simpleDateFormat = null; String dateAsString = null; System.out.println( "Date 1:\n" ); simpleDateFormat = new SimpleDateFormat("dd/MM/yy"); /* from N o w J a v a . c o m*/ dateAsString = simpleDateFormat.format(new Date()); System.out.println(dateAsString); System.out.println( "Date 2:\n" ); simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z"); dateAsString = simpleDateFormat.format(new Date()); System.out.println(dateAsString); System.out.println( "Date 3:\n" ); simpleDateFormat = new SimpleDateFormat("EEE, MMM d, ''yy"); dateAsString = simpleDateFormat.format(new Date()); System.out.println(dateAsString); System.out.println( "Date 4:\n" ); simpleDateFormat = new SimpleDateFormat("h:mm a"); dateAsString = simpleDateFormat.format(new Date()); System.out.println(dateAsString); System.out.println( "Date 5:\n" ); simpleDateFormat = new SimpleDateFormat("hh 'o''clock' a, zzzz"); dateAsString = simpleDateFormat.format(new Date()); System.out.println(dateAsString); System.out.println( "Date 6:\n" ); simpleDateFormat = new SimpleDateFormat("K:mm a, z"); dateAsString = simpleDateFormat.format(new Date()); System.out.println(dateAsString); System.out.println( "Date 7:\n" ); simpleDateFormat = new SimpleDateFormat("yyyyy.MMMMM.dd GGG hh:mm aaa"); dateAsString = simpleDateFormat.format(new Date()); System.out.println(dateAsString); System.out.println( "Date 8:\n" ); simpleDateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); dateAsString = simpleDateFormat.format(new Date()); System.out.println(dateAsString); System.out.println( "Date 9:\n" ); simpleDateFormat = new SimpleDateFormat("yyMMddHHmmssZ"); dateAsString = simpleDateFormat.format(new Date()); System.out.println(dateAsString); System.out.println( "Date 10:\n" ); simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); dateAsString = simpleDateFormat.format(new Date()); System.out.println(dateAsString); System.out.println( "Date 11:\n" ); simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); dateAsString = simpleDateFormat.format(