1. 程式人生 > >時間工具類DateU

時間工具類DateU

package com.ddtkj.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

/**
 * 工具類-日期處理
 *
 * @author xx
 * @version 2.0
 * @since 2014年1月28日
 */
public class DateUtil {
  private static final long ONE_MINUTE = 60;
  private static final long ONE_HOUR = 3600;
  private static final long ONE_DAY = 86400;
  private static final long ONE_MONTH = 2592000;
  private static final long ONE_YEAR = 31104000;
 
  /**
   * 日期轉換為字串 格式自定義
   *
   * @param date
   * @param f
   * @return
   */
  public static String dateStr(Date date, String f) {
    SimpleDateFormat format = new SimpleDateFormat(f);
    String str = format.format(date);
    return str;
  }
 
  public static long getSubDay(Date d) {
    Calendar nowDate = Calendar.getInstance(), oldDate = Calendar.getInstance();
    nowDate.setTime(new Date());// 設定為當前系統時間
    oldDate.setTime(DateUtil.valueOf(DateUtil.dateStr2(d)));
    long timeNow = nowDate.getTimeInMillis();
    long timeOld = oldDate.getTimeInMillis();
    long subDay = (timeNow - timeOld) / (1000 * 60 * 60 * 24);// 化為天
    return subDay;
  }
 
  /**
   * 兩日期間的天數
   *
   * @param d
   * @return
   */
  public static long getSubDay(Date sd, Date ed) {
    Calendar startDate = Calendar.getInstance(), endDate = Calendar
        .getInstance();
    endDate.setTime(DateUtil.valueOf(DateUtil.dateStr2(ed)));// 設定為當前系統時間
    startDate.setTime(DateUtil.valueOf(DateUtil.dateStr2(sd)));
    long timeStart = startDate.getTimeInMillis();
    long timeEnd = endDate.getTimeInMillis();
    long subDay = (timeEnd - timeStart) / (1000 * 60 * 60 * 24);// 化為天
    return subDay;
  }
 
  /**
   * 兩日期間的天數
   *
   * @param d
   * @return
   */
  public static long getSubDayHour(Date sd, Date ed) {
    Calendar startDate = Calendar.getInstance(), endDate = Calendar
        .getInstance();
    endDate.setTime(DateUtil.getDate10ByStr(DateUtil.dateStr4(ed),
        "yyyy-MM-dd HH:mm:ss"));// 設定為當前系統時間
    startDate.setTime(DateUtil.getDate10ByStr(DateUtil.dateStr4(sd),
        "yyyy-MM-dd HH:mm:ss"));
    long timeStart = startDate.getTimeInMillis();
    long timeEnd = endDate.getTimeInMillis();
    long subDay = (timeEnd - timeStart);// 化為天
    return subDay;
  }
 
  /**
   * 日期轉換為字串 MM月dd日 hh:mm
   *
   * @param date
   * @return
   */
  public static String dateStr(Date date) {
    return dateStr(date, "MM月dd日 hh:mm");
  }
 
  /**
   * 日期轉換為字串 yyyy-MM-dd
   *
   * @param date
   * @return
   */
  public static String dateStr2(Date date) {
    return dateStr(date, "yyyy-MM-dd");
  }
 
  /**
   * yyyy年MM月dd日HH時mm分ss秒
   *
   * @param date
   * @return
   */
  public static String dateStr5(Date date) {
    return dateStr(date, "yyyy年MM月dd日HH時mm分ss秒");
  }
 
  /**
   * yyyyMMddHHmmss
   *
   * @param date
   * @return
   */
  public static String dateStr3(Date date) {
    return dateStr(date, "yyyyMMddHHmmss");
  }
 
  /**
   * yyyy-MM-dd HH:mm:ss
   *
   * @param date
   * @return
   */
  public static String dateStr4(Date date) {
    return dateStr(date, "yyyy-MM-dd HH:mm:ss");
    
  }
 
  /**
   * yyyy年MM月dd日
   *
   * @param date
   * @return
   */
  public static String dateStr6(Date date) {
    return dateStr(date, "yyyy年MM月dd日");
  }
 
  /**
   * yyyyMMdd
   *
   * @param date
   * @return
   */
  public static String dateStr7(Date date) {
    return dateStr(date, "yyyyMMdd");
  }
 
