package com.moral.andbrickslib.utils; 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.Locale; public class TimeUtil { /** * 描述:获取表示当前日期时间的字符串. * * @param format 格式化字符串,如"yyyy-MM-dd HH:mm:ss" * @return String String类型的当前日期 */ public static String getCurrentDate(String format) { String curDateTime = null; try { SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format); Calendar c = new GregorianCalendar(); curDateTime = mSimpleDateFormat.format(c.getTime()); } catch (Exception e) { e.printStackTrace(); } return curDateTime; } public static String getDatebyLong(long date,String format){ String curDateTime = null; try { SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format); Date d = new Date(date); curDateTime = mSimpleDateFormat.format(d); } catch (Exception e) { e.printStackTrace(); } return curDateTime; } /** * 获取当前时间后n天的日期 * * @param format * @param day * @return */ public static String getAfterDate(String format, int day) { String curDateTime = null; try { SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format); Calendar c = Calendar.getInstance(); c.add(Calendar.DATE, day); curDateTime = mSimpleDateFormat.format(c.getTime()); } catch (Exception e) { e.printStackTrace(); } return curDateTime; } public static String getAfterDate(String format,String time, int day) { String curDateTime = null; try { SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format); Calendar c = Calendar.getInstance(); c.setTime(mSimpleDateFormat.parse(time)); c.add(Calendar.DATE, day); curDateTime = mSimpleDateFormat.format(c.getTime()); } catch (Exception e) { e.printStackTrace(); } return curDateTime; } /** * 获取日期间隔 * @param time1 * @param time2 * @return */ public static long getDateInterval(String time1,String time2){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date a1 = null; Date b1 = null; try { a1 = format.parse(time1); b1 = format.parse(time2); } catch (Exception e) { e.printStackTrace(); } long day = (a1.getTime()-b1.getTime())/(24*60*60*1000); return day; } /** * 时间字符串比较 等于 1表示 s1大于等于s2 * * @param s1 * @param s2 * @return */ public static int DateForStringCompare(String format, String s1, String s2) { // 设定时间的模板 SimpleDateFormat formatter = new SimpleDateFormat(format); // 得到指定模范的时间 Date d1 = null; Date d2 = null; try { d1 = formatter.parse(s1); d2 = formatter.parse(s2); } catch (Exception e) { return -1; } // 比较 if ((d1.getTime() - d2.getTime()) >= 0) { return 1; } else { return -1; } } public static Date date = null; public static DateFormat dateFormat = null; public static Calendar calendar = null; /** * 功能描述:格式化日期 * @param dateStr * String 字符型日期 * @param format * String 格式 * @return Date 日期 */ public static Date parseDate(String dateStr, String format) { try { dateFormat = new SimpleDateFormat(format); String dt = dateStr.replaceAll("-", "/"); if ((!dt.equals("")) && (dt.length() < format.length())) { dt += format.substring(dt.length()).replaceAll("[YyMmDdHhSs]", "0"); } date = (Date) dateFormat.parse(dt); } catch (Exception e) { } return date; } /** * 功能描述:格式化输出日期 * * @param date * Date 日期 * @param format * String 格式 * @return 返回字符型日期 */ public static String formatDate2String(Date date, String format) { String result = ""; try { if (date != null) { dateFormat = new SimpleDateFormat(format); result = dateFormat.format(date); } } catch (Exception e) { } return result; } /** * 功能描述:返回年份 * * @param date * Date 日期 * @return 返回年份 */ public static int getYear(Date date) { calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.YEAR); } /** * 功能描述:返回月份 * * @param date * Date 日期 * @return 返回月份 */ public static int getMonth(Date date) { calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.MONTH) + 1; } /** * 功能描述:返回日份 * * @param date * Date 日期 * @return 返回日份 */ public static int getDay(Date date) { calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.DAY_OF_MONTH); } /** * 功能描述:返回小时 * * @param date * 日期 * @return 返回小时 */ public static int getHour(Date date) { calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.HOUR_OF_DAY); } /** * 功能描述:返回分钟 * * @param date * 日期 * @return 返回分钟 */ public static int getMinute(Date date) { calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.MINUTE); } /** * 返回秒钟 * * @param date * Date 日期 * @return 返回秒钟 */ public static int getSecond(Date date) { calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.SECOND); } /** * 功能描述:返回毫秒 * * @param date * 日期 * @return 返回毫秒 */ public static long getMillis(Date date) { calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.getTimeInMillis(); } /** * 功能描述:日期相加 * * @param date * Date 日期 * @param day * int 天数 * @return 返回相加后的日期 */ public static Date addDate(Date date, int day) { calendar = Calendar.getInstance(); long millis = getMillis(date) + ((long) day) * 24 * 3600 * 1000; calendar.setTimeInMillis(millis); return calendar.getTime(); } /** * 功能描述:日期相减 * * @param date * Date 日期 * @param date1 * Date 日期 * @return 返回相减后的日期 */ public static int diffDate(Date date, Date date1) { return (int) ((getMillis(date) - getMillis(date1)) / (24 * 3600 * 1000)); } /** * 功能描述:取得指定月份的第一天 * * @param strdate * String 字符型日期 * @return String yyyy-MM-dd 格式 */ public static String getMonthBegin(String strdate,String format) { date = parseDate(strdate,format); return formatDate2String(date, "yyyy-MM") + "-01"; } /** * 功能描述:取得指定月份的最后一天 * * @param strdate * String 字符型日期 * @return String 日期字符串 yyyy-MM-dd格式 */ public static String getMonthEnd(String strdate,String format) { date = parseDate(getMonthBegin(strdate,format),format); calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MONTH, 1); calendar.add(Calendar.DAY_OF_YEAR, -1); return formatDate2String(calendar.getTime(),format); } /** * 功能描述:以指定的格式来格式化日期 * * @param date * Date 日期 * @param format * String 格式 * @return String 日期字符串 */ public static String formatDateByFormat(Date date, String format) { String result = ""; if (date != null) { try { SimpleDateFormat sdf = new SimpleDateFormat(format); result = sdf.format(date); } catch (Exception ex) { ex.printStackTrace(); } } return result; } /** * 获取某天是星期几 * @param date * @return */ public static String getMonthDayWeek(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); int year = c.get(Calendar.YEAR); //获取年 int month = c.get(Calendar.MONTH) + 1; //获取月份,0表示1月份 int day = c.get(Calendar.DAY_OF_MONTH); //获取当前天数 int week = c.get(Calendar.DAY_OF_WEEK); String weekStr = null; switch (week) { case Calendar.SUNDAY: weekStr = "周日"; break; case Calendar.MONDAY: weekStr = "周一"; break; case Calendar.TUESDAY: weekStr = "周二"; break; case Calendar.WEDNESDAY: weekStr = "周三"; break; case Calendar.THURSDAY: weekStr = "周四"; break; case Calendar.FRIDAY: weekStr = "周五"; break; case Calendar.SATURDAY: weekStr = "周六"; break; } return month + "月" + day + "日" + "(" + weekStr + ")"; } /** * 日期字符串转换为日期 * * @param date 日期字符串 * @param pattern 格式 * @return 日期 */ public static Date formatStringByFormat(String date, String pattern) { SimpleDateFormat sdf = new SimpleDateFormat(pattern); try { return sdf.parse(date); } catch (ParseException e) { e.printStackTrace(); return null; } } /** * 获得口头时间字符串,如今天,昨天等 * * @param d 时间格式为yyyy-MM-dd HH:mm:ss * @return 口头时间字符串 */ public static String getTimeFriendly(String d) { Date date = formatStringByFormat(d, "yyyy-MM-dd HH:mm:ss"); Calendar now = Calendar.getInstance(); now.setTime(new Date()); int nowYear = now.get(Calendar.YEAR); int nowMonth = now.get(Calendar.MONTH); int nowWeek = now.get(Calendar.WEEK_OF_MONTH); int nowDay = now.get(Calendar.DAY_OF_WEEK); int nowHour = now.get(Calendar.HOUR_OF_DAY); int nowMinute = now.get(Calendar.MINUTE); Calendar ca = Calendar.getInstance(); if(date!=null) ca.setTime(date); else ca.setTime(new Date()); int year = ca.get(Calendar.YEAR); int month = ca.get(Calendar.MONTH); int week = ca.get(Calendar.WEEK_OF_MONTH); int day = ca.get(Calendar.DAY_OF_WEEK); int hour = ca.get(Calendar.HOUR_OF_DAY); int minute = ca.get(Calendar.MINUTE); if (year != nowYear) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //不同年份 return sdf.format(date); } else { if (month != nowMonth) { //不同月份 SimpleDateFormat sdf = new SimpleDateFormat("M月dd日"); return sdf.format(date); } else { if (week != nowWeek) { //不同周 SimpleDateFormat sdf = new SimpleDateFormat("M月dd日"); return sdf.format(date); } else if (day != nowDay) { if (day + 1 == nowDay) { return "昨天" + formatDateByFormat(date, "HH:mm"); } if (day + 2 == nowDay) { return "前天" + formatDateByFormat(date, "HH:mm"); } //不同天 SimpleDateFormat sdf = new SimpleDateFormat("M月dd日"); return sdf.format(date); } else { //同一天 int hourGap = nowHour - hour; if (hourGap == 0)//1小时内 { if (nowMinute - minute < 1) { return "刚刚"; } else { return (nowMinute - minute) + "分钟前"; } } else if (hourGap >= 1 && hourGap <= 12) { return hourGap + "小时前"; } else { SimpleDateFormat sdf = new SimpleDateFormat("今天 HH:mm"); return sdf.format(date); } } } } } /** * 日期字符串转换为日期 * * @param date 日期字符串 * @param pattern 格式 * @return 日期 */ public static String reformatTime(String date, String pattern) { String fmt = "yyyy-MM-dd HH:mm:ss"; SimpleDateFormat simple = new SimpleDateFormat(pattern); Date old = parseToDate(date, fmt); return simple.format(old); } /** * 字符串转换成日期. * @param dateString 日期字符 * @param pattern 格式化. * @return */ public static Date parseToDate(String dateString,String pattern){ if(pattern==null || "".equals(pattern)){ pattern="yyyy-MM-dd HH:mm:ss"; } SimpleDateFormat formatter = new SimpleDateFormat(pattern, Locale.getDefault()); try { return formatter.parse(dateString); } catch (ParseException e) { } return new Date(); } /** * 解析日期 * @param str * @param formFormat * @param toFormat * @return */ public static String getDateToString(String str,String formFormat,String toFormat) { SimpleDateFormat sFormatter = new SimpleDateFormat(formFormat); Date date = null; try { date = sFormatter.parse(str); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } SimpleDateFormat pFormatter = new SimpleDateFormat(toFormat); String time = pFormatter.format(date); return time; } }