标签:style blog http color io os java ar for
1 package com.zhen.calendar; 2 3 import java.text.DateFormat; 4 import java.text.SimpleDateFormat; 5 import java.util.Calendar; 6 import java.util.Date; 7 import java.util.GregorianCalendar; 8 import java.util.Scanner; 9 10 /* 11 * 输入一个日期,控制台显示 12 * 格式:20140808 13 */ 14 15 public class CalendarMain { 16 public static void main(String[] argus) 17 { 18 System.out.println("请输入一个日期(格式:20140808): "); 19 20 Scanner scanner = new Scanner(System.in); 21 String sTemp = scanner.nextLine(); //获取输入字符 22 23 CalendarMain calendar = new CalendarMain(); 24 calendar.myCalendar(sTemp); 25 26 scanner.close(); //关闭输入流 27 } 28 29 private void myCalendar(String sTestDate) 30 { 31 DateFormat df = new SimpleDateFormat("yyyyMMdd"); //检查格式化 32 try{ 33 Date date = df.parse(sTestDate); 34 Calendar calendar = new GregorianCalendar(); 35 calendar.setTime(date); 36 int iYear = calendar.get(Calendar.YEAR); //获取输入的年份 37 int iMonth = calendar.get(Calendar.MONTH); //获取输入的月份 38 int iDate = calendar.get(Calendar.DATE); //获取输入的日期 39 40 calendar.set(Calendar.DATE, 1); //获取输入月份1号 41 int iFirstDay = calendar.get(Calendar.DAY_OF_WEEK); //获取输入月份1号是星期几 42 int iMaxDate = calendar.getActualMaximum(Calendar.DATE); //获取输入月份的最大天数 43 44 /* 45 //获取本地时间 46 calendar.setTime(new Date()); 47 int iCurYear = calendar.get(Calendar.YEAR); 48 int iCurMonth = calendar.get(Calendar.MONTH); 49 int iCurDate = -1; 50 if(iCurYear == iYear && iCurMonth == iMonth) 51 { 52 iCurDate = calendar.get(Calendar.DATE); 53 } 54 */ 55 56 System.out.println("-- "+ iYear +"年"+(iMonth+1)+"月"+"\n"); //月份从0开始 57 System.out.println("日\t一\t二\t三\t四\t五\t六"); 58 59 int j=iFirstDay; 60 for(int i=1;i<=iMaxDate;i++) 61 { 62 if(i == 1 && i<iFirstDay) //1号时,判定前面空多少‘\t‘ 63 { 64 int iTemp = i; 65 while(iTemp < iFirstDay) 66 { 67 System.out.print(‘\t‘); 68 iTemp++; 69 } 70 } 71 String sDate = i<10 ? "0"+i : i+ ""; //格式化日期 72 if(iDate == i) 73 { 74 sDate += "**"; //当前输入的日期加** 75 } 76 System.out.print(sDate +"\t"); 77 78 if(j%7==0) //换行 79 { 80 System.out.println(); 81 } 82 j++; 83 } 84 } 85 catch(Exception e) 86 { 87 88 } 89 90 } 91 92 }
标签:style blog http color io os java ar for
原文地址:http://www.cnblogs.com/fancyzhen/p/3975660.html