package Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class DateFormatDemo {
/**
* 日期格式化
*/
public static void main(String[] args) {
//Date now = new Date();
//System.out.println(now);
//useDateFormat();
//useSimpleDateFormat();
useStringFormat();
}
public static void useStringFormat() {
long now = System.currentTimeMillis();
Date dd = new Date();
Calendar cal = Calendar.getInstance();
System.out.println(String.format("my age is %x", 100));
System.out.println(String.format("%tY", now));
System.out.println(String.format("%tY年%1$tm月",now));
System.out.println(String.format("%tY年% System.out.println(String.format("%tY年% System.out.println(String.format("%tF %
}
public static void useSimpleDateFormat() {
Date now = new Date();
//2009#05#07 [19]<=>[45]>>(26)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy#MM#dd [HH]<=>[mm]>>(ss)");
System.out.println(sdf.format(now));
sdf.applyPattern("yyyy年MM月dd日 HH:mm:ss");
System.out.println(sdf.format(now));
}
public static void useDateFormat() {
System.out.println("使用DateFormat格式化日期对象...");
Date now = new Date();
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL); //仅格式化日期
String str = df.format(now);
System.out.println(str);
df = DateFormat.getDateInstance(DateFormat.LONG,Locale.US);
System.out.println(df.format(now));
df = DateFormat.getDateInstance(DateFormat.MEDIUM);
System.out.println(df.format(now));
df = DateFormat.getDateInstance(DateFormat.SHORT);
System.out.println(df.format(now));
System.out.println("--------------------------------");
df = DateFormat.getTimeInstance(DateFormat.FULL);
System.out.println(df.format(now));
df = DateFormat.getTimeInstance(DateFormat.LONG,Locale.US);
System.out.println(df.format(now));
df = DateFormat.getTimeInstance(DateFormat.MEDIUM);
System.out.println(df.format(now));
df = DateFormat.getTimeInstance(DateFormat.SHORT);
System.out.println(df.format(now));
System.out.println("------------------------");
df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM);
System.out.println(df.format(now));
}
}
阅读(687) | 评论(0) | 转发(0) |