日期类一些归纳


package com.kingdee.fdc.mobile.assist.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
 * 基础类
 * @author yoen_qin
 *
 */
public class AssistUtil {
 /**
  * 获得当前日期
  * @return
  */
 public static Calendar getCalendar() {
  return Calendar.getInstance();
 }
 
 /**
  * 获得当前日期
  * @return
  */
 public static Date getToday() {
  return getCalendar().getTime();
 }
    /**
     * 格式化某一个日期,例如"yyyy-MM-dd 00:00:00"
     * @param date
     * @param format
     * @return
     *
     */
 public static String getSimpleDateFormat(Date date,String format){
  SimpleDateFormat df=new SimpleDateFormat(format);
  return df.format(date);
 }
 
 /**
  * 某一天的前面几天的日期
  * @param date
  * @return
  */
 public static Date getLastCountDay(Date date,int count){
  Calendar cal = getCalendar();
  cal.setTime(date);
  cal.add(cal.DATE,-count);
  return cal.getTime();
 }
 
 /**
  * 某一天的后面几天的日期
  * @param date
  * @return
  */
 public static Date getNextCountDay(Date date,int count){
  Calendar cal = getCalendar();
  cal.setTime(date);
  cal.add(cal.DATE,count);
  return cal.getTime();
 }
 
 /**
  * 某一天所在的周第一天
  * @param date
  * @return
  */
 public static Date getFirstDayOfWeek(Date date){
  Calendar cal = getCalendar();
  cal.setTime(date);
  cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
  return cal.getTime();
 }
 
 /**
  * 某一天所在的周最后一天
  * @param date
  * @return
  */
 public static Date getLastDayOfWeek(Date date){
  Calendar cal = getCalendar();
  cal.setTime(date);
  cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
  return cal.getTime();
 }
 

 /**
  * 某一天下一周,第几天开始,day为0则从第一天开始
  * @param date
  * @return
  */
 public static Date getFirstDayOfNextWeek(Date date,int day){
  Calendar cal = getCalendar();
  cal.setTime(date);
  cal.add(cal.DATE,7);
  if(day == 0){
   cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
  }else if (day == 1){
   cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
  }else if(day == 3){
   cal.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
  }else if (day == 4){
   cal.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
  }else if (day == 5){
   cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
  }else if (day == 6){
   cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
  }
  
  return cal.getTime();
 }
 
 /**
  * 某一天下一周最后一天
  * @param date
  * @return
  */
 public static Date getLastDayOfNextWeek(Date date){
  Calendar cal = getCalendar();
  cal.setTime(date);
  cal.add(cal.DATE,7);
  cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
  return cal.getTime();
 }
 
 /**
  * 比较两个日期是否相同
  * @param dt1
  * @param dt2
  * @return
  */
   public static int compareDate(String DATE1, String DATE2) {
       SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
          try {
              Date dt1 = df.parse(DATE1);
              Date dt2 = df.parse(DATE2);
              if (dt1.getTime() > dt2.getTime()) {
                  return 1;
              } else if (dt1.getTime() < dt2.getTime()) {
                  return -1;
              } else {
                  return 0;
              }
          } catch (Exception exception) {
              exception.printStackTrace();
          }
          return 0;
      }
 
   /**
    * 判断今天是星期几
    * @param weekDay 若为5 则是星期五
    * @return
    */
   public static boolean getTodayForWeek(int weekDay){
    Calendar cal = Calendar.getInstance();
   int dayForWeek = 0;
   if(cal.get(Calendar.DAY_OF_WEEK) == 1){
    dayForWeek = 7;
   }else{
    dayForWeek = cal.get(Calendar.DAY_OF_WEEK) - 1;
   }
   if(weekDay == dayForWeek){
    return true;
   }
   return false;
   }
  
