歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Java裡關於日期Date的相關代碼實現

/**
  * 將指定格式的字符串轉為Date
  * @param sDate
  * @param format
  * 遵循 simpleDateFormat 規范
  * @return
  */
 public static Date stringToDate(String sDate, String format) {

  DateFormat df = new SimpleDateFormat(format);
  try {
   return df.parse(sDate);
  } catch (ParseException e) {

   throw new RuntimeException("date format error");
  }
 }
 
 /**
  * 把一個Date 按照指定格式轉換為String
  *
  * @param date
  * @return
  */
 public static String dateToString(Date date, String format) {
  DateFormat df = new SimpleDateFormat(format);
  return df.format(date);
 }

 /**
  * 計算時間差值
  * @param beginDate
  * @param endDate
  * @return
  */
 public static int computeDate(Date beginDate,Date endDate){
  Calendar calBegin=Calendar.getInstance();
  Calendar calEnd=Calendar.getInstance();
 
  calBegin.setTime(beginDate);
  calEnd.setTime(endDate);
 
  return (int)(calEnd.getTimeInMillis()-calBegin.getTimeInMillis())/1000/60/60/24;
 }
 
 /**
  * 計算一日期加上X天之後的日期
  * @param date
  * @param inteval
  * @return
  */
 public static String resetDay(String date,int inteval){
        String dateTimeFrom = "";
        try {
            Calendar calendar = new GregorianCalendar();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            calendar.clear();
            calendar.set(Integer.parseInt(date.substring(0,4)), Integer.parseInt(date.substring(5,7)) - 1, Integer
                    .parseInt(date.substring(8, 10)));
           
            calendar.add(Calendar.DAY_OF_MONTH, inteval);
            dateTimeFrom = sdf.format(calendar.getTime());
            return dateTimeFrom;
        } catch (Exception e){
            e.getMessage();
            return "";
        }

    }
 
 /**
  * 根據日期獲得對應的星期幾
  * @param argDate
  * @return
  */
 public static String getDayOfWeek(String argDate){

         if (argDate.length() != 10){
             throw new IllegalArgumentException("argDate   length  !=   8");
         }
         int intArgY = Integer.parseInt(argDate.substring(0, 4));
         int intArgM = Integer.parseInt(argDate.substring(5, 7));
         int intArgD = Integer.parseInt(argDate.substring(8, 10));

         Calendar calendar = GregorianCalendar.getInstance();
         calendar.set(intArgY, intArgM - 1, intArgD);
         int returnDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
         String showDate = "";
         switch (returnDayOfWeek){
             case 1:
                 showDate = "日";
                 break;
             case 2:
                 showDate = "一";
                 break;
             case 3:
                 showDate = "二";
                 break;
             case 4:
                 showDate = "三";
                 break;
             case 5:
                 showDate = "四";
                 break;
             case 6:
                 showDate = "五";
                 break;
             default:
                 showDate = "六";
                 break;
         }
         return showDate;
     }

 

自己封裝的DateUtil包,定期更新!如有錯誤,請指出,謝謝!

Copyright © Linux教程網 All Rights Reserved