Clander的用法。如何取得当前时间一个月后/前的时间。如何取得某个月的最后一天。 如何取得今年的第一个周一。五月的第二个星期日。 如何取得今年的第一周(注意国外的日历和国内可能不一样,如何设置一周

Clander的用法。如何取得当前时间一个月后/前的时间。如何取得某个月的最后一天。
如何取得今年的第一个周一。五月的第二个星期日。
如何取得今年的第一周(注意国外的日历和国内可能不一样,如何设置一周开始时间)

如何取得今年的第一周没能实现,需要修改代码。

 

01.package com.rj.yg;  
02.import java.util.Calendar;  
03./** 
04. *  
05. * @author 杨刚  
06. * Clander的用法。如何取得当前时间一个月后/前的时间。如何取得某个月的最后一天。 如何取得今年的第一个周一。五月的第二个星期日。 
07. * 如何取得今年的第一周(注意国外的日历和国内可能不一样,如何设置一周开始时间) 
08. *  Jul 28, 2010 
09. */  
10.public class CalendarDemo {  
11.    public static void main(String[] args) {  
12.        System.out.println("当前时间为:");  
13.        System.out.println(getCurrentTime());  
14.        System.out.println("当前时间一个月前的时间为:");  
15.        System.out.println(getOneMonthBeforeCurrentTime());  
16.        System.out.println("当前时间一个月后的时间为:");  
17.        System.out.println(getOneMonthAfterCurrentTime());  
18.        System.out.println("某月的最后一天时间为:");  
19.        System.out.println(getLastDayOfMonth());  
20.        System.out.println("今年的第一个周一为:");  
21.        System.out.println(getFirstMondayOfYear());  
22.        System.out.println("今年五月的第二个星期日为:");  
23.        System.out.println(getSecondSunDayOfMay());  
24.        System.out.print("第一周从:");  
25.        System.out.println(getFirstWeekOfYear());  
26.    }  
27.    // 获得系统当前时间   
28.    public static String getCurrentTime() {  
29.        Calendar cal = Calendar.getInstance();  
30.//      int year = cal.get(Calendar.YEAR);   
31.//      int month = cal.get(Calendar.MONTH) + 1;   
32.//      int day = cal.get(Calendar.DAY_OF_MONTH);   
33.//      return year + "-" + month + "-" + day;   
34.        return ""+cal.getTime();  
35.    }  
36.    // 获得当前时间一个月前的时间   
37.    public static String getOneMonthBeforeCurrentTime() {  
38.        Calendar cal = Calendar.getInstance();  
39.        cal.add(cal.MONTH, -1);  
40.          
41.        //roll()方法也可以   
42.//      cal.roll(Calendar.MONTH, -1);   
43.          
44.//      return cal.get(cal.YEAR) + "-" + (cal.get(cal.MONTH) + 1) + "-"   
45.//              + cal.get(cal.DAY_OF_MONTH);   
46.        return ""+cal.getTime();  
47.    }  
48.    // 获得当前时间一个月后的时间   
49.    public static String getOneMonthAfterCurrentTime() {  
50.        Calendar cal = Calendar.getInstance();  
51.        cal.add(cal.MONTH, 1);  
52.//      return cal.get(cal.YEAR) + "-" + (cal.get(cal.MONTH) + 1) + "-"   
53.//              + cal.get(cal.DAY_OF_MONTH);   
54.        return ""+cal.getTime();  
55.    }  
56.    // 取得某个月的最后一天   
57.    public static String getLastDayOfMonth() {  
58.        Calendar cal = Calendar.getInstance();  
59.        cal.set(2000, 1, 1);// 2000年2月1日   
60.        return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"  
61.                + cal.getActualMaximum(Calendar.DAY_OF_MONTH);  
62.    }  
63.    // 获得今年的第一个周一   
64.    public static String getFirstMondayOfYear() {  
65.        Calendar cal = Calendar.getInstance();  
66.        cal.set(cal.get(Calendar.YEAR), 0, 1);// 今年的一月一日   
67.//      while (true) {   
68.//          if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {   
69.//   
70.//              return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"   
71.//              + cal.get(Calendar.DAY_OF_MONTH);   
72.//   
73.//          } else {   
74.//              cal.add(Calendar.DAY_OF_WEEK, 1);   
75.//          }   
76.//      }   
77.        //如果weekDay =2 是周一   
78.        int weekDay = cal.get(Calendar.DAY_OF_WEEK);  
79.        int monDay=Calendar.MONDAY;  
80.        int sumDay = 0;  
81.        if (weekDay == monDay) {  
82.            sumDay = 0;  
83.        }   
84.        else{  
85.            sumDay=(7-weekDay+monDay);  
86.        }  
87.        cal.add(Calendar.DAY_OF_MONTH, sumDay);  
88.        return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"  
89.        + cal.get(Calendar.DAY_OF_MONTH);  
90.    }  
91.    //取得五月的第二个星期日   
92.    public static String getSecondSunDayOfMay(){  
93.        Calendar cal = Calendar.getInstance();  
94.        cal.set(cal.get(Calendar.YEAR), 4, 1);//日期设置为今年的5月1日   
95.        // 如果weekDay =1 是周日   
96.        int weekDay = cal.get(Calendar.DAY_OF_WEEK);  
97.        int sunDay=Calendar.SUNDAY;  
98.        int sumDay = 0;  
99.        if (weekDay == sunDay) {  
100.            sumDay = 7;  
101.        }   
102.        else{  
103.            sumDay=(7-weekDay+sunDay)+7;  
104.        }  
105.        cal.add(Calendar.DAY_OF_MONTH, sumDay);  
106.        return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"  
107.        + cal.get(Calendar.DAY_OF_MONTH);  
108.    }  
109.    //如何取得今年的第一周   
110.    /* 
111.     * 每年的第一个周开始的时间是这样计算的: 
112.     *  即:如果新年开始的1月1号在周四、周五、周六,则新年的第一周开始的时间是在新年的第二个周日; 
113.     *  否则,新年第一周开始的时间是在去年的最后一个周日; 
114.     */  
115.    public static String getFirstWeekOfYear(){  
116.        Calendar cal=Calendar.getInstance();  
117.//      cal.set(cal.get(Calendar.YEAR), 0,1);//设置时间为1月1日   
118.        cal.set(2013, 0,1);  
119.        int weekDay=cal.get(Calendar.DAY_OF_WEEK);  
120.        int sunDay=Calendar.SUNDAY;  
121.        int sumDay = 0;  
122.        //如果1月1号是周四、周五、周六   
123.        if((weekDay==5)||(weekDay==6)||(weekDay==7)){  
124.                sumDay=(7-weekDay+sunDay);  
125.            cal.add(Calendar.DAY_OF_MONTH, sumDay);  
126.            return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"  
127.            + cal.get(Calendar.DAY_OF_MONTH)+"开始,到"+cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"  
128.            + (cal.get(Calendar.DAY_OF_MONTH)+6)+"结束";  
129.        }  
130.        else if(weekDay==1){  
131.            return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"  
132.            + cal.get(Calendar.DAY_OF_MONTH)+"开始,到"+cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"  
133.            + (cal.get(Calendar.DAY_OF_MONTH)+6)+"结束";  
134.        }  
135.        else {  
136.            sumDay=(7-weekDay+sunDay);  
137.            cal.add(Calendar.DAY_OF_MONTH, -sumDay);  
138.            return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"  
139.            + cal.get(Calendar.DAY_OF_MONTH)+"开始,到"+cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"  
140.            + (cal.get(Calendar.DAY_OF_MONTH)+6)+"结束";  
141.        }  
142.          
143.    }  
144.}  

转载链接: http://blog.csdn.net/gang00ge/article/details/5776049
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值