1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
| import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date;
import org.apache.commons.lang.time.DateFormatUtils;
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" ,"yyyyMMdd"};
public static String getDate() { return getDate("yyyy-MM-dd"); }
public static String getDate(String pattern) { return DateFormatUtils.format(new Date(), pattern); }
public static String formatDate(Date date, Object... pattern) { String formatDate = null; if (pattern != null && pattern.length > 0) { formatDate = DateFormatUtils.format(date, pattern[0].toString()); } else { formatDate = DateFormatUtils.format(date, "yyyy-MM-dd"); } return formatDate; }
public static String formatDateTime(Date date) { return formatDate(date, "yyyy-MM-dd HH:mm:ss"); }
public static String getTime() { return formatDate(new Date(), "HH:mm:ss"); }
public static String getDateTime() { return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"); }
public static String getYear() { return formatDate(new Date(), "yyyy"); }
public static String getMonth() { return formatDate(new Date(), "MM"); }
public static String getDay() { return formatDate(new Date(), "dd"); }
public static String getWeek() { return formatDate(new Date(), "E"); }
public static Date parseDate(Object str) { if (str == null) { return null; } try { return parseDate(str.toString(), parsePatterns); } catch (ParseException e) { return null; } }
public static long pastDays(Date date) { long t = new Date().getTime() - date.getTime(); return t / (24 * 60 * 60 * 1000); }
public static long pastHour(Date date) { long t = new Date().getTime()-date.getTime(); return t/(60*60*1000); }
public static long pastMinutes(Date date) { long t = new Date().getTime()-date.getTime(); return t/(60*1000); }
public static String formatDateTime(long timeMillis){ long day = timeMillis/(24*60*60*1000); long hour = (timeMillis/(60*60*1000)-day*24); long min = ((timeMillis/(60*1000))-day*24*60-hour*60); long s = (timeMillis/1000-day*24*60*60-hour*60*60-min*60); long sss = (timeMillis-day*24*60*60*1000-hour*60*60*1000-min*60*1000-s*1000); return (day>0?day+",":"")+hour+":"+min+":"+s+"."+sss; }
public static Date getDateStart(Date date) { if (date == null) { return null; } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { date = sdf.parse(formatDate(date, "yyyy-MM-dd") + " 00:00:00"); } catch (ParseException e) { e.printStackTrace(); } return date; }
public static Date getDateEnd(Date date) { if (date == null) { return null; } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { date = sdf.parse(formatDate(date, "yyyy-MM-dd") + " 23:59:59"); } catch (ParseException e) { e.printStackTrace(); } return date; }
public static int compareDate(Object preDateStr, Object nextDateStr) { int result = 0; Date preDate = parseDate(preDateStr); Date nextDate = parseDate(nextDateStr); try { result = preDate.compareTo(nextDate); } catch (Exception e) { result = 0; e.printStackTrace(); } return result; }
public static String getPastDayStr(Object dateObj, int days) { Date date = parseDate(dateObj); long time = date.getTime() + days * (long)(24 * 60 * 60 * 1000); return formatDate(new Date(time)); }
public static long getSubactDate(Object preDateStr, Object nextDateStr) { Date preDate = parseDate(preDateStr); Date nextDate = parseDate(nextDateStr); long result = (preDate.getTime() - nextDate.getTime()) / 1000L; return result; }
public static long getDifferDate(Object preDateStr, Object nextDateStr) { return getSubactDate(preDateStr, nextDateStr) / (60 * 60 * 24L); }
public static String getNewUpdateDateString(String updateDate, String updateTime) { String result = updateDate; long time = 0; if (updateDate.equals(DateUtils.getDate())) { time = DateUtils.getSubactDate(DateUtils.getDateTime(), updateDate + " " + updateTime); if (time >= 3600) { result = time / 3600 + "小时前"; } else if (time >= 60) { result = time / 60 + "分钟前"; } else if (time >= 1) { result = time + "秒前"; } else { result = "刚刚"; } } else if (result.length() >= 10) { result = result.substring(5); } return result; } }
|