傳入日期返回日期字符數字

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;
 }
發佈了81 篇原創文章 · 獲贊 12 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章