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

 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.*;

 public class Test {

     private static Date deformatDatetime(String strDate, String fmt) {
         try {
             if (fmt == null) {
                 return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                         java.util.Locale.ENGLISH)).parse(strDate);
             } else {
                 return (new SimpleDateFormat(fmt, java.util.Locale.ENGLISH))
                         .parse(strDate);
             }
         } catch (ParseException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
             return null;
         }
     }
     private static String datetimeToString(Date dt, String fmt) {
         if (fmt == null) {
             return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                     java.util.Locale.ENGLISH)).format(dt);
         } else {
             return (new SimpleDateFormat(fmt, java.util.Locale.ENGLISH))
                     .format(dt);
         }
     }
     private static class DateRange{
         public Date startDate;
         public Date endDate;
         public DateRange(){

         }
         public DateRange(Date dt1,Date dt2){
             startDate=dt1;
             endDate=dt2;
         }
     }
     /**
      * 根據年份獲得該年所有周次及每周的開始-結束日期
      * @param pvnYear
      * @return
      */
     private static LinkedHashMap<Integer,DateRange> getWeeksDetByYear(int pvnYear){
         LinkedHashMap<Integer, DateRange> lvRet=new LinkedHashMap<Integer,DateRange>();
         Calendar lvCal=Calendar.getInstance();
         lvCal.setFirstDayOfWeek(Calendar.MONDAY);

         Date lvDt=deformatDatetime(String.valueOf(pvnYear)+"-01-01 00:00:00", null);
         lvCal.setTime(lvDt);
         int lvWeek=1;
         while (true) {
             lvCal.set(Calendar.DAY_OF_WEEK, lvCal.getFirstDayOfWeek()); // Monday
             Date lvFirstDt=lvCal.getTime();
             if (lvFirstDt.getYear()+1900<pvnYear){
                 lvFirstDt=lvDt;
             }
             if (lvFirstDt.getYear()+1900>pvnYear) break;
             lvCal.set(Calendar.DAY_OF_WEEK, lvCal.getFirstDayOfWeek()+6); // Sunday?
             Date lvLastDt=lvCal.getTime();
             if (lvLastDt.getYear()+1900>pvnYear){
                 lvLastDt=deformatDatetime(String.valueOf(pvnYear+1)+"-01-01 00:00:00", null);
                 lvCal.setTime(lvLastDt);
                 lvCal.add(Calendar.DAY_OF_YEAR, -1);
                 lvLastDt=lvCal.getTime();
             }
             lvRet.put(Integer.valueOf(lvWeek), new DateRange(lvFirstDt, lvLastDt));
             lvWeek++;
             lvCal.add(Calendar.WEEK_OF_YEAR, 1);
         }
         return lvRet;
     }
     public static void main(String[] args) throws  Exception{
         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
         LinkedHashMap<Integer,DateRange>  lvRet=getWeeksDetByYear(2011);
         for (Map.Entry<Integer, DateRange> item:lvRet.entrySet()){
             String startDate = datetimeToString(item.getValue().startDate,"yyyy-MM-dd");
             String endDate = datetimeToString(item.getValue().endDate,"yyyy-MM-dd");
             Date date1 = format.parse(startDate);
             Date date2 = format.parse(endDate);
             int diff = differentDays(date1,date2);
             System.out.println(String.format("Week: %d, %s - %s", item.getKey(),datetimeToString(item.getValue().startDate,"yyyy-MM-dd"),
                     datetimeToString(item.getValue().endDate,"yyyy-MM-dd"))+" ----------"+diff);
         }
     }

     public static int differentDays(Date date1,Date date2)
     {
         Calendar cal1 = Calendar.getInstance();
         cal1.setTime(date1);

         Calendar cal2 = Calendar.getInstance();
         cal2.setTime(date2);
         int day1= cal1.get(Calendar.DAY_OF_YEAR);
         int day2 = cal2.get(Calendar.DAY_OF_YEAR);

         int year1 = cal1.get(Calendar.YEAR);
         int year2 = cal2.get(Calendar.YEAR);
         if(year1 != year2)   //同一年
         {
             int timeDistance = 0 ;
             for(int i = year1 ; i < year2 ; i ++)
             {
                 if(i%4==0 && i%100!=0 || i%400==0)    //闰年
                 {
                     timeDistance += 366;
                 }
                 else    //不是闰年
                 {
                     timeDistance += 365;
                 }
             }

             return timeDistance + (day2-day1) ;
         }
         else    //不同年
         {
             return day2-day1;
         }
     }
 }

