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; /** * 可视化日历程序 * */ public class VisualCalendar { public static void main(String[] args) { System.out.println("请输入日期(按照格式:2015-08-17):"); Scanner scanner = new Scanner(System.in); String temp=scanner.nextLine(); //String temp ="2015-8-17"; DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); try { Date date = format.parse(temp); //Date 转换成Calendar Calendar calender = new GregorianCalendar(); calender.setTime(date); int day = calender.get(Calendar.DATE); calender.set(Calendar.DATE, 1); int dat_week =calender.get(Calendar.DAY_OF_WEEK); int maxDate=calender.getActualMaximum(Calendar.DATE); System.out.println("日\t一\t二\t三\t四\t五\t六"); for(int i=1;i<dat_week;i++){ System.out.print('\t'); } for(int i=1;i<=maxDate;i++) { if(i==day){ System.out.print("*"); } System.out.print(i+"\t"); int w = calender.get(Calendar.DAY_OF_WEEK); if(w==Calendar.SATURDAY) { System.out.print('\n'); } calender.add(Calendar.DATE, 1); } scanner.close(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240826070647288-243310440.jpg)