天天看点

java 日期计算工具类小结

1、本周第一天: 

public static Date getStartTime4Week() throws Exception{  
      //SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");  
      Calendar cal = Calendar.getInstance();
      int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 2;
      cal.add(Calendar.DATE, -day_of_week);
      /*String startTT = df.format(cal.getTime());
      System.out.println("本周第一天: " + startTT);*/
      return cal.getTime();
}      

2、本周最后一天:

public static Date getEndTime4Week() throws Exception{  
      //SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");  
      Calendar cal = Calendar.getInstance();
      int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 2;
      cal.add(Calendar.DATE, -day_of_week);
      cal.add(Calendar.DATE, 6);
      /*String startTT = df.format(cal.getTime());
      System.out.println("本周第一天: " + startTT);*/
      return cal.getTime();
}      

3、本月第一天:

public static Date getStartTime4Month() throws Exception{  
      //SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
      Calendar c = Calendar.getInstance();    
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
        //String first = df.format(c.getTime());
        //System.out.println("===============first:"+first);
        return c.getTime();
}      

4、本月最后一天:

public static Date getEndTime4Month() throws Exception{  
      //SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");  
      Calendar ca = Calendar.getInstance();    
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));  
        //String last = df.format(ca.getTime());
        //System.out.println("===============last:"+last);
      /*String startTT = df.format(cal.getTime());
      System.out.println("本周第一天: " + startTT);*/
      return ca.getTime();
}      

5、上月第一天:

<pre name="code" class="java">public static Date getStartTime4PreMonth() throws Exception{      
<pre name="code" class="java">        Calendar   cal_1=Calendar.getInstance();//获取当前日期 
        cal_1.add(Calendar.MONTH, -1);
        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
        firstDay = format.format(cal_1.getTime());
        System.out.println("-----1------firstDay:"+firstDay);      

return cal_1.getTime(); }

6、上月最后一天:

<pre name="code" class="java"><pre name="code" class="java">public static Date getStartTime4PreMonth() throws Exception{<span style="font-family: Arial, Helvetica, sans-serif;">       </span>      
<pre name="code" class="java"><pre name="code" class="java">        Calendar cale = Calendar.getInstance();   
        cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天 
        lastDay = format.format(cale.getTime());
        System.out.println("-----2------lastDay:"+lastDay);      

return cale.getTime();  

}      

7、计算两个日期之间相差的天数 :

public static int daysBetween(Date smdate,Date bdate) throws ParseException{    
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
        smdate=sdf.parse(sdf.format(smdate));  
        bdate=sdf.parse(sdf.format(bdate));  
        Calendar cal = Calendar.getInstance();    
        cal.setTime(smdate);    
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(bdate);    
        long time2 = cal.getTimeInMillis();         
        long between_days=(time2-time1)/(1000*3600*24);  
            
       return Integer.parseInt(String.valueOf(between_days));           
    }      

 8、字符串的日期格式的计算:

public static int daysBetween(String smdate,String bdate) throws ParseException{  
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
        Calendar cal = Calendar.getInstance();    
        cal.setTime(sdf.parse(smdate));    
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(sdf.parse(bdate));    
        long time2 = cal.getTimeInMillis();         
        long between_days=(time2-time1)/(1000*3600*24);  
            
       return Integer.parseInt(String.valueOf(between_days));     
}      

9、本季度第一天:

public static Date getStartTime4Quart() throws Exception{ 
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");  
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 4);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
            System.out.println(df.format(c.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }      

10、本季度最后一天:

public static Date getEndTime4Quart() throws Exception{  
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");  
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        try {
            if (currentMonth >= 1 && currentMonth <= 3) {
                c.set(Calendar.MONTH, 2);
                c.set(Calendar.DATE, 31);
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                c.set(Calendar.MONTH, 8);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 10 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }
            System.out.println(df.format(c.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }      

11、本年第一天:

public static Date getStartTime4Year() throws Exception{ 
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");  
        Calendar c = Calendar.getInstance();
        try {
            c.set(Calendar.MONTH, 0);
            c.set(Calendar.DATE, 1);
            System.out.println(df.format(c.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }      

12、本年最后一天:

public static Date getEndTime4Year() throws Exception{  
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");  
        Calendar c = Calendar.getInstance();
        try {
            c.set(Calendar.MONTH, 11);
            c.set(Calendar.DATE, 31);
            System.out.println(df.format(c.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }      

13、求两个日期之间的每一天

/** 
     * 获得日期字符串数组 
     * @param calendarType 日期跨度的类型, 
     * */  
      
    public static Date[] getDateArrays(Date start,Date end ,int calendarType){  
        ArrayList<Date> ret = new ArrayList<Date>();  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(start);  
        Date tmpDate = calendar.getTime();  
        long endTime = end.getTime();  
        while(tmpDate.before(end)||tmpDate.getTime() == endTime){  
            ret.add(calendar.getTime());  
            calendar.add(calendarType, 1);  
            tmpDate = calendar.getTime();  
        }         
          
        Date[] dates = new Date[ret.size()];  
        return ret.toArray(dates);        
    }  
  
@Test  
    public void getDateArrays2(){  
        Date date = DateUtils.getDate("2008-04-01");  
        Date date2 = DateUtils.getDate("2008-08-01");  
        Date[] strArray = DateUtils.getDateArrays(date, date2, Calendar.DAY_OF_YEAR);  
        for (Date string : strArray) {  
            System.out.println(DateUtils.getDayStr(string));  
        }  
    }      

注:SimpleDateFormat是线程不安全的,放到类变量中需要注意。