public static void main(String[] args) throws Exception{
System.out.println(formatDate2(new Date()));
}
//xy年月日
public static String formatDate2(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.YEAR)
+ replenishZERO(calendar.get(Calendar.MONTH)+1)
+ replenishZERO(calendar.get(Calendar.DATE));
}
/**
* 对缺失0的字段进行补位
*/
private static String replenishZERO(int field){
String str = ""+field;
if (field < 10) {
str = "0" + field;
}
return str;
}
System.out.println(formatDate2(new Date()));
}
//xy年月日
public static String formatDate2(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.YEAR)
+ replenishZERO(calendar.get(Calendar.MONTH)+1)
+ replenishZERO(calendar.get(Calendar.DATE));
}
/**
* 对缺失0的字段进行补位
*/
private static String replenishZERO(int field){
String str = ""+field;
if (field < 10) {
str = "0" + field;
}
return str;
}