package com.example.myapplication.utils; import android.text.TextUtils; import com.orhanobut.logger.Logger; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; public class DateUtils { private static final String UNIT_MONTH = "月前"; private static final String UNIT_DAY = "天前"; private static final String UNIT_HOUR = "小时前"; private static final String UNIT_MINUTE = "分钟前"; public static final String TYPE_CHAR_FORMAT_DAY = "yyyy年MM月dd日"; public static final String TYPE_FULL_COMMON_FORMAT = "yyyy-MM-dd HH:mm:ss"; public static final String TYPE_COMMON_FORMAT_DAY = "yyyy-MM-dd"; public static final String TYPE_FORMAT_MONTH = "yyyy-MM"; public static final String TYPE_MONTHY_DAY = "MM-dd"; public static String formatDate(String oldDateType, String newDateType, String value) { final SimpleDateFormat format = new SimpleDateFormat(oldDateType); Logger.d("values: " + value); Date date = null; try { date = format.parse(value); } catch (ParseException e) { Logger.e("format date error,please check it."); return value; } final SimpleDateFormat newFormat = new SimpleDateFormat(newDateType); return newFormat.format(date); } /** * 传入时间及格式 * * @param format 格式 * @param value 时间 * @return 特定格式时间 */ public static String getShowTime(String format, String value) { SimpleDateFormat sf = new SimpleDateFormat(format); try { Date date = sf.parse(value); long targetTime = date.getTime(); return getShowTime(targetTime); } catch (ParseException e) { return 1 + UNIT_MINUTE; } } /** * 传入时间及格式 * * @param time long类型时间 * @return 特定格式时间 */ public static String getShowTime(long time) { Date nowDate = new Date(); long nowTime = nowDate.getTime(); long differTime = nowTime - time; long minute = 1000 * 60; if (differTime <= 0 || differTime < minute) { return 1 + UNIT_MINUTE; } long hour = 1000 * 60 * 60; if (differTime < hour) { long showMinute = (differTime / minute) % 60; return (showMinute == 0 ? 1 : showMinute) + UNIT_MINUTE; } long day = 24 * hour; if (differTime < day) { long showHour = (differTime / hour) % 24; return (showHour == 0 ? 1 : showHour) + UNIT_HOUR; } long month = 30 * day; if (differTime < month) { long showDay = (differTime / day) % 30; return (showDay == 0 ? 1 : showDay) + UNIT_DAY; } long year = 12 * month; if (differTime < year) { long showMonth = (differTime / month) % 12; return (showMonth == 0 ? 1 : showMonth) + UNIT_MONTH; } return 12 + UNIT_MONTH; } public static long getDateToLong(String format, String date) { SimpleDateFormat sf = new SimpleDateFormat(format); try { Date parse = sf.parse(date); return parse.getTime(); } catch (ParseException e) { Logger.e(e.getLocalizedMessage()); return new Date().getTime(); } } public static String getDataByFormat(String format, long data) { SimpleDateFormat sf = new SimpleDateFormat(format); return sf.format(new Date(data)); } public static int getMonthByDate() { // 获取日期实例 Calendar calendar = Calendar.getInstance(); // 这里要注意,月份是从0开始。 int month = calendar.get(Calendar.MONTH); Logger.d("getMonthByDate : " + (month + 1)); return month + 1; } public static int getYearByDate() { // 获取日期实例 Calendar calendar = Calendar.getInstance(); // 这里要注意,月份是从0开始。 int year = calendar.get(Calendar.YEAR); return year; } public static int getMonthByDate(long date) { // 获取日期实例 Calendar calendar = Calendar.getInstance(); // 将日历设置为指定的时间 calendar.setTimeInMillis(date); // 这里要注意,月份是从0开始。 int month = calendar.get(Calendar.MONTH); Logger.d("getMonthByDate : " + (month + 1)); return month + 1; } public static int getDayByDate() { // 获取日期实例 Calendar calendar = Calendar.getInstance(); // 这里要注意,月份是从0开始。 int day = calendar.get(Calendar.DAY_OF_MONTH); return day; } public static String getPreOrNextMonth(long time, boolean isPre) { Calendar ca = Calendar.getInstance();//得到一个Calendar的实例 ca.setTimeInMillis(time); Date now = ca.getTime(); if (isPre) { ca.add(Calendar.MONTH, -1); //月份减1 } else { ca.add(Calendar.MONTH, +1); //月份减1 } Date lastMonth = ca.getTime(); //结果 SimpleDateFormat sf = new SimpleDateFormat(TYPE_COMMON_FORMAT_DAY); return sf.format(lastMonth); } public static String getPreOrNextMonth(long time, boolean isPre, String formatType) { Calendar ca = Calendar.getInstance();//得到一个Calendar的实例 ca.setTimeInMillis(time); Date now = ca.getTime(); if (isPre) { ca.add(Calendar.MONTH, -1); //月份减1 } else { ca.add(Calendar.MONTH, +1); //月份减1 } Date lastMonth = ca.getTime(); //结果 SimpleDateFormat sf = new SimpleDateFormat(formatType); return sf.format(lastMonth); } public static String getPreOrNextDay(long time, boolean isPre) { Calendar ca = Calendar.getInstance();//得到一个Calendar的实例 ca.setTimeInMillis(time); Date now = ca.getTime(); if (isPre) { ca.add(Calendar.DATE, -1); } else { ca.add(Calendar.DATE, +1); } Date lastMonth = ca.getTime(); //结果 SimpleDateFormat sf = new SimpleDateFormat(TYPE_COMMON_FORMAT_DAY); return sf.format(lastMonth); } public static Date getPreOrNextDayToDate(long time, boolean isPre) { Calendar ca = Calendar.getInstance();//得到一个Calendar的实例 ca.setTimeInMillis(time); Date now = ca.getTime(); if (isPre) { ca.add(Calendar.DATE, -1); } else { ca.add(Calendar.DATE, +1); } Date lastMonth = ca.getTime(); //结果 return lastMonth; } public static String[] WEEK_DAYS = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"}; /** * 获取日期是周几 * * @param dt 日期 * @return 周几的角标 */ public static int getWeekOfDate(Date dt) { Calendar cal = Calendar.getInstance(); cal.setTime(dt); int w = cal.get(Calendar.DAY_OF_WEEK) - 1; if (w < 0) w = 0; return w; } /** * 获取每月多少天 * * @param formatType 日期的格式化 * @param date 日期 * @return 当月总天数 */ public static int getDaysByMonth(String formatType, String date) { Calendar c = Calendar.getInstance(); if (date == null) { return c.getActualMaximum(Calendar.DAY_OF_MONTH); } else { SimpleDateFormat sf = new SimpleDateFormat(formatType); try { Date d = sf.parse(date); c.setTime(d); return c.getActualMaximum(Calendar.DAY_OF_MONTH); } catch (ParseException e) { return 30; } } } public static String convertStringToDateForCST(String currentTimeType, String time) { if (TextUtils.isEmpty(time)) { return null; } try { SimpleDateFormat formatter = new SimpleDateFormat("EEE d MMM yyyy", Locale.ENGLISH); SimpleDateFormat sdt = new SimpleDateFormat(currentTimeType); Date date = sdt.parse(time); return formatter.format(date); } catch (Exception e) { e.printStackTrace(); return null; } } }