1 PackageCom.zhen.calendar;2 3 ImportJava.text.DateFormat;4 ImportJava.text.SimpleDateFormat;5 ImportJava.util.Calendar;6 Importjava.util.Date;7 ImportJava.util.GregorianCalendar;8 ImportJava.util.Scanner;9 Ten /* One * Enter a date, console display A * Format: 20140808 - */ - the Public classCalendarmain { - Public Static voidMain (string[] Argus) - { -System.out.println ("Please enter a date (format: 20140808):"); + -Scanner Scanner =NewScanner (system.in); +String stemp = Scanner.nextline ();//Get input characters A atCalendarmain Calendar =NewCalendarmain (); - Calendar.mycalendar (stemp); - -Scanner.close ();//close the input stream - } - in Private voidMyCalendar (String stestdate) - { toDateFormat DF =NewSimpleDateFormat ("YyyyMMdd");//Check Formatting + Try{ -Date Date =Df.parse (stestdate); theCalendar Calendar =NewGregorianCalendar (); * calendar.settime (date); $ intIyear = Calendar.get (calendar.year);//get the year enteredPanax Notoginseng intImonth = Calendar.get (calendar.month);//get the month entered - intIDate = Calendar.get (calendar.date);//get the date entered the +Calendar.set (calendar.date, 1);//get input Month number 1th A intIfirstday = Calendar.get (Calendar.day_of_week);//get Input Month 1th number is the day of the week the intImaxdate = Calendar.getactualmaximum (calendar.date);//get the maximum number of days to enter a month + - /* $ //Get local time $ Calendar.settime (New Date ()); - int icuryear = Calendar.get (calendar.year); - int icurmonth = Calendar.get (calendar.month); the int icurdate =-1; - if (icuryear = = Iyear && Icurmonth = = imonth)Wuyi { the icurdate = Calendar.get (calendar.date); - } Wu */ - AboutSystem.out.println ("--" + Iyear + "year" + (imonth+1) + "month" + "\ n");//month starts from 0 $System.out.println ("t \ t two \ t three \ Four \ five \ t six"); - - intj=Ifirstday; - for(inti=1;i<=imaxdate;i++) A { + if(i = = 1 && i<ifirstday)//number 1th, determine the front empty how much ' \ t ' the { - intITemp =i; $ while(ITemp <ifirstday) the { theSystem.out.print (' \ t '); theitemp++; the } - } inString sdate = i<10? "0" +i:i+ "";//formatted Date the if(IDate = =i) the { AboutSdate + = "* *";//the current input date plus * * the } theSystem.out.print (sdate + "\ T"); the + if(j%7==0)//line Break - { the System.out.println ();Bayi } theJ + +; the } - } - Catch(Exception e) the { the the } the - } the the}
Java Calendar Model: Enter a date, console display