• java处理日期时间代码


    public static String FORMATE_DATE_STR = "yyyy-MM-dd";
     public static String FORMATE_TIME_STR = "yyyy-MM-dd HH:mm:ss";
     public static String FORMATE_TIMESTAMPE_STR = "HH:mm";
    
     /**
      * @author jerry.chen
      * @return 返回时间格式为:yyyy-MM-dd 2010-12-09
      */
     public static String getCurrentDate(Date date) {
      SimpleDateFormat dateFormate = new SimpleDateFormat(FORMATE_DATE_STR);
      return dateFormate.format(date);
     }
    
     /**
      * @author jerry.chen
      * @return 返回时间格式为:yyyy-MM-dd HH:mm:ss 2010-12-09
      */
     public static String getCurrentDate(Date date, String formateStr) {
      SimpleDateFormat dateFormate = new SimpleDateFormat(formateStr);
      return dateFormate.format(date);
     }
    
     /**
      * @author jerry.chen
      * @param dateStr
      * @return 将日期字符串(yyyy-MM-dd或yyyy-MM-dd HH:mm:ss)转换为日期类型;
      */
     public static Date convertStringToDate(String dateStr, String formaterStr) {
      if ((null == dateStr) || (null == formaterStr)) {
       return null;
      }
      try {
       SimpleDateFormat formate = new SimpleDateFormat(formaterStr);
       return formate.parse(dateStr);
      } catch (Exception e) {
       e.printStackTrace();
       return null;
      }
     }
    
     /**
      * @author wagon
      * @param cal
      * @param format
      * @return 将XMLGregorianCalendar转换为Date
      */
     public static Date XMLGconvertToDate(XMLGregorianCalendar cal, String format) {
      if ((null == cal) || (null == format))
       return null;
      try {
       GregorianCalendar ca = cal.toGregorianCalendar();
       SimpleDateFormat formatter = new SimpleDateFormat(format);
       String dateF = formatter.format(ca.getTime());
       return formatter.parse(dateF);
      } catch (Exception e) {
       e.printStackTrace();
       return null;
      }
     }
    
     /**
      * @author jerry.chen
      * @param dateStr
      * @return 将格式为:yyyy-MM-dd/yyyy-MM-dd HH:mm:ss 的字符串转换为Calendar的对象;
      */
     public static Calendar convertStringToCalendar(String dateStr,
       String dateFormateStr) {
      Calendar calendar = Calendar.getInstance();
      Date date = convertStringToDate(dateStr, dateFormateStr);
      calendar.setTime(date);
      return calendar;
     }
    
     /**
      * @author jerry.chen
      * @param currentDateStr
      * @param addDateCount
      *            增量
      * @param dateFormateStr
      * @return 返回当前日期加几天之后的日期结果 如:如果当前为2011-05-25 那么加7天之后,则为 2011-06-01
      */
     public static String addDateString(String currentDateStr, int addDateCount,
       String dateFormateStr) {
      Calendar calendar = convertStringToCalendar(currentDateStr,
        dateFormateStr);
      calendar.add(Calendar.DAY_OF_MONTH, addDateCount);
      String addDateStr = getDateFmtStrFromCST(String.valueOf(calendar
        .getTime()), dateFormateStr);
      return addDateStr;
     }
    

    根据日期字符串获取日期毫秒数

    /**
      * @author jerry.chen
      * @param dateStr
      * @return
      * @throws ParseException
      *             根据日期字符串获取日期毫秒数;
      */
     public static long stringConvertToLong(String dateStr)
       throws ParseException {
      SimpleDateFormat format = new SimpleDateFormat(FORMATE_DATE_STR);
      return format.parse(dateStr).getTime();
     }
    /**
      * @author jerry.chen
      * @param cststr
      * @param fmt
      * @return 将cst日期格式转换yyyy-mm-dd
      */
     public static String getDateFmtStrFromCST(String cststr, String fmt) {
      if ((null == cststr) || (null == fmt)) {
       return null;
      }
      String str = null;
      SimpleDateFormat sdfy = new SimpleDateFormat(fmt.trim());
      SimpleDateFormat sdf = new SimpleDateFormat(
        "EEE MMM dd HH:mm:ss 'CST' yyyy", Locale.US);
      try {
       str = sdfy.format(sdf.parse(cststr.trim()));
      } catch (ParseException e) {
       e.printStackTrace();
       return null;
      }
      return str;
     }
    /**
      * @author jerry.chen
      * @param beginDate
      * @param endDate
      * @param parseStr
      * @return 比较日期大小
      */
     public static int comparedDate(String beginDate, String endDate,
       String parseStr) {
      int comparedValue = 0;
      try {
       SimpleDateFormat formateObj = new SimpleDateFormat(parseStr);
       comparedValue = formateObj.parse(beginDate).compareTo(
         formateObj.parse(endDate));
      } catch (java.text.ParseException e) {
       e.printStackTrace();
       return comparedValue;
      }
      return comparedValue;
     }</pre>
    public static Date getConvertDateStr(String dateStr, String timeStr) {
      SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm");
      try {
       return formate.parse(dateStr + " " + timeStr);
      } catch (ParseException e) {
       e.printStackTrace();
      }
      return null;
     }
    
     /**
      * @author wagon
      * @param dateStr
      *            XMLGregorianCalendar格式的String
      * @param timeStr
      * @return
      */
     public static Date getConvertDateStrByXMLGStr(String dateStr, String timeStr) {
      try {
       String dateS = dateStr.substring(0, 10);
       SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm");
       return formate.parse(dateS + " " + timeStr);
      } catch (ParseException e) {
       e.printStackTrace();
      }
      return null;
     }
    
     /**
      * @author Administrator
      * @param timeStr
      * @param formatStr
      * @return 将时间段转换为日期格式
      */
     public static String getConvertTimeStr(String timeStr) {
      SimpleDateFormat formate = new SimpleDateFormat("HH:mm:ss");
      try {
       return formate.format(formate.parse(timeStr));
      } catch (ParseException e) {
       e.printStackTrace();
      }
      return null;
     }
    <pre>
    
    沉淀,是一种境界!
  • 相关阅读:
    【刷题】BZOJ 3626 [LNOI2014]LCA
    【刷题】UOJ #207 共价大爷游长沙
    【刷题】COGS 2701 动态树
    【刷题】BZOJ 4530 [Bjoi2014]大融合
    【刷题】BZOJ 2959 长跑
    【刷题】BZOJ 1969 [Ahoi2005]LANE 航线规划
    【刷题】BZOJ 4998 星球联盟
    【刷题】BZOJ 1977 [BeiJing2010组队]次小生成树 Tree
    【刷题】BZOJ 4817 [Sdoi2017]树点涂色
    获取元素 在网页中的 坐标
  • 原文地址:https://www.cnblogs.com/java-chen/p/3141559.html
Copyright © 2020-2023  润新知