   /**
    * 获得当时时间多少分钟后的时间,精确到分钟 yyyy-MM-dd HH:mm
    * @param minutelyTrigger
    * @return
    */
   public static String getMinutelyTrigger(Date date,int minutelyTrigger){
        String timeStr = "";
          String day = new SimpleDateFormat("yyyy-MM-dd").format(date);
          String hourMinute = new SimpleDateFormat("HHmm").format(date);
          int time2 = Integer.parseInt(hourMinute);
          time2 = time2 + minutelyTrigger;
          int hour = time2 / 100;
          int minute = time2 % 100;
          if (minute > 60) {
              hour = hour + 1;
              minute = minute % 60;
          }
          String minuteStr = "" + minute;
          if (minute < 10) {
              minuteStr = "0" + minute;
          }
          String hourStr = "" + hour;
          if (hour > 23) {
           SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
          day = formatter.format(getNextCountDay(date,1));
              hour = hour % 24;
          }
          if (hour < 10) {
              hourStr = "0" + hour;
          }
          timeStr = day + " " + hourStr + ":" + minuteStr+":59" ;
          return timeStr;
   }
   /**
    * String 转成Date
    * @param dateStr
    * @return
    */
   public static Date toChangeStringToDate(String dateStr){
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   Date d = null;
   try {
    d = format.parse(dateStr);
   } catch (ParseException e) {
    e.printStackTrace();
   }
   return d;
   }
  
   /**
   * 某一天所在的月第一天
   * @param date
   * @return
   */
  public static Date getFirstDayOfMonth(Date date){
   Calendar cal = getCalendar();
   cal.setTime(date);
   int currentYear = cal.get(Calendar.YEAR);
   int currentMonth = cal.get(Calendar.MONTH);//此值比实际月份少1
   cal.set(currentYear,currentMonth,1);//当前月的第一天
   return cal.getTime();
  }
  
  /**
   * 某一天所在的月最后一天
   * @param date
   * @return
   */
  public static Date getLastDayOfMonth(Date date){
   Calendar cal = getCalendar();
   cal.setTime(date);
   int currentYear = cal.get(Calendar.YEAR);
   int currentMonth = cal.get(Calendar.MONTH);//此值比实际月份少1
   cal.set(currentYear,currentMonth + 1,0);//让下个月的第一天减一天即上个月的最后一天
   return cal.getTime();
  }
  
  /**
   * 某一天所在的年份
   * @param date
   * @return
   */
  public static int getYearOfDay(Date date){
   Calendar cal = getCalendar();
   cal.setTime(date);
   int currentYear = cal.get(Calendar.YEAR);
   return currentYear;
  }
  
  public static int getMonthOfDay(Date date){
   Calendar cal = getCalendar();
   cal.setTime(date);
   int currentMonth = cal.get(Calendar.MONTH)+1;
   return currentMonth;
  }
  
  /**
   * 某一天所在的季度第一天
   * @param date
   * @return
   */
  public static Date getFirstDayOfQuarter(Date date){
   Calendar cal = getCalendar();
   cal.setTime(date);
   int month = getQuarterInMonth(cal.get(Calendar.MONTH)+1, true); 
   cal.set(Calendar.MONTH, month-1); 
   cal.set(Calendar.DAY_OF_MONTH, 1); 
   return cal.getTime();
   
  }
  
  /**
   * 某一天所在的季度最后一天
   * @param date
   * @return
   */
  public static Date getLastDayOfQuarter(Date date){
   Calendar cal = getCalendar();
   cal.setTime(date);
   int month = getQuarterInMonth(cal.get(Calendar.MONTH)+1, false); 
   cal.set(Calendar.MONTH, month); 
   cal.set(Calendar.DAY_OF_MONTH, 0); 
   return cal.getTime();
  }
  
 private static int getQuarterInMonth(int month, boolean isQuarterStart) { 
         int months[] = { 1, 4, 7, 10 }; 
        if (!isQuarterStart) { 
             months = new int[] { 3, 6, 9, 12 }; 
         } 
         if (month >= 1 && month < 4) 
            return months[0]; 
        else if (month >= 4 && month < 7) 
             return months[1]; 
         else if (month >= 7 && month < 10) 
            return months[2]; 
         else 
            return months[3]; 
 } 
 public static int getQuarterByMonth(int month) { 
        int months[] = { 1, 2, 3, 4 }; 
        if (month >= 1 && month < 4) 
           return months[0]; 
        else if (month >= 4 && month < 7) 
            return months[1]; 
        else if (month >= 7 && month < 10) 
           return months[2]; 
        else 
           return months[3]; 
   } 
 