  /**
   * MM-dd
   *
   * @param date
   * @return
   */
  public static String dateStr8(Date date) {
    return dateStr(date, "MM-dd");
  }
 
  /**
   * yyyy年MM月
   *
   * @param date
   * @return
   */
  public static String dateStr9(Date date) {
    return dateStr(date, "yyyy年MM月");
  }
 
  /**
   * MM月dd日
   *
   * @param date
   * @return
   */
  public static String dateStr10(Date date) {
    return dateStr(date, "MM月dd日");
  }
 
  /**
   * MM
   *
   * @param date
   * @return
   */
  public static String dateStr11(Date date) {
    return dateStr(date, "MM");
  }
 
  /**
   * MMdd
   *
   * @param date
   * @return
   */
  public static String dateStr12(Date date) {
    return dateStr(date, "MMdd");
  }
 
  /**
   * dd
   *
   * @param date
   * @return
   */
  public static String dateStr13(Date date) {
    return dateStr(date, "dd");
  }
 
  /**
   * 將時間戳轉換為Date
   *
   * @param times
   * @return
   */
  public static Date getDate(String times) {
    long time = Long.parseLong(times);
    return new Date(time * 1000);
  }
 
  public static String dateStr(String times) {
    return dateStr(getDate(times));
  }
 
  public static String dateStr2(String times) {
    return dateStr2(getDate(times));
  }
 
  public static String dateStr3(String times) {
    return dateStr3(getDate(times));
  }
 
  public static String dateStr4(String times) {
    return dateStr4(getDate(times));
  }
 
  public static String dateStr5(String times) {
    return dateStr5(getDate(times));
  }
 
  /**
   * 將Date轉換為時間戳
   *
   * @param date
   * @return
   */
  public static long getTime(Date date) {
    return date.getTime() / 1000;
  }
 
