欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java的Calendar类

程序员文章站 2022-05-02 15:52:07
...
public class TestCalendar { 
2. 
3.    @Test 
4.    public void test(){ 
5.        try { 
6.            Calendar calendar = new GregorianCalendar();//子类实例化 
7.            //获取年、月、日、时、分、秒、毫秒 
8.            System.out.println("年: "+calendar.get(Calendar.YEAR)); 
9.            System.out.println("月 "+(calendar.get(Calendar.MONTH)+1)); 
10.            System.out.println("日: "+calendar.get(Calendar.DAY_OF_MONTH)); 
11.            System.out.println("时: "+calendar.get(Calendar.HOUR_OF_DAY)); 
12.            System.out.println("分: "+calendar.get(Calendar.MINUTE)); 
13.            System.out.println("秒: "+calendar.get(Calendar.SECOND)); 
14.            System.out.println("毫秒 "+calendar.get(Calendar.MILLISECOND)); 
15.             
16.            // 当前月第一天、最后一天 
17.            int currYear = calendar.get(Calendar.YEAR); 
18.            int currMonth = calendar.get(Calendar.MONTH) + 1; 
19.            System.out.print(currYear + "-" + currMonth); 
20.            SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd"); 
21.            calendar.set(Calendar.DAY_OF_MONTH, 1); 
22.            Date beginTime = calendar.getTime(); 
23.            String sTime = datef.format(beginTime) + " 00:00:00"; 
24.            calendar.set(Calendar.DATE, 1); 
25.            calendar.roll(Calendar.DATE, -1); 
26.            Date endTime = calendar.getTime(); 
27.            String eTime = datef.format(endTime) + " 23:59:59"; 
28.            System.out.println("第一天"+sTime+"最后一天"+eTime); 
29.             
30.            Calendar cal = Calendar.getInstance();  
31.            //指定年月的的第一天、最后一天 
32.            int year = 2015; 
33.            int moth = 3; 
34.            cal.set(Calendar.YEAR,year); 
35.            cal.set(Calendar.MONTH, moth-1); 
36.            //当前月的最后一天    
37.            cal.set( Calendar.DATE, 1 ); 
38.            cal.roll(Calendar.DATE, - 1 ); 
39.            Date endTime2=cal.getTime(); 
40.            String endTimeStr=datef.format(endTime2)+" 23:59:59"; 
41.            //当前月的第一天           
42.            cal.set(GregorianCalendar.DAY_OF_MONTH, 1);  
43.            Date beginTime2=cal.getTime(); 
44.            String beginTimeStr=datef.format(beginTime2)+" 00:00:00"; 
45.             
46.            System.out.println("指定年月的的第一天"+endTimeStr+"最后一天"+beginTimeStr); 
47.             
48.            //当前时间的前一天 
49.            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd:HH");   
50.            String tmStr = "2015-5-18:00"; 
51.            Date d = sdf2.parse(tmStr);   
52.            System.out.println(d); 
53.            Calendar now = Calendar.getInstance();   
54.            now.setTime(d);   
55.            now.set(Calendar.HOUR, now.get(Calendar.HOUR) -1);   
56.            System.out.println(sdf2.format(now.getTime())); 
57.             
58.            //最近N天 
59.            SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
60.            cal.add(Calendar.DATE,-7); 
61.            System.out.println("最近7天"+df2.format(cal.getTime())); 
62.            Calendar ca2 =Calendar.getInstance(); 
63.            //最近N个月 
64.            ca2.add(Calendar.MONTH,-1);//最近一个月 
65.            System.out.println("最近一个月"+df2.format(ca2.getTime())); 
66.            Calendar ca3 =Calendar.getInstance(); 
67.            //最近N个年 
68.            ca3.add(Calendar.YEAR,-1);//最近一年 
69.            System.out.println("最近一年"+df2.format(ca3.getTime())); 
70.        } catch (Exception e) { 
71.            e.printStackTrace(); 
72.        } 
73.    } 
74.} 
相关标签: Calendar