时间处理工具类TimeUtil

转自:https://cnblogs.com/ityouknow/p/5662753.html

功能

Date与String之间的互相转换,以及一些特殊格式的时间字符串处理

代码

  1 /**
  2  * 类名:TimeUtil.java 类描述:时间处理工具
  3  *
  4  * @author wader 创建时间:2011-12-02 11:03
  5  */
  6 public class TimeUtil {
  7  public final static String FORMAT_DATE = "yyyy-MM-dd";
  8  public final static String FORMAT_TIME = "hh:mm";
  9  public final static String FORMAT_DATE_TIME = "yyyy-MM-dd hh:mm";
 10  public final static String FORMAT_MONTH_DAY_TIME = "MM月dd日 hh:mm";
 11  private static SimpleDateFormat sdf = new SimpleDateFormat();
 12  private static final int YEAR = 365 * 24 * 60 * 60;//
 13  private static final int MONTH = 30 * 24 * 60 * 60;//
 14  private static final int DAY = 24 * 60 * 60;//
 15  private static final int HOUR = 60 * 60;// 小时
 16  private static final int MINUTE = 60;// 分钟
 17 
 18  /**
 19   * 根据时间戳获取描述性时间,如3分钟前,1天前
 20   *
 21   * @param timestamp
 22   *            时间戳 单位为毫秒
 23   * @return 时间字符串
 24   */
 25  public static String getDescriptionTimeFromTimestamp(long timestamp) {
 26   long currentTime = System.currentTimeMillis();
 27   long timeGap = (currentTime - timestamp) / 1000;// 与现在时间相差秒数
 28   System.out.println("timeGap: " + timeGap);
 29   String timeStr = null;
 30   if (timeGap > YEAR) {
 31    timeStr = timeGap / YEAR + "年前";
 32   } else if (timeGap > MONTH) {
 33    timeStr = timeGap / MONTH + "个月前";
 34   } else if (timeGap > DAY) {// 1天以上
 35    timeStr = timeGap / DAY + "天前";
 36   } else if (timeGap > HOUR) {// 1小时-24小时
 37    timeStr = timeGap / HOUR + "小时前";
 38   } else if (timeGap > MINUTE) {// 1分钟-59分钟
 39    timeStr = timeGap / MINUTE + "分钟前";
 40   } else {// 1秒钟-59秒钟
 41    timeStr = "刚刚";
 42   }
 43   return timeStr;
 44  }
 45 
 46  /**
 47   * 根据时间戳获取指定格式的时间,如2011-11-30 08:40
 48   *
 49   * @param timestamp
 50   *            时间戳 单位为毫秒
 51   * @param format
 52   *            指定格式 如果为null或空串则使用默认格式"yyyy-MM-dd HH:MM"
 53   * @return
 54   */
 55  public static String getFormatTimeFromTimestamp(long timestamp,
 56    String format) {
 57   if (format == null || format.trim().equals("")) {
 58    sdf.applyPattern(FORMAT_DATE);
 59    int currentYear = Calendar.getInstance().get(Calendar.YEAR);
 60    int year = Integer.valueOf(sdf.format(new Date(timestamp))
 61      .substring(0, 4));
 62    System.out.println("currentYear: "+currentYear);
 63    System.out.println("year: "+year);
 64    if (currentYear == year) {//如果为今年则不显示年份
 65     sdf.applyPattern(FORMAT_MONTH_DAY_TIME);
 66    } else {
 67     sdf.applyPattern(FORMAT_DATE_TIME);
 68    }
 69   } else {
 70    sdf.applyPattern(format);
 71   }
 72   Date date = new Date(timestamp);
 73   return sdf.format(date);
 74  }
 75 
 76  /**
 77   * 根据时间戳获取时间字符串,并根据指定的时间分割数partionSeconds来自动判断返回描述性时间还是指定格式的时间
 78   *
 79   * @param timestamp
 80   *            时间戳 单位是毫秒
 81   * @param partionSeconds
 82   *            时间分割线,当现在时间与指定的时间戳的秒数差大于这个分割线时则返回指定格式时间,否则返回描述性时间
 83   * @param format
 84   * @return
 85   */
 86  public static String getMixTimeFromTimestamp(long timestamp,
 87    long partionSeconds, String format) {
 88   long currentTime = System.currentTimeMillis();
 89   long timeGap = (currentTime - timestamp) / 1000;// 与现在时间相差秒数
 90   if (timeGap <= partionSeconds) {
 91    return getDescriptionTimeFromTimestamp(timestamp);
 92   } else {
 93    return getFormatTimeFromTimestamp(timestamp, format);
 94   }
 95  }
 96 
 97  /**
 98   * 获取当前日期的指定格式的字符串
 99   *
100   * @param format
101   *            指定的日期时间格式,若为null或""则使用指定的格式"yyyy-MM-dd HH:MM"
102   * @return
103   */
104  public static String getCurrentTime(String format) {
105   if (format == null || format.trim().equals("")) {
106    sdf.applyPattern(FORMAT_DATE_TIME);
107   } else {
108    sdf.applyPattern(format);
109   }
110   return sdf.format(new Date());
111  }
112 
113  /**
114   * 将日期字符串以指定格式转换为Date
115   *
116   * @param time
117   *            日期字符串
118   * @param format
119   *            指定的日期格式,若为null或""则使用指定的格式"yyyy-MM-dd HH:MM"
120   * @return
121   */
122  public static Date getTimeFromString(String timeStr, String format) {
123   if (format == null || format.trim().equals("")) {
124    sdf.applyPattern(FORMAT_DATE_TIME);
125   } else {
126    sdf.applyPattern(format);
127   }
128   try {
129    return sdf.parse(timeStr);
130   } catch (ParseException e) {
131    return new Date();
132   }
133  }
134 
135  /**
136   * 将Date以指定格式转换为日期时间字符串
137   *
138   * @param date
139   *            日期
140   * @param format
141   *            指定的日期时间格式,若为null或""则使用指定的格式"yyyy-MM-dd HH:MM"
142   * @return
143   */
144  public static String getStringFromTime(Date time, String format) {
145   if (format == null || format.trim().equals("")) {
146    sdf.applyPattern(FORMAT_DATE_TIME);
147   } else {
148    sdf.applyPattern(format);
149   }
150   return sdf.format(time);
151  }
152 }

