使用Calendar對年月日的輸出

package entity;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
public class showdate {
 static int y;
 static int m;
 static int d1;
 public showdate() {
 }
 private static boolean yun(int year) {
  if (year%4==0&&year%400!=0){
   return true;}  
  else{
   return false;}
 }
 
 public  static void main(String[] args) throws ParseException {
  SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
  Date date=f.parse("2004-2-2");
  Calendar now = Calendar.getInstance();
  now.setTime(date);
       y=now.get(Calendar.YEAR);
       m=now.get(Calendar.MONTH)+1;
       d1=now.get(Calendar.DATE);
  int[] a = { 1, 3, 5, 7, 8, 10, 12 };
  int[] b = { 4, 6, 9, 11 };

  for (int i = 0; i < a.length; i++) {
   if (m == a[i]) {
    d1 = 31;
    break;
   }
  }
  for (int i = 0; i < b.length; i++) {
   if (m == b[i]) {
    d1 = 30;
    break;
   }
  }
  if (yun(y)==true && m == 2) {
    d1 = 29;
  }

  if (yun(y)==false && m == 2) {
   d1 = 28;
  }
  System.out.print( y+ "/" + m + "/" + d1);
 }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章