日期加天数、字符串类型转date、获取本周第一天、本周最后一天、本月第一天、本月最后一天、上月第一天、上月最后一天时间

时间:2022-11-19 17:14:36

//日期相加,当前日期加n天,n可以为负整数也可以为正整数

      public static Date addDays(Integer addNum){

DateTime dateTime = new DateTime(new Date());
return dateTime.plusDays(addNum).toDate();
}

       //字符串日期转换为Date类型

public static Date string2Date(String timeStr){
DateTimeFormatter format = DateTimeFormat .forPattern("yyyyMMdd");
DateTime dateTime = DateTime.parse(timeStr, format);
return dateTime.toDate();
}

     //获取本周第一天的时间,输出格式为:如2014-10-16 13:45:52

public static Date getFirstDayOfWeek(){
DateTime dateTime = new DateTime(new Date()).dayOfWeek().withMinimumValue();
return dateTime.toDate();
}


      //获取本周最后一天的时间,输出格式为:如2014-10-19 15:39:04

public static Date getLastDayOfWeek(){
DateTime dateTime = new DateTime(new Date()).dayOfWeek().withMaximumValue();
return dateTime.toDate();
}

       //获取当前月份第一天的时间,输出格式为:如2014-10-31 23:59:59

public static Date getFirstDayOfMonth(){
DateTime dateTime = new DateTime(new Date()).dayOfMonth().withMinimumValue();
return dateTime.toDate();
}

       //获取当前月份的最后一天的时间,输出格式为:如2014-10-02 15:25:14

public static Date getLastDayOfMonth(){
DateTime dateTime = new DateTime(new Date()).dayOfMonth().withMaximumValue();
return dateTime.toDate();
}

//获取当前日期的凌晨0点时间,输出格式为:如2014-10-10 00:00:00
public static Date getTodayMorning(){
 Calendar calendar = Calendar.getInstance();
 calendar.setTime(new Date());
 calendar.set(Calendar.HOUR_OF_DAY, 0);
 calendar.set(Calendar.MINUTE, 0);
 calendar.set(Calendar.SECOND, 0);
 return calendar.getTime();
}

//获取当前日期的晚上23点时间,输出格式为:如2014-10-18 23:59:59
public static Date getTodayNight(){
 Calendar calendar = Calendar.getInstance();
 calendar.setTime(new Date());
 calendar.set(Calendar.HOUR_OF_DAY, 23);
 calendar.set(Calendar.MINUTE, 59);
 calendar.set(Calendar.SECOND, 59);
 return calendar.getTime();
}

//获取本周第一天,输出格式为:如2014-10-13 00:00:00
public static Date getWeekFirstDay(){
 Calendar calendar = Calendar.getInstance();
 int dayofweek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
 if (dayofweek == 0)
  dayofweek = 7;
 calendar.add(Calendar.DATE, -dayofweek + 1);
 calendar.set(Calendar.HOUR_OF_DAY, 0);
 calendar.set(Calendar.MINUTE, 0);
 calendar.set(Calendar.SECOND, 0);
 return calendar.getTime();
}

//获取本周最后一天,输出格式为:如2014-10-19 23:59:59
public static Date getWeekLastDay(){
Calendar calendar = Calendar.getInstance();
 int dayofweek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
 if (dayofweek == 0)
  dayofweek = 7;
 calendar.add(Calendar.DATE, -dayofweek + 7);
 calendar.set(Calendar.HOUR_OF_DAY, 23);
 calendar.set(Calendar.MINUTE, 59);
 calendar.set(Calendar.SECOND, 59);
 return calendar.getTime();
}

//获取当前月第一天,输出格式为:如2014-10-01 00:00:00
public static Date getMonthFirstDay(){
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
    return calendar.getTime();
}

//获取当前月最后一天,输出格式为:如2014-10-31 23:59:59
public static Date getMonthLastDay(){
Calendar calendar = Calendar.getInstance();
   calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
   calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
    return calendar.getTime();
}

//获取上月第一天,输出格式为:如2014-10-01 00:00:00
public static Date getPrevMonthFirstDay(){
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
    return calendar.getTime();
}

//获取上月最后一天时间,输出格式为:如2014-10-31 23:59:59
public static Date getPrevMonthLastDay(){
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
   return calendar.getTime();
}