输入年月日,获取当月日历

来源:互联网 发布:mac口红是哪个国家的 编辑:程序博客网 时间:2024/05/21 05:41
<span style="font-family:Times New Roman;">import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.Scanner;//输入年月日,获取当月日历</span>
<span style="font-family:Times New Roman;">public class MyCalendar2{//public static final int YEAR_ =2000;//public static final int MONTH_ = 2;//public static final int DAY_ = 6;public static void main(String[] args) {//输入年、月、日System.out.println( "请输入   年、月、日:"  );Scanner in = new Scanner(System.in);int YEAR_= in.nextInt();int MONTH_= in.nextInt();int DAY_= in.nextInt();in.close();GregorianCalendar now = new GregorianCalendar();now.set(Calendar.YEAR, YEAR_);now.set(Calendar.MONTH, MONTH_-1);now.set(Calendar.DAY_OF_MONTH, DAY_);int year_ = now.get(Calendar.YEAR) ;int month_ = now.get(Calendar.MONTH)+1 ;    //不太明白为何加1才能得到所需月份?int day_ = now.get(Calendar.DAY_OF_MONTH) ;Date date = now.getTime();System.out.println( "date:"+date.toString()  );System.out.println( "  year_:"+year_+"  month_:"+month_+"  day_:"+ day_ );now.set(Calendar.DAY_OF_MONTH, 1);//该月第一天   day置为1int week = now.get(Calendar.DAY_OF_WEEK);// now 是一周的第几天System.out.println( " 是一周的第"+week+"天  ");System.out.println(" Sun   Mon   Tue   Wed   Thu   Fri   Sat");for (int i = Calendar.SUNDAY; i < week; i++) {System.out.print("      ");//找到当月1号的位置}while (now.get(Calendar.MONTH) == month_-1) {int day = now.get(Calendar.DAY_OF_MONTH);//从1号开始if (day < 10) {if (day == day_)System.out.print(" -" + day + "-  ");elseSystem.out.print("  " + day + "   ");} else {if (day == day_)System.out.print("-" + day + "-  ");elseSystem.out.print(" " + day + "   ");}if (week == Calendar.SATURDAY) {// 周六换行System.out.println();}now.add(Calendar.DAY_OF_MONTH, 1);week = now.get(Calendar.DAY_OF_WEEK);}}}</span>

0 0
原创粉丝点击