//日历源代码 import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Scanner; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; public class Calendar1 { public void show(String temp){ Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String dateNowStr = sdf.format(d); if(temp==null) temp=dateNowStr; calen(temp); } public static void calen(String temp) { 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)-1; calendar.set(calendar.DATE, 1); int oneday=calendar.get(calendar.DAY_OF_WEEK); int maxDay=calendar.getActualMaximum(calendar.DATE); System.out.println(temp); System.out.println("日\t一\t二\t三\t四\t五\t六"); for(int j=0;j<oneday-1;j++) System.out.print(" \t"); for (int i = 0; i < maxDay; i++) { if(i==day){ System.out.print("*"); } System.out.print(i+1+"\t"); int w=calendar.get(calendar.DAY_OF_WEEK); if(w==calendar.SATURDAY)System.out.println(); calendar.add(calendar.DATE, 1); } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { Calendar1 test1=new Calendar1(); test1.show(null); while(true){ System.out.println("\n请输入日期:(格式)XXXX-XX-XX"); Scanner scanner=new Scanner(System.in); String inputstr= scanner.nextLine(); System.out.println(inputstr); if(inputstr.matches("^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$")) test1.show(inputstr); else System.out.println("输入错误,请重新输入");} } }
时间: 2024-10-12 02:09:07