java获取一年的周数和间隔天数

java获取一年的周数和间隔天数

  1 import java.text.ParseException;
  2 import java.text.SimpleDateFormat;
  3 import java.util.*;
  4 
  5 public class Test {
  6 
  7     private static Date deformatDatetime(String strDate, String fmt) {
  8         try {
  9             if (fmt == null) {
 10                 return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
 11                         java.util.Locale.ENGLISH)).parse(strDate);
 12             } else {
 13                 return (new SimpleDateFormat(fmt, java.util.Locale.ENGLISH))
 14                         .parse(strDate);
 15             }
 16         } catch (ParseException e) {
 17             // TODO Auto-generated catch block
 18             e.printStackTrace();
 19             return null;
 20         }
 21     }
 22     private static String datetimeToString(Date dt, String fmt) {
 23         if (fmt == null) {
 24             return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
 25                     java.util.Locale.ENGLISH)).format(dt);
 26         } else {
 27             return (new SimpleDateFormat(fmt, java.util.Locale.ENGLISH))
 28                     .format(dt);
 29         }
 30     }
 31     private static class DateRange{
 32         public Date startDate;
 33         public Date endDate;
 34         public DateRange(){
 35 
 36         }
 37         public DateRange(Date dt1,Date dt2){
 38             startDate=dt1;
 39             endDate=dt2;
 40         }
 41     }
 42     /**
 43      * 根據年份獲得該年所有周次及每周的開始-結束日期
 44      * @param pvnYear
 45      * @return
 46      */
 47     private static LinkedHashMap<Integer,DateRange> getWeeksDetByYear(int pvnYear){
 48         LinkedHashMap<Integer, DateRange> lvRet=new LinkedHashMap<Integer,DateRange>();
 49         Calendar lvCal=Calendar.getInstance();
 50         lvCal.setFirstDayOfWeek(Calendar.MONDAY);
 51 
 52         Date lvDt=deformatDatetime(String.valueOf(pvnYear)+"-01-01 00:00:00", null);
 53         lvCal.setTime(lvDt);
 54         int lvWeek=1;
 55         while (true) {
 56             lvCal.set(Calendar.DAY_OF_WEEK, lvCal.getFirstDayOfWeek()); // Monday
 57             Date lvFirstDt=lvCal.getTime();
 58             if (lvFirstDt.getYear()+1900<pvnYear){
 59                 lvFirstDt=lvDt;
 60             }
 61             if (lvFirstDt.getYear()+1900>pvnYear) break;
 62             lvCal.set(Calendar.DAY_OF_WEEK, lvCal.getFirstDayOfWeek()+6); // Sunday?
 63             Date lvLastDt=lvCal.getTime();
 64             if (lvLastDt.getYear()+1900>pvnYear){
 65                 lvLastDt=deformatDatetime(String.valueOf(pvnYear+1)+"-01-01 00:00:00", null);
 66                 lvCal.setTime(lvLastDt);
 67                 lvCal.add(Calendar.DAY_OF_YEAR, -1);
 68                 lvLastDt=lvCal.getTime();
 69             }
 70             lvRet.put(Integer.valueOf(lvWeek), new DateRange(lvFirstDt, lvLastDt));
 71             lvWeek++;
 72             lvCal.add(Calendar.WEEK_OF_YEAR, 1);
 73         }
 74         return lvRet;
 75     }
 76     public static void main(String[] args) throws  Exception{
 77         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 78         LinkedHashMap<Integer,DateRange>  lvRet=getWeeksDetByYear(2011);
 79         for (Map.Entry<Integer, DateRange> item:lvRet.entrySet()){
 80             String startDate = datetimeToString(item.getValue().startDate,"yyyy-MM-dd");
 81             String endDate = datetimeToString(item.getValue().endDate,"yyyy-MM-dd");
 82             Date date1 = format.parse(startDate);
 83             Date date2 = format.parse(endDate);
 84             int diff = differentDays(date1,date2);
 85             System.out.println(String.format("Week: %d, %s - %s", item.getKey(),datetimeToString(item.getValue().startDate,"yyyy-MM-dd"),
 86                     datetimeToString(item.getValue().endDate,"yyyy-MM-dd"))+" ----------"+diff);
 87         }
 88     }
 89 
 90     public static int differentDays(Date date1,Date date2)
 91     {
 92         Calendar cal1 = Calendar.getInstance();
 93         cal1.setTime(date1);
 94 
 95         Calendar cal2 = Calendar.getInstance();
 96         cal2.setTime(date2);
 97         int day1= cal1.get(Calendar.DAY_OF_YEAR);
 98         int day2 = cal2.get(Calendar.DAY_OF_YEAR);
 99 
100         int year1 = cal1.get(Calendar.YEAR);
101         int year2 = cal2.get(Calendar.YEAR);
102         if(year1 != year2)   //同一年
103         {
104             int timeDistance = 0 ;
105             for(int i = year1 ; i < year2 ; i ++)
106             {
107                 if(i%4==0 && i%100!=0 || i%400==0)    //闰年
108                 {
109                     timeDistance += 366;
110                 }
111                 else    //不是闰年
112                 {
113                     timeDistance += 365;
114                 }
115             }
116 
117             return timeDistance + (day2-day1) ;
118         }
119         else    //不同年
120         {
121             return day2-day1;
122         }
123     }
124 }
View Code

 

转载于:https://www.cnblogs.com/xiaofengfree/p/10253835.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值