使用范例

 1 public class Test {
 2  public static void main(String[] args) {
 3   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
 4   String timeStr = "2010-11-30 10:12:23";
 5   try {
 6    Date date = sdf.parse(timeStr);
 7 
 8    System.out.println(TimeUtil.getDescriptionTimeFromTimestamp(date
 9      .getTime()));
10    System.out.println(TimeUtil.getDescriptionTimeFromTimestamp(new Date()
11      .getTime()));
12    
13    System.out.println(TimeUtil.getFormatTimeFromTimestamp(date.getTime(),
14    "yyyy年MM月dd日"));
15    System.out.println(TimeUtil.getFormatTimeFromTimestamp(date.getTime(),
16      null));
17    System.out.println(TimeUtil.getFormatTimeFromTimestamp(new Date().getTime(),
18      null));
19    
20    System.out.println(TimeUtil.getMixTimeFromTimestamp(date.getTime(),
21      3*24 * 60 * 60, "yyyy年MM月dd日 hh:mm"));
22    System.out.println(TimeUtil.getMixTimeFromTimestamp(date.getTime(),
23      24 * 60 * 60, null)); 
24    System.out.println(TimeUtil.getMixTimeFromTimestamp(new Date().getTime(),
25      3*24 * 60 * 60, "yyyy年MM月dd日 hh:mm"));
26    
27   } catch (ParseException e) {
28    e.printStackTrace();
29   }
30 
31  }
32 }
33 
34 输出结果
35 
36 1年前
37 刚刚
38 2010年11月30日
39 2010-11-30 10:12
40 12月02日 01:21
41 2010年11月30日 10:12
42 2010-11-30 10:12
原文地址:https://www.cnblogs.com/sharpest/p/7788247.html