获取当前日期,过去一周,过去一月,过去半年,过去一年......等等

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Tong__wei/article/details/92773245
import java.util.Date;

import com.ibm.icu.text.SimpleDateFormat;
import com.ibm.icu.util.Calendar;

/**
 * 日期工具类
 * @author Tong
 * @Date 2019年5月15日
 */
public class DateUtils {
 
   /**
    * 获取今天
    * @return String
    * */
   public static String getToday(){
      return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
   }
   
   /**
    * 获取昨天
    * @return String
    */
   public static String getYestoday(){
      Calendar cal=Calendar.getInstance();
      cal.add(Calendar.DATE,-1);
      return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
   }
   
   /**
    * 获取本月开始日期
    * @return String
    */
   public static String getMonthStart(){
      Calendar cal=Calendar.getInstance();
      cal.add(Calendar.MONTH, 0);
      cal.set(Calendar.DAY_OF_MONTH, 1);
      return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
   }
   
   /**
    * 获取本月最后一天
    * @return String
    */
   public static String getMonthEnd(){
      Calendar calendar=Calendar.getInstance();
      calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
      return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
   }
   
   /**
    * 获取上月开始日期
    * @return
    */
   public static String getLastMonthStart(){
      Calendar calendar = Calendar.getInstance();
      calendar.add(Calendar.MONTH, -1);
      calendar.set(Calendar.DAY_OF_MONTH, 1);
      return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
   }
   
   /**
    * 获取上月最后一天
    * @return
    */
   public static String getLastMonthEnd(){
      Calendar calendar = Calendar.getInstance();
      calendar.set(Calendar.DAY_OF_MONTH, 1); 
      calendar.add(Calendar.DATE, -1);
      return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
   }
   
   /**
    * 获取以当前时间为准的上一个月时间
    * @return
    */
   public static String getLastMonthByDate(Date date) {
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      calendar.add(Calendar.MONTH, -1);
      return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
   }
   
   /**
    * 获取本周的第一天
    * @return String
    */
   public static String getWeekStart(){
      Calendar cal=Calendar.getInstance();
      cal.add(Calendar.WEEK_OF_MONTH, 0);
      cal.set(Calendar.DAY_OF_WEEK, 2);
      return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
   }
   /**
    * 获取本周的最后一天
    * @return String
    */
   public static String getWeekEnd(){
      Calendar cal=Calendar.getInstance();
      cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
      cal.add(Calendar.DAY_OF_WEEK, 1);
      return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
   }
   /**
    * 获取本年的第一天
    * @return String
    */
   public static String getYearStart(){
      return new SimpleDateFormat("yyyy").format(new Date())+"-01-01";
   }
 
   /**
    * 获取本年的最后一天
    * @return String
    */
   public static String getYearEnd(){
      Calendar calendar = Calendar.getInstance();
      calendar.set(Calendar.MONTH,calendar.getActualMaximum(Calendar.MONTH));
      calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
      return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
   }
   
   /** 
   * 获取前/后半年的开始时间 
   * @return 
   */ 
   public static String getHalfYearStartTime(){
      SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
      Calendar c = Calendar.getInstance(); 
      int currentMonth = c.get(Calendar.MONTH) + 1; 
      String halfYearStartTime = null; 
      try { 
         if (currentMonth >= 1 && currentMonth <= 6){ 
            c.set(Calendar.MONTH, 0); 
         }else if (currentMonth >= 7 && currentMonth <= 12){ 
            c.set(Calendar.MONTH, 6); 
         } 
         c.set(Calendar.DATE, 1);
         halfYearStartTime = shortSdf.format(c.getTime());
      } catch (Exception e) { 
         e.printStackTrace(); 
      } 
      return halfYearStartTime;
   }
   
   /** 
   * 获取前/后半年的结束时间 
   * @return 
   */ 
   public static String getHalfYearEndTime(){ 
      SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
      Calendar c = Calendar.getInstance(); 
      int currentMonth = c.get(Calendar.MONTH) + 1; 
      String halfYearEndTime = null; 
      try { 
         if (currentMonth >= 1 && currentMonth <= 6){ 
            c.set(Calendar.MONTH, 5); 
            c.set(Calendar.DATE, 30); 
         }else if (currentMonth >= 7 && currentMonth <= 12){ 
            c.set(Calendar.MONTH, 11); 
            c.set(Calendar.DATE, 31); 
         }
         halfYearEndTime = shortSdf.format(c.getTime());
      } catch (Exception e) { 
         e.printStackTrace(); 
      } 
      return halfYearEndTime; 
   }

   /**
    * 获取过去几天
    * @param num 过去几天
    * @return
    */
   public static String getPastNumDay(int num) {
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
      Calendar c = Calendar.getInstance();
      c.setTime(new Date());
        c.add(Calendar.DATE, - num);
        return format.format(c.getTime());
   }
   
   /**
    * 获取过去几个月
    * @param num 几个月
    * @return
    */
   public static String getPastNumMonth(int num) {
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
      Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MONTH, - num);
        return format.format(c.getTime());
   }
   
   /**
    * 获取过去几年
    * @param num 几年
    * @return
    */
   public static String getPastNumYear(int num) {
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
      Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.YEAR, - num);
        return format.format(c.getTime());
   }
}
展开阅读全文

没有更多推荐了,返回首页