java获取一年的周数和间隔天数的更多相关文章

  1. java获取当前上一周、上一月、上一年的时间

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar c = Calend ...

  2. C# 获取当前日期当年的周数

    这几天跨年,项目上遇到了一个周数计算的问题. 2016年的元旦是周五开始的,之前系统计算的是属于15年的第53个周,但是年份已经到了16年了. 公司要求从1月1号周五开始算作16年的第一个周,今天1月 ...

  3. JAVA获取当前日期时间所在周的周一和周日日期

    /** * 获取当前时间所在周的周一和周日的日期时间 * @return */ public static Map<String,String> getWeekDate() { Map&l ...

  4. Java获取当前第几周【转】

    本文copy自:http://swxzqsd.blog.sohu.com/156208509.html 作者:camelcanoe String today = "2010-01-11&qu ...

  5. java获取当前日期所在的周的周一,并以周一为一周开始

    public String getMonday(String date) { if (date == null || date.equals("")) { System.out.p ...

  6. java获取本月开始时间和结束时间、上个月第一天和最后一天的时间以及当前日期往前推一周、一个月

    import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.uti ...

  7. Java获取某年某周的第一天

    Java获取某年某周的第一天 1.设计源码 FirstDayOfWeek.java: /** * @Title:FirstDayOfWeek.java * @Package:com.you.freem ...

  8. sql 中,如何获取两个日期之前月数、周数、天数

    1.获取两个日期之间的月数.周数.天数语法 --1.获取两个日期之间的月数.周数.天数 --1.1)声明参数 ) ) --1.2)获取两个日期直接的月数 select DATEDIFF(MM,@sta ...

  9. Java获取日期属于当年第几周

    String today = "2013-01-14"; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM- ...

随机推荐

  1. css中border画三角形

    <!doctype html><html lang="en"> <head>  <meta charset="UTF-8&quo ...

  2. Python中什么是深拷贝和浅拷贝且有什么区别

    浅拷贝: >>> a = [1, 2, 3] >>> b = a >>> a [1, 2, 3] >>> b [1, 2, 3] ...

  3. yslow V2 准则详细讲解

    主要有12条:   1. Make fewer HTTP requests 尽可能少的http请求..我们有141个请求(其中15个JS请求,3个CSS请求,47个CSS background ima ...

  4. How does exercise keep your brain young?

    Exercise may protect the brain from disease and dementia as we age, but the mechanisms behind its be ...

  5. [转] 对express中next函数的一些理解

    最近公司在使用node做前后端分离,采用的web框架是express,所以对express框架进行了深入的了解,前段时间写了篇关于express路由的文章,但是在那篇文章中貌似少了一个很重要的内容,就 ...

  6. 1.RN环境搭建,创建项目,使用夜神模拟调试

    1.环境搭建(Yarn.React Native 的命令行工具(react-native-cli)) npm install -g yarn react-native-cli 具体参考 参见官方(中文 ...

  7. Note for "Some Remarks on Writing Mathematical Proofs"

    John M. Lee is a famous mathematician, who bears the reputation of writing the classical book " ...

  8. CentOS7.5安装nodejs 转

    CentOS7.5安装nodejs CentOS安装NodeJS 在CentOS下安装NodeJS有以下几种方法.使用的CentOS版本为7.2.CentOS其他版本的NodeJS安装大同小异,也可以 ...

  9. 【原创】c# socket 粘包 其实。。。

    文章内容有错,请直接关闭~~~不要看了.丢人. private static Dictionary<string, Packet> cache = new Dictionary<st ...

  10. 清除DNS缓存(解决能上QQ但是无法上网页问题)

    ipconfig/displaydnsipconfig/flushdns