 /**
  * 某一天的前面几月的日期
  * @param date
  * @return
  */
 public static Date getLastCountMonth(Date date,int count){
  Calendar cal = getCalendar();
  cal.setTime(date);
  cal.add(cal.MONTH,-count);
  return cal.getTime();
 }
 
 /**
  * 某一天的后面几月的日期
  * @param date
  * @return
  */
 public static Date getNextCountMonth(Date date,int count){
  Calendar cal = getCalendar();
  cal.setTime(date);
  cal.add(cal.MONTH,count);
  return cal.getTime();
 }
 
 /**
  * 某一天所在季度的第几周
  * @param date
  * @return
  */
 public static int getQuarterBetweenDate(Date date1,Date date2){
   Calendar cal2 = getCalendar();
   cal2.setTime(date2);
   int week2 = cal2.get(Calendar.WEEK_OF_YEAR);
  
   Calendar cal = getCalendar();
   cal.setFirstDayOfWeek(Calendar.SUNDAY);
   cal.setTime(date1);
   int week1 = cal.get(Calendar.WEEK_OF_YEAR);
   return (week2-week1+1);
  
//  long allDays = date2.getTime()-date1.getTime();
//  long day = allDays/1000/3600/24/7;
//  return Integer.parseInt(String.valueOf(day))+1;
  
//  int day =Integer.parseInt(String.valueOf((date2.getTime()-date1.getTime())/(3600*24*1000)/7));
//  return day+1;
 }
 
 /**
  * 某一天所在月的天数
  * @param date
  * @return
  */
 public static int getDaysOfMonth(Date date){
  Calendar cal = getCalendar();
  cal.setTime(date);
  return cal.get(Calendar.DATE);
 }
 
 /**
  * 前几年的今天
  * @param date
  * @param count
  * @return
  */
 public static Date getLastCountYear(Date date,int count){
  Calendar cal = getCalendar();
  cal.setTime(date);
  cal.add(cal.YEAR,-count);
  return cal.getTime();
 }
 
 /**
  * 后几年的今天
  */
 public static Date getNextCountYear(Date date,int count){
  Calendar cal = getCalendar();
  cal.setTime(date);
  cal.add(cal.YEAR,count);
  return cal.getTime();
 }
 
 /**
  * 得到季度的第一天
  * @param time
  * @return
  */
 public static Date getQuarterFirstDate(String time){
  String yearStr = time.substring(0,4);
  String quarterStr = time.substring(4);
  String dayStr = "-01-01 00:00:00";
  if(quarterStr.equals("02")){
   dayStr = "-04-01 00:00:00";
  }else if(quarterStr.equals("03")){
   dayStr = "-07-01 00:00:00";
  }else if(quarterStr.equals("04")){
   dayStr = "-10-01 00:00:00";
  }
  Date d = toChangeStringToDate(yearStr+dayStr);
  return d;
 }

 
 /**
  * 比较两个日期相差的天数
  * @param fDate
  * @param oDate
  * @return
  */
 public static int daysOfTwo(Date fDate, Date oDate) {
        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(fDate);
        int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
        aCalendar.setTime(oDate);
        int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
        return day2 - day1;
     }

}
 
 
/**
   * 某一天所在的周第一天 从星期一开始
   * @param date
   * @return
   */
  public static Date getFirstDayOfWeek(Date date){
   Calendar cal = getCalendar();
   if(getTodayForWeek(date,7)){ //如果是星期日,归到上一周
    date = getLastDay(date,1);
   }
   cal.setTime(date);
   cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
   Date newDate = cal.getTime();
   return newDate;
  }
 
  /**
   * 某一天所在的周最后一天 以星期日结束
   * @param date
   * @return
   */
  public static Date getLastDayOfWeek(Date date){
   Calendar cal = getCalendar();
   if(getTodayForWeek(date,7)){ //如果是星期日,归到上一周
    date = getLastDay(date,1);
   }
   cal.setTime(date);
   cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
   cal.add(Calendar.DAY_OF_YEAR, 1);
   return  cal.getTime();
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值