码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-01-11 11:27:11      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:ast   out   rgs   for   system   +=   @param   gen   cal   

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

 

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

标签:ast   out   rgs   for   system   +=   @param   gen   cal   

原文地址:https://www.cnblogs.com/xiaofengfree/p/10253835.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!