JAVA学习笔记-日历程序

package myCalendar;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

/**
* 可视化日历
* @author iwang
*
*/
public class myCode {

  public static void main(String[] args) {

  System.out.println("请输入日期:(按照格式:2030-1-1)");

  Scanner str = new Scanner(System.in);

  String temp = str.nextLine();

    //String temp = "2016-11-13";

  DateFormat format = new SimpleDateFormat("yyyy-MM-dd");

  try {

    Date date = format.parse(temp);

    Calendar calendar = new GregorianCalendar();

    calendar.setTime(date);

    int day = calendar.get(Calendar.DATE);

    calendar.set(Calendar.DATE, 1);

      //System.out.println(calendar.get(Calendar.DAY_OF_WEEK)); //一号是周几?

      //int kong = calendar.get(Calendar.DAY_OF_WEEK)-1;

      //System.out.println(calendar.getActualMaximum(Calendar.DATE)); //最后一天为多少号?

    int maxDate = calendar.getActualMaximum(Calendar.DATE);

    System.out.println("日\t一\t二\t三\t四\t五\t六\t");


  for(int k=0;k<(calendar.get(Calendar.DAY_OF_WEEK)-1);k++){
    System.out.print(" "+"\t");
  }
      //System.out.println("日\t一\t二\t三\t四\t五\t六\t");
      //int j = 0;
  for(int i=1;i<=maxDate;i++){
    if(i!=day){
      System.out.print(i+"\t");
    }else
    if(i==day){
      System.out.print("["+i+"]\t");
    }
      //System.out.print(i+"\t");

      //j++;
      int w = calendar.get(Calendar.DAY_OF_WEEK);
      if(/*j%7==0*/w == Calendar.SATURDAY){
        System.out.println();
      }
    calendar.add(Calendar.DATE, 1);
  }

} catch (ParseException e) {

  e.printStackTrace();
  }

}

}

posted @ 2016-11-13 17:16  iamAnonymous  阅读(1029)  评论(0编辑  收藏  举报