  public static int getDay(Date d) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(d);
    return cal.get(Calendar.DAY_OF_MONTH);
  }
 
  /**
   * s - 表示 "yyyy-mm-dd" 形式的日期的 String 物件
   *
   * @param f
   * @return
   */
  public static Date valueOf(String s) {
    final int YEAR_LENGTH = 4;
    final int MONTH_LENGTH = 2;
    final int DAY_LENGTH = 2;
    final int MAX_MONTH = 12;
    final int MAX_DAY = 31;
    int firstDash;
    int secondDash;
    int threeDash = 0;
    int fourDash = 0;
    Date d = null;
    
    if (s == null) {
      throw new java.lang.IllegalArgumentException();
    }
    
    firstDash = s.indexOf('-');
    secondDash = s.indexOf('-', firstDash + 1);
    if (s.contains(":")) {
      threeDash = s.indexOf(':');
      fourDash = s.indexOf(':', threeDash + 1);
    }
    if ((firstDash > 0) && (secondDash > 0) && (secondDash < s.length() - 1)) {
      String yyyy = s.substring(0, firstDash);
      String mm = s.substring(firstDash + 1, secondDash);
      String dd = "";
      String hh = "";
      String MM = "";
      String ss = "";
      if (s.contains(":")) {
        dd = s.substring(secondDash + 1, threeDash - 3);
        hh = s.substring(threeDash - 2, threeDash);
        MM = s.substring(threeDash + 1, fourDash);
        ss = s.substring(fourDash + 1);
      } else {
        dd = s.substring(secondDash + 1);
      }
      if (yyyy.length() == YEAR_LENGTH && mm.length() == MONTH_LENGTH
          && dd.length() == DAY_LENGTH) {
        int year = Integer.parseInt(yyyy);
        int month = Integer.parseInt(mm);
        int day = Integer.parseInt(dd);
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (s.contains(":")) {
          hour = Integer.parseInt(hh);
          minute = Integer.parseInt(MM);
          second = Integer.parseInt(ss);
        }
        if (month >= 1 && month <= MAX_MONTH) {
          int maxDays = MAX_DAY;
          switch (month) {
          // February determine if a leap year or not
            case 2:
              if ((year % 4 == 0 && !(year % 100 == 0)) || (year % 400 == 0)) {
                maxDays = MAX_DAY - 2; // leap year so 29 days in
                // February
              } else {
                maxDays = MAX_DAY - 3; // not a leap year so 28 days
                // in February
              }
              break;
            // April, June, Sept, Nov 30 day months
            case 4:
            case 6:
            case 9:
            case 11:
              maxDays = MAX_DAY - 1;
              break;
          }
          if (day >= 1 && day <= maxDays) {
            Calendar cal = Calendar.getInstance();
            cal.set(year, month - 1, day, hour, minute, second);
            cal.set(Calendar.MILLISECOND, 0);
            d = cal.getTime();
          }
        }
      }
    }
    if (d == null) {
      throw new java.lang.IllegalArgumentException();
    }
    return d;
  }
 
  /**
   * @author lijie
   * @param Begin
   * @param end
   *          傳入開始時間 和 結束時間 格式如:2012-09-07
   * @return 返回Map 獲取相隔多少年 get("YEAR")及為倆個時間年只差,月 天,類推 Key : YEAR MONTH DAY
   *         如果開始時間 晚於 結束時間 return null;
   */
 
  @SuppressWarnings("rawtypes")
  public static Map getApartTime(String Begin, String end) {
    String[] temp = Begin.split("-");
    String[] temp2 = end.split("-");
    if (temp.length > 1 && temp2.length > 1) {
      Calendar ends = Calendar.getInstance();
      Calendar begin = Calendar.getInstance();
      
      begin.set(StringUtil.toInt(temp[0]), StringUtil.toInt(temp[1]),
          StringUtil.toInt(temp[2]));
      ends.set(StringUtil.toInt(temp2[0]), StringUtil.toInt(temp2[1]),
          StringUtil.toInt(temp2[2]));
      if (begin.compareTo(ends) < 0) {
        Map map = new HashMap();
        ends.add(Calendar.YEAR, -StringUtil.toInt(temp[0]));
        ends.add(Calendar.MONTH, -StringUtil.toInt(temp[1]));
        ends.add(Calendar.DATE, -StringUtil.toInt(temp[2]));
        map.put("YEAR", ends.get(Calendar.YEAR));
        map.put("MONTH", ends.get(Calendar.MONTH) + 1);
        map.put("DAY", ends.get(Calendar.DATE));
        return map;
      }
    }
    return null;
  }
 
  /**
   * 前/後?分鐘
   *
   * @param d
   * @param minute
   * @return
   */
  public static Date rollMinute(Date d, int minute) {
    return new Date(d.getTime() + minute * 60 * 1000);
  }
 
  /**
   * 前/後?天
   *
   * @param d
   * @param day
   * @return
   */
  public static Date rollDay(Date d, int day) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(d);
    cal.add(Calendar.DAY_OF_MONTH, day);
    return cal.getTime();
  }
 
  /**
   * 前/後?月
   *
   * @param d
   * @param mon
   * @return
   */
  public static Date rollMon(Date d, int mon) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(d);
    cal.add(Calendar.MONTH, mon);
    return cal.getTime();
  }
 
  /**
   * 前/後?年
   *
   * @param d
   * @param year
   * @return
   */
  public static Date rollYear(Date d, int year) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(d);
    cal.add(Calendar.YEAR, year);
    return cal.getTime();
  }
 
  public static Date rollDate(Date d, int year, int mon, int day) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(d);
    cal.add(Calendar.YEAR, year);
    cal.add(Calendar.MONTH, mon);
    cal.add(Calendar.DAY_OF_MONTH, day);
    return cal.getTime();
  }
 
  /**
   * 獲取當前時間-時間戳字串
   *
   * @return
   */
  public static String getNowTimeStr() {
    String str = Long.toString(System.currentTimeMillis() / 1000);
    return str;
  }
 
  /**
   * 將Date轉換為時間戳
   *
   * @param time
   * @return
   */
  public static String getTimeStr(Date time) {
    long date = time.getTime();
    String str = Long.toString(date / 1000);
    return str;
  }
 
  public static String getTimeStr(String dateStr, String format) {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    Date date;
    try {
      date = sdf.parse(dateStr);
    } catch (ParseException e) {
      e.printStackTrace();
      return "";
    }
    String str = DateUtil.getTimeStr(date);
    return str;
  }
 
  public static String rollMonth(Date addTime, String time_limit) {
    Date t = DateUtil.rollDate(addTime, 0, StringUtil.toInt(time_limit), 0);
    return t.getTime() / 1000 + "";
  }
 
  public static String rollDay(Date addTime, String time_limit_day) {
    Date t = DateUtil.rollDate(addTime, 0, 0,
        StringUtil.toInt(time_limit_day));
    return t.getTime() / 1000 + "";
  }
 
  public static Date getIntegralTime() {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTime();
  }
 
  public static Date getLastIntegralTime() {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, 23);
    cal.set(Calendar.SECOND, 59);
    cal.set(Calendar.MINUTE, 59);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTime();
  }
 
  public static Date getLastSecIntegralTime(Date d) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(d.getTime());
    cal.set(Calendar.HOUR_OF_DAY, 23);
    cal.set(Calendar.SECOND, 59);
    cal.set(Calendar.MINUTE, 59);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTime();
  }
 
  public static long getTime(String format) {
    long t = 0;
    if (StringUtil.isBlank(format))
      return t;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date;
    try {
      date = sdf.parse(format);
      t = date.getTime() / 1000;
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return t;
  }
 
  // 獲取本週日的日期
  public static String getCurrentWeekday() {
    int weeks = 0;
    int mondayPlus = DateUtil.getMondayPlus();
    GregorianCalendar currentDate = new GregorianCalendar();
    currentDate.add(GregorianCalendar.DATE, mondayPlus + 6);
    Date monday = currentDate.getTime();
    
    DateFormat df = DateFormat.getDateInstance();
    String preMonday = df.format(monday);
    return preMonday;
  }
 
  // 獲得當前日期與本週日相差的天數
  private static int getMondayPlus() {
    Calendar cd = Calendar.getInstance();
    // 獲得今天是一週的第幾天,星期日是第一天,星期二是第二天......
    int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因為按中國禮拜一作為第一天所以這裡減1
    if (dayOfWeek == 1) {
      return 0;
    } else {
      return 1 - dayOfWeek;
    }
  }
 
  // 獲得本週一的日期
  public static String getMondayOFWeek() {
    int weeks = 0;
    int mondayPlus = DateUtil.getMondayPlus();
    GregorianCalendar currentDate = new GregorianCalendar();
    currentDate.add(GregorianCalendar.DATE, mondayPlus);
    Date monday = currentDate.getTime();
    
    DateFormat df = DateFormat.getDateInstance();
    String preMonday = df.format(monday);
    return preMonday;
  }
 
  // 獲取當前月第一天:
  public static String getFirstDayOfMonth(String first) {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    c.add(Calendar.MONTH, 0);
    c.set(Calendar.DAY_OF_MONTH, 1);// 設定為1號,當前日期既為本月第一天
    first = format.format(c.getTime());
    return first;
  }
 
  // 獲取當月最後一天
  public static String getLastDayOfMonth(String last) {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Calendar ca = Calendar.getInstance();
    ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
    last = format.format(ca.getTime());
    return last;
  }
 
  /**
   * 日期月份處理
   *
   * @param d
   *          時間
   * @param month
   *          相加的月份,正數則加,負數則減
   * @return
   */
  public static Date timeMonthManage(Date d, int month) {
    Calendar rightNow = Calendar.getInstance();
    rightNow.setTime(d);
    rightNow.add(Calendar.MONTH, month);
    return rightNow.getTime();
  }
 
  /**
   * 獲取指定年月的最後一天
   *
   * @param year_time
   *          指定年
   * @param month_time
   *          指定月
   * @return
   */
  public static Date monthLastDay(int year_time, int month_time) {
    Calendar cal = Calendar.getInstance();
    cal.set(year_time, month_time, 0, 23, 59, 59);
    return cal.getTime();
  }
 
  /**
   * 獲取指定年月的第一天
   *
   * @param year_time
   *          指定年
   * @param month_time
   *          指定月
   * @return
   */
  public static Date monthFirstDay(int year_time, int month_time) {
    Calendar cal = Calendar.getInstance();
    cal.set(year_time, month_time - 1, 1, 0, 0, 0);
    return cal.getTime();
  }
 
  /**
   * 獲取指定時間月的第一天
   *
   * @param d
   *          指定時間,為空代表當前時間
   * @return
   */
  public static Date currMonthFirstDay(Date d) {
    Calendar cal = Calendar.getInstance();
    if (d != null)
      cal.setTime(d);
    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
    cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
        cal.get(Calendar.DATE), 0, 0, 0);
    return cal.getTime();
  }
 
  /**
   * 獲取指定時間月的最後一天
   *
   * @param d
   *          指定時間,為空代表當前時間
   * @return
   */
  public static Date currMonthLastDay(Date d) {
    Calendar cal = Calendar.getInstance();
    if (d != null)
      cal.setTime(d);
    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
        cal.get(Calendar.DATE), 23, 59, 59);
    return cal.getTime();
  }
 
  /**
   * 距離今天多久
   *
   * @param date
   * @return
   *
   */
  public static String fromToday(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    
    long time = date.getTime() / 1000;
    long now = new Date().getTime() / 1000;
    long ago = now - time;
    if (ago <= ONE_HOUR)
      return ago / ONE_MINUTE + "分鐘前";
    else if (ago <= ONE_DAY)
      return ago / ONE_HOUR + "小時" + (ago % ONE_HOUR / ONE_MINUTE) + "分鐘前";
    else if (ago <= ONE_DAY * 2)
      return "昨天" + calendar.get(Calendar.HOUR_OF_DAY) + "點"
          + calendar.get(Calendar.MINUTE) + "分";
    else if (ago <= ONE_DAY * 3)
      return "前天" + calendar.get(Calendar.HOUR_OF_DAY) + "點"
          + calendar.get(Calendar.MINUTE) + "分";
    else if (ago <= ONE_MONTH) {
      long day = ago / ONE_DAY;
      return day + "天前" + calendar.get(Calendar.HOUR_OF_DAY) + "點"
          + calendar.get(Calendar.MINUTE) + "分";
    } else if (ago <= ONE_YEAR) {
      long month = ago / ONE_MONTH;
      long day = ago % ONE_MONTH / ONE_DAY;
      return month + "個月" + day + "天前" + calendar.get(Calendar.HOUR_OF_DAY)
          + "點" + calendar.get(Calendar.MINUTE) + "分";
    } else {
      long year = ago / ONE_YEAR;
      int month = calendar.get(Calendar.MONTH) + 1;// JANUARY which is 0
      // so month+1
      return year + "年前" + month + "月" + calendar.get(Calendar.DATE) + "日";
    }
    
  }
 
  /**
   * 獲取指定時間的年
   *
   * @param date
   *          指定時間
   * @return
   */
  public static int getTimeYear(Date date) {
    if (date == null)
      date = new Date();
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal.get(Calendar.YEAR);
  }
 
  /**
   * 獲取指定時間的月
   *
   * @param date
   *          指定時間
   * @return
   */
  public static int getTimeMonth(Date date) {
    if (date == null)
      date = new Date();
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal.get(Calendar.MONTH) + 1;
  }
 
  /**
   * 獲取指定時間的天
   *
   * @param date
   *          指定時間
   * @return
   */
  public static int getTimeDay(Date date) {
    if (date == null)
      date = new Date();
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal.get(Calendar.DATE);
  }
 
  public static Date getFirstSecIntegralTime(Date d) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(d.getTime());
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.MILLISECOND, 0);
    cal.set(Calendar.DATE, 0);
    return cal.getTime();
  }
 
  /**
   * 獲取指定時間天的結束時間
   *
   * @param d
   * @return
   */
  public static Date getDayEndTime(long d) {
    Date day = new Date(d * 1000);
    if (d <= 0)
      day = new Date();
    Calendar cal = Calendar.getInstance();
    if (day != null)
      cal.setTimeInMillis(day.getTime());
    cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
        cal.get(Calendar.DATE), 23, 59, 59);
    return cal.getTime();
  }
 
  /**
   * 獲取指定時間天的開始時間
   *
   * @param d
   * @return
   */
  public static Date getDayStartTime(long d) {
    Date day = new Date(d * 1000);
    if (d <= 0)
      day = new Date();
    Calendar cal = Calendar.getInstance();
    if (day != null)
      cal.setTimeInMillis(day.getTime());
    cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
        cal.get(Calendar.DATE), 0, 0, 0);
    return cal.getTime();
  }
 
  /**
   * 獲取19位的格式時間
   *
   * @param dateStr
   * @return
   * @throws ParseException
   */
  public static Date getDateByFullDateStr(String dateStr) {
    try {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      return sdf.parse(dateStr);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
 
  /**
   * 獲取10位的格式時間
   *
   * @param dateStr
   * @return
   * @throws ParseException
   */
  public static Date getDate10ByStr(String dateStr, String format) {
    try {
      SimpleDateFormat sdf = new SimpleDateFormat(format);
      return sdf.parse(dateStr);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
 
  /**
   * 獲得當前年份和月
   *
   * @param date
   * @param i
   * @return
   */
  public static String currYearMonth(Date date, int i) {
    String currDate;
    
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int month = cal.get(Calendar.MONTH) + i;
    int year = cal.get(Calendar.YEAR);
    currDate = year + String.format("%02d", month);
    return currDate;
  }
 
  /**
   * 字串轉為日期
   *
   * @param dateTime
   * @param format
   * @return
   */
  public static Date str2Date(String dateTime, String format) {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    Date date = null;
    try {
      date = sdf.parse(dateTime);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return date;
  }
  /**
   * 日期轉換字串
   *
   * @param dateTime
   * @param format
   * @return
   */
  public static String date2Str(Date date, String f) {
    SimpleDateFormat format = new SimpleDateFormat(f);
    String str = format.format(date);
    return str;
  }
 
  /**
   * 在日期上增加天數
   * @param date
   * @param n
   * @return
   */
  public static Date addDay(Date date, int n) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.DATE, n);
    return cal.getTime();
  }
 
 
  /**
   * 虛擬資產列表釋出時間格式化
   * @author

[email protected]
   * @date 2018-04-18
   * @return
   */
  public static String publishTimeFormat(Date publishTime) {
      
      //剛剛釋出:一小時內釋出
      //今日釋出:1小時前-24小時內
      //昨日釋出:24小時前-48小時內
      //三日內釋出:48小時前-72小時
      //七日內釋出:72小時前-7天內
      //七日前釋出:超過7天的
      if(publishTime!=null){
        long timeDiff = System.currentTimeMillis()-publishTime.getTime();
        
        if(timeDiff<=1*60*60*1000) {
          return "剛剛釋出";
        } else if(timeDiff>1*60*60*1000 && timeDiff<= 24*60*60*1000) {
          return "今日釋出";
        } else if(timeDiff>48*60*60*1000 && timeDiff<= 72*60*60*1000) {
          return "三日內釋出";
        } else if(timeDiff>72*60*60*1000 && timeDiff<= 168*60*60*1000) {
          return "七日內釋出";
        } else {
          return "七日前釋出";
        }
      }
      return "剛剛釋出";
      
  }
 
  /**
     * 虛擬資產列表市場動態格式化
     * @author wangmingxi
     * @date 2018.4.25
     * @param date
     * @return
     */
    public static String formationDate(Date date) {  
        String dateString = "";  
        // 獲取系統當前時間  
        Date now = new Date();  
        long endTime = now.getTime();   
        long currentTime= date.getTime();   
        // 計算兩個時間點相差的秒數  
        long seconds = (endTime - currentTime);  
        if (seconds<60*60*1000) {  
             dateString = seconds/1000/60+"分鐘前";  
        }else if (seconds<60*60*24*1000) {  
             dateString = seconds/1000/60/60+"小時前";  
        }else if (seconds>60*60*24*1000) {  
             dateString =seconds/1000/60/60/24+ "天前";  
        }  
        return dateString;  
    }
}

public List<String> getBetweenDate(long startTime, long endTime){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    // 宣告儲存日期集合
    List<String> list = new ArrayList<String>();

    Date dateStart = new Date(startTime*1000L);
    Date endStart = new Date(endTime*1000L);
    String start = sdf.format(dateStart);
    String end = sdf.format(endStart);
    try {
        // 轉化成日期型別
        Date startDate = sdf.parse(start.toString());
        Date endDate = sdf.parse(end.toString());

        //用Calendar 進行日期比較判斷
        Calendar calendar = Calendar.getInstance();
        while (startDate.getTime()<=endDate.getTime()){
            // 把日期新增到集合
            list.add(sdf.format(startDate));
            // 設定日期
            calendar.setTime(startDate);
            //把日期增加一天
            calendar.add(Calendar.DATE, 1);
            // 獲取增加後的日期
            startDate=calendar.getTime();
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return list;
}

private int dayForWeek(String pTime) throws Exception {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    c.setTime(format.parse(pTime));
    int dayForWeek = 0;
    if(c.get(Calendar.DAY_OF_WEEK) == 1){
        dayForWeek = 7;
    }else{
        dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
    }
    return dayForWeek;
}