`

Calendar的用法

    博客分类:
  • java
 
阅读更多
public class CalendarDemo {   
11.11.    public static void main(String[] args) {   
12.12.        System.out.println("当前时间为:");   
13.13.        System.out.println(getCurrentTime());   
14.14.        System.out.println("当前时间一个月前的时间为:");   
15.15.        System.out.println(getOneMonthBeforeCurrentTime());   
16.16.        System.out.println("当前时间一个月后的时间为:");   
17.17.        System.out.println(getOneMonthAfterCurrentTime());   
18.18.        System.out.println("某月的最后一天时间为:");   
19.19.        System.out.println(getLastDayOfMonth());   
20.20.        System.out.println("今年的第一个周一为:");   
21.21.        System.out.println(getFirstMondayOfYear());   
22.22.        System.out.println("今年五月的第二个星期日为:");   
23.23.        System.out.println(getSecondSunDayOfMay());   
24.24.        System.out.print("第一周从:");   
25.25.        System.out.println(getFirstWeekOfYear());   
26.26.    }   
27.27.    // 获得系统当前时间     
28.28.    public static String getCurrentTime() {   
29.29.        Calendar cal = Calendar.getInstance();   
30.30.//      int year = cal.get(Calendar.YEAR);     
31.31.//      int month = cal.get(Calendar.MONTH) + 1;     
32.32.//      int day = cal.get(Calendar.DAY_OF_MONTH);     
33.33.//      return year + "-" + month + "-" + day;     
34.34.        return ""+cal.getTime();   
35.35.    }   
36.36.    // 获得当前时间一个月前的时间     
37.37.    public static String getOneMonthBeforeCurrentTime() {   
38.38.        Calendar cal = Calendar.getInstance();   
39.39.        cal.add(cal.MONTH, -1);   
40.40.           
41.41.        //roll()方法也可以     
42.42.//      cal.roll(Calendar.MONTH, -1);     
43.43.           
44.44.//      return cal.get(cal.YEAR) + "-" + (cal.get(cal.MONTH) + 1) + "-"     
45.45.//              + cal.get(cal.DAY_OF_MONTH);     
46.46.        return ""+cal.getTime();   
47.47.    }   
48.48.    // 获得当前时间一个月后的时间     
49.49.    public static String getOneMonthAfterCurrentTime() {   
50.50.        Calendar cal = Calendar.getInstance();   
51.51.        cal.add(cal.MONTH, 1);   
52.52.//      return cal.get(cal.YEAR) + "-" + (cal.get(cal.MONTH) + 1) + "-"     
53.53.//              + cal.get(cal.DAY_OF_MONTH);     
54.54.        return ""+cal.getTime();   
55.55.    }   
56.56.    // 取得某个月的最后一天     
57.57.    public static String getLastDayOfMonth() {   
58.58.        Calendar cal = Calendar.getInstance();   
59.59.        cal.set(2000, 1, 1);// 2000年2月1日     
60.60.        return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"   
61.61.                + cal.getActualMaximum(Calendar.DAY_OF_MONTH);   
62.62.    }   
63.63.    // 获得今年的第一个周一     
64.64.    public static String getFirstMondayOfYear() {   
65.65.        Calendar cal = Calendar.getInstance();   
66.66.        cal.set(cal.get(Calendar.YEAR), 0, 1);// 今年的一月一日     
67.67.//      while (true) {     
68.68.//          if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {     
69.69.//     
70.70.//              return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"     
71.71.//              + cal.get(Calendar.DAY_OF_MONTH);     
72.72.//     
73.73.//          } else {     
74.74.//              cal.add(Calendar.DAY_OF_WEEK, 1);     
75.75.//          }     
76.76.//      }     
77.77.        //如果weekDay =2 是周一     
78.78.        int weekDay = cal.get(Calendar.DAY_OF_WEEK);   
79.79.        int monDay=Calendar.MONDAY;   
80.80.        int sumDay = 0;   
81.81.        if (weekDay == monDay) {   
82.82.            sumDay = 0;   
83.83.        }    
84.84.        else{   
85.85.            sumDay=(7-weekDay+monDay);   
86.86.        }   
87.87.        cal.add(Calendar.DAY_OF_MONTH, sumDay);   
88.88.        return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"   
89.89.        + cal.get(Calendar.DAY_OF_MONTH);   
90.90.    }   
91.91.    //取得五月的第二个星期日     
92.92.    public static String getSecondSunDayOfMay(){   
93.93.        Calendar cal = Calendar.getInstance();   
94.94.        cal.set(cal.get(Calendar.YEAR), 4, 1);//日期设置为今年的5月1日     
95.95.        // 如果weekDay =1 是周日     
96.96.        int weekDay = cal.get(Calendar.DAY_OF_WEEK);   
97.97.        int sunDay=Calendar.SUNDAY;   
98.98.        int sumDay = 0;   
99.99.        if (weekDay == sunDay) {   
100.100.            sumDay = 7;   
101.101.        }    
102.102.        else{   
103.103.            sumDay=(7-weekDay+sunDay)+7;   
104.104.        }   
105.105.        cal.add(Calendar.DAY_OF_MONTH, sumDay);   
106.106.        return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"   
107.107.        + cal.get(Calendar.DAY_OF_MONTH);   
108.108.    }   
109.109.    //如何取得今年的第一周     
110.110.    /* 
111.111.     * 每年的第一个周开始的时间是这样计算的: 
112.112.     *  即:如果新年开始的1月1号在周四、周五、周六,则新年的第一周开始的时间是在新年的第二个周日; 
113.113.     *  否则,新年第一周开始的时间是在去年的最后一个周日; 
114.114.     */   
115.115.    public static String getFirstWeekOfYear(){   
116.116.        Calendar cal=Calendar.getInstance();   
117.117.//      cal.set(cal.get(Calendar.YEAR), 0,1);//设置时间为1月1日     
118.118.        cal.set(2013, 0,1);   
119.119.        int weekDay=cal.get(Calendar.DAY_OF_WEEK);   
120.120.        int sunDay=Calendar.SUNDAY;   
121.121.        int sumDay = 0;   
122.122.        //如果1月1号是周四、周五、周六     
123.123.        if((weekDay==5)||(weekDay==6)||(weekDay==7)){   
124.124.                sumDay=(7-weekDay+sunDay);   
125.125.            cal.add(Calendar.DAY_OF_MONTH, sumDay);   
126.126.            return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"   
127.127.            + cal.get(Calendar.DAY_OF_MONTH)+"开始,到"+cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"   
128.128.            + (cal.get(Calendar.DAY_OF_MONTH)+6)+"结束";   
129.129.        }   
130.130.        else if(weekDay==1){   
131.131.            return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"   
132.132.            + cal.get(Calendar.DAY_OF_MONTH)+"开始,到"+cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"   
133.133.            + (cal.get(Calendar.DAY_OF_MONTH)+6)+"结束";   
134.134.        }   
135.135.        else {   
136.136.            sumDay=(7-weekDay+sunDay);   
137.137.            cal.add(Calendar.DAY_OF_MONTH, -sumDay);   
138.138.            return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"   
139.139.            + cal.get(Calendar.DAY_OF_MONTH)+"开始,到"+cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"   
140.140.            + (cal.get(Calendar.DAY_OF_MONTH)+6)+"结束";   
141.141.        }   
142.142.           
143.143.    }   
144.144.}   


//得到当前时间前一个小时时间
Calendar upHour = Calendar.getInstance();   
upHour.add(upHour.HOUR, -1);
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics