zoukankan      html  css  js  c++  java
  • Java 日期处理

    //查询条件日期时间段为空的话默认设置本月一号到明天(0时),查询时小于明天

    String beginDate=request.getParameter("beginDate");
       String endDate=request.getParameter("endDate");

    if(!CommonMethod.isNull(beginDate)){
        qm.setBeginDate(DateUtil.parseStringToDate(beginDate, "yyyy-MM-dd"));
       }else{
        qm.setBeginDate(DateUtil.parseStringToDate(DateUtil.getFormatDateStr(new Date(), "yyyy-MM")+"-01", "yyyy-MM-dd"));
       }
       if(!CommonMethod.isNull(endDate)){
        qm.setEndDate(DateUtil.getNextNDayDate(DateUtil.parseStringToDate(endDate, "yyyy-MM-dd"),1));
       }else{
        qm.setEndDate(DateUtil.getNextNDayDate(new Date(), 1));
       }

    用到函数:

    /**
      * @功能描述:將字符串轉換為日期
      * @param dateStr
      * @return
      * @创建时间 2008-7-13
      * @author Administrator
      */
     public static Date parseStringToDate(String dateStr, String formate)
       throws Exception {
      try {
       SimpleDateFormat sdf = getSimpleDateFormat(formate);
       return sdf.parse(dateStr);
      } catch (Exception ex) {
       return null;
      }
     }

    /**
      * 获取当前日期
      * @return
      * @throws Exception
      */
     public static Date getCurrentDate() throws Exception {
      Date currTime = new Date(System.currentTimeMillis());
      return currTime;
     }
     /**
      * 获取date日期的n天后的日期
      * @param date
      * @param day
      * @return
      */
     public static Date getNextNDayDate(Date date, int day){
      Calendar c=Calendar.getInstance();
      c.setTime(date);
      c.add(Calendar.DATE, day);
    //  long d = date.getTime();
    //  long n = day * 24 * 60 * 60 * 1000;
    //  d = d + n;
      return c.getTime();
     }

    /**   * @功能描述:获取指定格式的字符串   * @return   * @创建时间 2008-7-26   * @author beedoor   */  public static String getFormatDateStr(Object obj, String format)    throws Exception {   Date d = null;   SimpleDateFormat sdf = getSimpleDateFormat(format);   if (obj instanceof String) {    d = sdf.parse(obj.toString());   } else if (obj instanceof Date) {    d = (Date) obj;

      }   if (null != d) {    return sdf.format(d);   }

      return "";  }

  • 相关阅读:
    jquery
    模板库
    Luogu P1902 刺杀大使
    8.20模拟赛再次观光记
    Luogu P1122 最大子树和
    Luogu P1470 最长前缀 Longest Prefix
    8.18爆炸记
    Luogu P1388 算式
    Luogu P1103 书本整理
    8.17
  • 原文地址:https://www.cnblogs.com/Defry/p/4612403.html
Copyright © 2011-2022 走看看