Java时间工具类

jopen 6年前

public class TimeUtil {   /**    * 日期字符串转换 pattern可以从R.string中获取,datetime_pattern_yyyymmddhhmmss,    * datetime_pattern_yyyy_mm_dd_hhmmss,    * date_pattern_chinese,datetime_pattern_chinese等    *     * @param originalPattern    *            初始日期格式    * @param targetPattern    *            目标日期格式    * @param datetime    *            日期字符串    * @return    */   public static String formatDateTime(Context context, int originalPatternStrId, int targetPatternStrId, String datetime) {    SimpleDateFormat sdf = new SimpleDateFormat(context.getString(originalPatternStrId), Locale.getDefault());    Date date = null;    try {     date = sdf.parse(datetime);    } catch (ParseException e) {     e.printStackTrace();    }    if (date != null) {     sdf = new SimpleDateFormat(context.getString(targetPatternStrId), Locale.getDefault());     String target = sdf.format(date);     return target;    }    return null;     }     /**    * @param strDate    *            e.g. "2012-12-12 12:12:12"    * @return Wed Dec 12 12:12:12 CST 2012    */   public static Date string2Date(String strDate) {    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");    Date date = null;    try {     date = dateFormat.parse(strDate);    } catch (ParseException e) {     try {      // 第二种格式方案      date = dateFormat2.parse(strDate);     } catch (ParseException e1) {      e1.printStackTrace();      return null;     }    }    return date;   }     /**    * @param strDate    *            2014-02-17    * @return long    */   public static long string2Long(String strDate) {    Date date = string2Date(strDate);    return date.getTime();   }     public static long string2Long2(String time) {    Date date = null;    try {     date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(time);    } catch (Exception e) {     return 0;    }    return date.getTime();   }     /**    * time应该为13位    *     * @param time    * @return yyyy-MM-dd HH:mm:ss    */   public static String long2String(long time) {    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    Date date = new Date(time);    String str = dateFormat.format(date);    return str;   }     /**    * @param time    * @return yyyy-MM-dd    */   public static String long2String2(long time) {    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");    Date date = new Date(time);    String str = dateFormat.format(date);    return str;   }     /**    * time应该为13位    *     * @param time    * @return M月d日 HH:mm    */   public static String long2String3(long time) {    DateFormat dateFormat = new SimpleDateFormat("M月d日 HH:mm");    Date date = new Date(time);    String str = dateFormat.format(date);    return str;   }     /**    * @param time    * @return yy-MM-dd    */   public static String long2String4(long time) {    DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd");    Date date = new Date(time);    String str = dateFormat.format(date);    return str;   }     /**    * @param time    *            yyyy-MM-dd HH:mm:ss or yyyy-MM-ddTHH:mm:ss    * @return yyyy-MM-dd    */   public static String trim(String time) {    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");    Date date = string2Date(time);    String str = dateFormat.format(date);    return str;   }     /**    * @param time    *            yyyy-MM-dd HH:mm:ss    * @return yyyy-MM-dd    */   public static String trim2(String time) {    DateFormat dateFormat = new SimpleDateFormat("M月d日 HH:mm");    Date date = string2Date(time);    String str = dateFormat.format(date);    return str;   }     /**    * @param time    *            yyyy-MM-dd HH:mm:ss    * @return HH:mm    */   public static String trim3(String time) {    DateFormat dateFormat = new SimpleDateFormat("HH:mm");    Date date = string2Date(time);    String str = dateFormat.format(date);    return str;   }     /**    * @param time    *            yyyy-MM-dd    * @return MM-dd    */   public static String string2String(String time) {    DateFormat dateFormat = new SimpleDateFormat("MM-dd");    Date date = string2Date(time);    String str = dateFormat.format(date);    return str;   }     /**    * @param strDate    *            yyyy-MM-dd    * @return M or d    */   public static String string2String2(String strDate) {    DateFormat dateFormat = new SimpleDateFormat("M-d");    Date date = string2Date(strDate);    String str = dateFormat.format(date);    String[] temp = str.split("-");    if (temp[1].equals("1")) {     return temp[0] + "月";    } else {     return temp[1];    }   }     /**    * @param time    *            yyyy-MM-ddTHH:mm:ss    * @return yyyy-MM-dd HH:mm:ss    */   public static String string2String3(String time) {    Date date = null;    try {     date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(time);    } catch (ParseException e) {     e.printStackTrace();    }    String str = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);    return str;   }     /**    * @param time    *            yyyy-MM-ddTHH:mm:ss    * @return yy-MM-dd    */   public static String string2String4(String time) {    Date date = null;    try {     date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(time);    } catch (Exception e) {     return null;    }    String str = new SimpleDateFormat("yyyy-MM-dd").format(date);    return str;   }     /**    * @param time    *            yyyy-MM-dd    * @return yy-MM-dd    */   public static String string2String5(String time) {    Date date = null;    try {     date = new SimpleDateFormat("yyyy-MM-dd").parse(time);    } catch (Exception e) {     return null;    }    String str = new SimpleDateFormat("yy-MM-dd").format(date);    return str;   }     /**    * 获取某一天距离今天多少天    *     * @param date    *            yyyy-MM-dd'T'HH:mm:ss    * @return    */   public static int getPassedDays(String date) {    SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");    int day = 0;    try {     Date passedDate = myFormatter.parse(date);     day = (int) ((System.currentTimeMillis() - passedDate.getTime()) / (24 * 60 * 60 * 1000)) + 1;    } catch (Exception e) {     return 0;    }    return day;   }     /**    *     * 获取当天开始时间    *     * @param index    *            0为当天,1为前一天,以此类推    *     * @return e.g. 1391702400    */   public static long getStartTime(int index) {    Calendar todayStart = Calendar.getInstance();    todayStart.set(Calendar.HOUR, 0);    todayStart.set(Calendar.MINUTE, 0);    todayStart.set(Calendar.SECOND, 0);    todayStart.set(Calendar.MILLISECOND, 0);    return todayStart.getTime().getTime() / 1000 - index * 3600 * 24;   }     /**    * 获取当天结束时间    *     * @param index    *            0为当天,1为前一天,以此类推    *     * @return e.g. 1391788799    */   public static long getEndTime(int index) {    Calendar todayEnd = Calendar.getInstance();    todayEnd.set(Calendar.HOUR, 23);    todayEnd.set(Calendar.MINUTE, 59);    todayEnd.set(Calendar.SECOND, 59);    todayEnd.set(Calendar.MILLISECOND, 999);    return todayEnd.getTime().getTime() / 1000 - index * 3600 * 24;   }     /**    *     * @param minute    *            140    * @return 2小时20分钟    */   public static String second2Hour(int second) {    int minute = second / 60;    if (second % 60 > 30) {     minute += 1;    }    boolean hadAppend = false;    StringBuilder builder = new StringBuilder();    if (minute >= 1440) {     builder.append(minute / 1440 + "天");     minute %= 1440;     hadAppend = true;    }    if (minute >= 60) {     builder.append(minute / 60 + "小时");     minute %= 60;     hadAppend = true;    }    if (!hadAppend || minute != 0) {     builder.append(minute + "分钟");    }      return builder.toString();   }     /**    *     * @param date    *            2014-02-12    * @return    */   public static boolean isToday(String date) {    String today = TimeUtil.long2String2(Calendar.getInstance().getTimeInMillis());    if (date.equals(today)) {     return true;    }    return false;   }     /**    *     * @param date    *            2014-02-12    * @return    */   public static boolean isYesterday(String date) {    String yesterday = TimeUtil.long2String2(Calendar.getInstance().getTimeInMillis() - 3600 * 24 * 1000L);    if (date.equals(yesterday)) {     return true;    }    return false;   }     /**    *     * @param date    *            2014-02-12    * @return    */   public static boolean isTheDayBeforeYesterday(String date) {    String theDayBeforeYesterday = TimeUtil.long2String2(Calendar.getInstance().getTimeInMillis() - 3600 * 24 * 1000L * 2);    if (date.equals(theDayBeforeYesterday)) {     return true;    }    return false;   }     /**    *     * @param date    *            2014-02-12    * @return    */   public static String theDayOfWeek(String date) {    Calendar calendar = Calendar.getInstance();    calendar.setTime(string2Date(date));    int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);    String temp = null;    switch (dayOfWeek) {    case 1:     temp = "周日";     break;    case 2:     temp = "周一";     break;    case 3:     temp = "周二";     break;    case 4:     temp = "周三";     break;    case 5:     temp = "周四";     break;    case 6:     temp = "周五";     break;    case 7:     temp = "周六";     break;      default:     break;    }    return temp;   }     public static String convert(String mDate) {    if (TimeUtil.isToday(mDate)) {     return "今天";    } else if (TimeUtil.isYesterday(mDate)) {     return "昨天";    } else if (TimeUtil.isTheDayBeforeYesterday(mDate)) {     return "前天";    } else {     return TimeUtil.theDayOfWeek(mDate);    }   }     /**    * 获取同步时间    *     * @return    */   public static String recentUpdateTime() {    long recentUpdateTime = PreferenceUtil.getLong(Preferences.RECENT_UPDATE_TIME);    long currentTime = System.currentTimeMillis();    int temp = (int) ((currentTime - recentUpdateTime) / 1000);// 换算成以秒为单位    String reString = null;    if (temp < 60) {     reString = "刚刚同步";    } else if (temp < 1800) {     reString = temp / 60 + "分钟前同步";    } else if (temp < 3600) {     reString = "半小时前同步";    } else if (temp < 3600 * 24) {     reString = temp / 3600 + "小时前同步";    } else if (temp < 3600 * 24 * 7) {     reString = temp / (3600 * 24) + "天前同步";    } else if (temp < 3600 * 24 * 7 * 4) {     reString = temp / (3600 * 24 * 7) + "周前同步";    } else if (temp < 3600 * 24 * 30 * 12) {     reString = temp / (3600 * 24 * 30) + "个月前同步";    } else if (temp < 3600 * 24 * 365 * 10) {     reString = temp / (3600 * 24 * 365) + "年前同步";    } else {     reString = long2String(recentUpdateTime);    }    if (recentUpdateTime == 0) {     reString = "";    }    return reString;   }     /**    * 根据给定日期,添加一天时间    *     * @param date    *            yyyy-mm-dd    * @return yyyy-mm-dd    */   public static String obtainEndDate(String date) {    return TimeUtil.long2String(TimeUtil.string2Long(date) + 86400 * 1000);   }  }