1. 程式人生 > >日期轉換工具包(DateUtil)

日期轉換工具包(DateUtil)



package com.baiwang.zbxjf.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
  * @ClassName: CalendarUtil
  * @Description: TODO
  * @author qiaoyongda
  * @date 2016年7月19日 下午1:39:21
  */
public class DateUtil {
  private static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");;
     private static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd");;
    
     public static void main(String[] args) {
   System.out.println(startOfyesterday());
   System.out.println(endOfyesterday());
   System.out.println(shortSdf.format(getCurrentQuarterStartTime()));
  }
  /**
     * 當前季度的開始時間,即2016-07-01
  * @param tjsj
     *
     * @return
     */
    public static  Date getCurrentQuarterStartTime() {
       Calendar c = Calendar.getInstance();
          int currentMonth = c.get(Calendar.MONTH) + 1;
          Date now = null;
          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, 6);
              else if (currentMonth >= 10 && currentMonth <= 12)
                  c.set(Calendar.MONTH, 9);
              c.set(Calendar.DATE, 1);
              now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
          } catch (Exception e) {
              e.printStackTrace();
          }
          return now;
    }

    /**
     * 當前季度的結束時間,即2016-09-30
     *
     * @return
     */
    public static Date getCurrentQuarterEndTime(Date date1) {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now2 = null;
        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);
            }
            now2 = longSdf.parse(shortSdf.format(c.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now2;
    }
    /**
     * 下一季度的開始時間
     * @return
     */
    public static  Date getNextSeasonStartTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now1 = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 6);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 9);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 0);
            c.set(Calendar.DATE, 1);
            now1 = longSdf.parse(shortSdf.format(c.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now1;
    }
   
    //昨天開始時間
    public static long startOfyesterday() {
     Calendar calendar = Calendar.getInstance();
     calendar.set(Calendar.HOUR_OF_DAY, 0);
     calendar.set(Calendar.MINUTE, 0);
     calendar.set(Calendar.SECOND, 0);
     calendar.add(Calendar.DATE, -2);
     calendar.set(Calendar.MILLISECOND, 0);
     Date date=calendar.getTime();
     return date.getTime();
   }
    //昨天結束時間
 public static long endOfyesterday() {
     Calendar calendar = Calendar.getInstance();
     calendar.set(Calendar.HOUR_OF_DAY, 23);
     calendar.set(Calendar.MINUTE, 59);
     calendar.set(Calendar.SECOND, 59);
     calendar.set(Calendar.MILLISECOND, 999);
     calendar.add(Calendar.DATE, -1);
     Date date=calendar.getTime();
     return date.getTime();
   }
 
 /**
    * 功能:獲取上月的開始時間
    */
 public static Date startOfLastMonth() {
     Calendar currentDate = Calendar.getInstance();
     currentDate.set(Calendar.HOUR_OF_DAY, 0);
     currentDate.set(Calendar.MINUTE, 0);
     currentDate.set(Calendar.SECOND, 0);
     currentDate.set(Calendar.MILLISECOND, 0);
     currentDate.set(Calendar.DAY_OF_MONTH, 1);
     currentDate.add(Calendar.MONTH, -1);
     Date date=currentDate.getTime();
     return date;
   }
   /**
    * 功能:獲取上月的結束時間
    */
   public static Date endOfLastMonth() {
     Calendar cal = Calendar.getInstance();
     cal.set(Calendar.DAY_OF_MONTH, 1);
     cal.set(Calendar.HOUR_OF_DAY, 23);
     cal.set(Calendar.MINUTE, 59);
     cal.set(Calendar.SECOND, 59);
     cal.set(Calendar.MILLISECOND, 999);
     cal.add(Calendar.DATE, -1);
     Date date=cal.getTime();
     return date;
   }
   /**
    * 功能:獲取時間段內的所有日期
    */
   public static List<Date> findDates(Date dBegin, Date dEnd) {
  
   
         List<Date> lDate = new ArrayList<Date>(); 
         lDate.add(dBegin); 
         Calendar calBegin = Calendar.getInstance();        
         // 使用給定的 Date 設定此 Calendar 的時間   
         calBegin.setTime(dBegin); 
         Calendar calEnd = Calendar.getInstance(); 
         // 使用給定的 Date 設定此 Calendar 的時間   
         calEnd.setTime(dEnd); 
         // 測試此日期是否在指定日期之後   
         while (dEnd.after(calBegin.getTime())) { 
             // 根據日曆的規則,為給定的日曆欄位新增或減去指定的時間量   
             calBegin.add(Calendar.DAY_OF_MONTH, 1);        
             lDate.add(calBegin.getTime()); 
         } 
         return lDate; 
     } 
}