时间类型处理
import org.apache.commons.lang3.StringUtils; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public final class DateUtils { /** * 日期转换成字符串 * @param date * @param formatStr * @return */ public static String DateToString(Date date, String formatStr){ DateFormat df = new SimpleDateFormat(formatStr); return df.format(date); } /** * 日期转换成yyyy-MM-dd HH:mm:ss字符串 * @param date * @return */ public static String getStringDate(Date date){ DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return df.format(date); } /** * 字符串转换成日期 * @param date * @param formatStr * @return */ public static Date StringToDate(String date, String formatStr) { DateFormat df = new SimpleDateFormat(formatStr); try { return df.parse(date); } catch (ParseException e) { //日期格式转换错误 e.printStackTrace(); return null; } } /** * 时间转换为时/分/秒 * @param longTime * @return */ public static String TimeToString(long longTime){ StringBuilder time = new StringBuilder(); long hours = longTime / 3600; long minutes = (longTime % 3600) / 60; long seconds = longTime % 60; if (hours > 0) time.append(hours).append("小时"); if (minutes > 0) time.append(minutes).append("分"); if (seconds > 0) time.append(seconds).append("秒"); if (hours == 0 && minutes == 0 && seconds == 0) time.append("0秒"); return time.toString(); } /** * 字符串日期加减 * @param dateStr 操作日期 * @param count 加的数量,负数表示减去 * @param field 单位:Calendar.DATE, Calendar.MONTH, Calendar.YEAR * @return 相加后的日期字符串 */ public static String DateAdd(String dateStr, int count, int field){ Calendar cd = Calendar.getInstance(); if(StringUtils.isNotBlank(dateStr)){ cd.setTime(StringToDate(dateStr, "yyyy-MM-dd")); cd.add(field, count); dateStr = DateToString(cd.getTime(), "yyyy-MM-dd"); } return dateStr; } /** * 日期相加 * @param date 操作日期 * @param count 加的数量,负数表示减去 * @param field 单位:Calendar.DATE, Calendar.MONTH, Calendar.YEAR * @return 相加后的日期 */ public static Date DateAdd(Date date, int count, int field){ Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(field, count); return calendar.getTime(); } /** * 日期相减获取时间差(毫秒数) * @param startTime * @param endTime * @param formatStr * @return */ public static long DateDiff(String startTime,String endTime,String formatStr){ long timeDiff = 0; if(StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)){ long start_time = StringToDate(startTime,formatStr).getTime(); long end_time = StringToDate(endTime,formatStr).getTime(); timeDiff = Math.abs(start_time - end_time); } return timeDiff; } /** * 获取今天之前的days天日期列表 * @param days * @return */ public static List<String> getDateList(int days){ Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); List dateList = new ArrayList(); for(int i = 0;i<days;i++){ calendar.setTime(new Date()); calendar.add(Calendar.DAY_OF_MONTH, 0-(i+1)); String dateStr = DateToString(calendar.getTime(),"yyyy-MM-dd"); dateList.add(dateStr); } Collections.reverse(dateList); return dateList; } /** * 获取今天之前的days天日期列表 * @param days * @return */ public static String[] getDateArray(int days){ Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); String[] dateArray = new String[days]; for(int i=days;i>0;i--){ calendar.setTime(new Date()); calendar.add(Calendar.DAY_OF_MONTH, 0-i); String dateStr = DateToString(calendar.getTime(),"yyyy-MM-dd"); dateArray[days-i] = dateStr; } return dateArray; } /** * 字符串日期人性化显示 * String formatStr * @param date * @param formatStr * @return */ public static String friendlyTime(String date, String formatStr){ Date nowdate = StringToDate(DateToString(new Date(),formatStr),formatStr); Date ondate = StringToDate(date,formatStr); Long diffTime = (nowdate.getTime() - ondate.getTime()) / (86400000); String dateStr = ""; switch (diffTime.intValue()){ case 0: dateStr = "今天"; break; case 1: dateStr = "昨天"; break; case 2: dateStr = "两天前"; break; case 3: dateStr = "三天前"; break; case 4: dateStr = "四天前"; break; case 5: dateStr = "五天前"; break; case 6: dateStr = "六天前"; break; case 7: dateStr = "一周前"; break; default: DateFormat df = new SimpleDateFormat(formatStr); dateStr = df.format(date); } return dateStr; } /** * 获取日期所在的周 * @param dateStr * @param formatStr * @return */ public static String weekOfMonth(String dateStr, String formatStr){ Date date = StringToDate(dateStr,formatStr); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH); return String.valueOf(weekOfMonth); } }