java-日期Calendar类

  • java月份是从0开始数,0月就是1月,美国的星期日是一周的第一天
 1 package edu.freshen.api;
 2 /***
 3  * 使用Calendar来获取日历时间
 4  * 计算到放假还有多少天
 5  */
 6 import java.util.Calendar;
 7 
 8 public class TestCalendar {
 9     static Calendar c = Calendar.getInstance();//获取日历对象
10     public static void main(String[] args) {    
11         /*
12         c.add(Calendar.DAY_OF_MONTH,100);//增加100天(增加几年或几个月几日后来显示日历都可以)
13         c.set(2016, 11,25,0,0,0);//设置固定时间日历
14 */         //上面时间是放假日期,来算一算现在到放假还有多长时间    
15         int day1 = dayNum(c);
16         c.set(2016, 11,25,0,0,0);
17         int day2 = dayNum(c);
18         System.out.println("今天到放假时间还要"+(day2-day1)+"天");
19 
20     }
21     public static int dayNum(Calendar c){
22         int year = c.get(Calendar.YEAR);//
23         int mon = c.get(Calendar.MONTH)+1;//
24         int day = c.get(Calendar.DAY_OF_MONTH);//
25         
26         int hour = c.get(Calendar.HOUR_OF_DAY);//
27         int min = c.get(Calendar.MINUTE);//
28         int second= c.get(Calendar.SECOND);//
29         int dw = c.get(Calendar.DAY_OF_WEEK_IN_MONTH)-1;//由于使用的是美国星期制,星期日是一周的开始,所以要减一
30         int days = c.get(Calendar.DAY_OF_YEAR);
31         
32         System.out.println(year+"年"+mon+"月"+day+"日"+hour+"点"+min+"分"+second+"秒"+"今天是周"+dw);
33         return days;
34         
35     }
36 
37 }

 结果

2016年8月8日17点5分43秒今天是周1
2016年12月25日0点0分0秒今天是周3
今天到放假时间还要139天
原文地址:https://www.cnblogs.com/slxydyl/p/5750174.html