PoeticalJustice

导航

练习:可视化日历(时间处理相关类)

 1 package cn.bjsxt.dateclass;
 2 
 3 import java.security.SignatureException;
 4 import java.text.DateFormat;
 5 import java.text.ParseException;
 6 import java.text.SimpleDateFormat;
 7 import java.util.Calendar;
 8 import java.util.Date;
 9 import java.util.GregorianCalendar;
10 import java.util.Scanner;
11 
12 /**
13  * 可视化日历程序
14  * 
15  * @author Administrator
16  *
17  */
18 public class VisualCalendar {
19     public static void main(String[] args) {
20         System.out.println("请输入日期,按照格式:(2030-3-10)");
21         Scanner scanner = new Scanner(System.in);
22         String temp = scanner.nextLine();
23         
24         DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
25         try {
26             Date date = format.parse(temp);
27             Calendar calendar = new GregorianCalendar();
28             calendar.setTime(date);
29             int day = calendar.get(Calendar.DATE);
30             calendar.set(Calendar.DATE,1);
31 //            //每个月的日期是 周几
32 //            System.out.println(calendar.get(Calendar.DAY_OF_WEEK));
33 //            //每月的最大天数
34 //            System.out.println(calendar.getActualMaximum(Calendar.DATE));
35             
36             int maxDate = calendar.getActualMaximum(Calendar.DATE);
37             System.out.println("日\t一\t二\t三\t四\t五\t六");
38             
39             for(int i=1;i<calendar.get(Calendar.DAY_OF_WEEK);i++){
40                 System.out.print("\t");
41             }
42             
43                 
44             for(int i=1;i<=maxDate;i++){
45                 if(i==day){
46                     System.out.print("*");
47                 }
48                 System.out.print(i+"\t");
49                 int w=calendar.get(Calendar.DAY_OF_WEEK);
50                 if(w==Calendar.SATURDAY){
51                     System.out.print('\n');
52                 }
53                 calendar.add(Calendar.DATE, 1);
54             }
55             
56         } catch (ParseException e) {
57             
58             e.printStackTrace();
59         }
60     }
61 }

 

posted on 2017-10-06 01:20  PoeticalJustice  阅读(248)  评论(0编辑  收藏  举报