|
@@ -0,0 +1,1047 @@
|
|
|
+package com.cooleshow.base.utils;
|
|
|
+
|
|
|
+
|
|
|
+import android.annotation.SuppressLint;
|
|
|
+import android.text.TextUtils;
|
|
|
+
|
|
|
+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;
|
|
|
+import java.util.TimeZone;
|
|
|
+
|
|
|
+@SuppressLint("SimpleDateFormat")
|
|
|
+public class DateUtil {
|
|
|
+
|
|
|
+ private static final String FORMAT = "yyyy-MM-dd HH:mm:ss";
|
|
|
+ private static final SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ private static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
|
|
|
+ private static String mYear;
|
|
|
+ private static String mMonth;
|
|
|
+ private static String mDay;
|
|
|
+ private static String mWay;
|
|
|
+
|
|
|
+ public static Date str2Date(String str) {
|
|
|
+ return str2Date(str, null);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String getDate() {
|
|
|
+ final Calendar mCalendar = Calendar.getInstance();
|
|
|
+ String s = new SimpleDateFormat("yyyy年MM月dd日").format(mCalendar.getTime());
|
|
|
+ String time = new SimpleDateFormat("HH:mm").format(mCalendar.getTime());
|
|
|
+ int apm = mCalendar.get(Calendar.AM_PM);
|
|
|
+ // apm=0 表示上午,apm=1表示下午。
|
|
|
+ return apm == 0 ? (s + " 上午" + time) : (s + " 下午" + time);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getTodayDate() {
|
|
|
+ final Calendar mCalendar = Calendar.getInstance();
|
|
|
+ String s = new SimpleDateFormat("yyyy-MM-dd").format(mCalendar.getTime());
|
|
|
+ return s;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字符串转换到时间格式
|
|
|
+ *
|
|
|
+ * @param dateStr 需要转换的字符串
|
|
|
+ * 需要格式的目标字符串 yyyy-MM-dd
|
|
|
+ * @return String 返回转换后的时间
|
|
|
+ * @throws ParseException 转换异常
|
|
|
+ */
|
|
|
+ public static String dateCurrencyFormat(String dateStr) {
|
|
|
+ if (TextUtils.isEmpty(dateStr)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ DateFormat sdf = new SimpleDateFormat(FORMAT);
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(dateStr);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return dateFormat.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getSevenDayTime() {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + 3);
|
|
|
+ String shur_time = dateFormat.format(calendar.getTime());
|
|
|
+ return shur_time;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字符串转换到时间格式
|
|
|
+ *
|
|
|
+ * @param dateStr 需要转换的字符串
|
|
|
+ * 需要格式的目标字符串 yyyy-MM-dd
|
|
|
+ * @return String 返回转换后的时间
|
|
|
+ * @throws ParseException 转换异常
|
|
|
+ */
|
|
|
+ public static String dateFormatYear(String dateStr) {
|
|
|
+ if (TextUtils.isEmpty(dateStr)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ DateFormat sdf = new SimpleDateFormat(FORMAT);
|
|
|
+ Date date = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ date = sdf.parse(dateStr);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
|
|
|
+ return dateFormat.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String replaceHour(String dateStr, String hour) {
|
|
|
+ if (TextUtils.isEmpty(dateStr)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ DateFormat sdf = new SimpleDateFormat(FORMAT);
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(dateStr);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ return dateFormat.format(date) + " " + hour;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字符串转换到时间格式
|
|
|
+ *
|
|
|
+ * @param dateStr 需要转换的字符串
|
|
|
+ * 需要格式的目标字符串 yyyy/MM/dd
|
|
|
+ * @return String 返回转换后的时间
|
|
|
+ * @throws ParseException 转换异常
|
|
|
+ */
|
|
|
+ public static String dateFormat(String dateStr) {
|
|
|
+ if (TextUtils.isEmpty(dateStr)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ DateFormat sdf = new SimpleDateFormat(FORMAT);
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(dateStr);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
|
|
|
+ return dateFormat.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字符串转换到时间格式
|
|
|
+ * <p>
|
|
|
+ * 需要格式的目标字符串 yyyy/MM/dd
|
|
|
+ *
|
|
|
+ * @return String 返回转换后的时间
|
|
|
+ * @throws ParseException 转换异常
|
|
|
+ */
|
|
|
+ public static String dateFormat(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
|
|
|
+ return dateFormat.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String dateFormatMM_dd(String dateStr) {
|
|
|
+ if (TextUtils.isEmpty(dateStr)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ DateFormat sdf = new SimpleDateFormat(FORMAT);
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(dateStr);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return new SimpleDateFormat("MM月dd日").format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String dateFormatYYMM_dd(String dateStr) {
|
|
|
+ if (TextUtils.isEmpty(dateStr)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ DateFormat sdf = new SimpleDateFormat(FORMAT);
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(dateStr);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return new SimpleDateFormat("yyyy/MM-dd").format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String dateFormatyyMMddDot(String dateStr) {
|
|
|
+ if (TextUtils.isEmpty(dateStr)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ DateFormat sdf = new SimpleDateFormat(FORMAT);
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(dateStr);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return new SimpleDateFormat("yyyy.MM.dd").format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String dateFormatHH_mm(String dateStr) {
|
|
|
+ if (TextUtils.isEmpty(dateStr)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ DateFormat sdf = new SimpleDateFormat(FORMAT);
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(dateStr);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return new SimpleDateFormat("HH:mm").format(date);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String dateFormatmm_ss(int time) {
|
|
|
+ if (time <= 0) {
|
|
|
+ return "00:00";
|
|
|
+ }
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.clear();
|
|
|
+ calendar.add(Calendar.MILLISECOND, time);
|
|
|
+
|
|
|
+ return new SimpleDateFormat("mm:ss").format(calendar.getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String dateFormatNoSecond(String dateStr) {
|
|
|
+ if (TextUtils.isEmpty(dateStr)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ DateFormat sdf = new SimpleDateFormat(FORMAT);
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(dateStr);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return new SimpleDateFormat("yyyy年MM月dd日 HH:mm").format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String dateFormatMMdd(String dateStr) {
|
|
|
+ if (TextUtils.isEmpty(dateStr)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ DateFormat sdf = new SimpleDateFormat(FORMAT);
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(dateStr);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return new SimpleDateFormat("MM-dd").format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字符串获取周
|
|
|
+ * @param dateStr
|
|
|
+ * 需要转换的字符串
|
|
|
+ * @param formatStr
|
|
|
+ * 需要格式的目标字符串 举例 yyyy-MM-dd
|
|
|
+ * @return Date 返回转换后的时间
|
|
|
+ * @throws ParseException
|
|
|
+ * 转换异常
|
|
|
+ */
|
|
|
+ /**
|
|
|
+ * 根据当前日期获得是星期几
|
|
|
+ *
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String dateCurrencyGetWeek(String time) {
|
|
|
+ if (TextUtils.isEmpty(time)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ String Week = "";
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat(FORMAT);
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ try {
|
|
|
+ c.setTime(format.parse(time));
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ if (c.get(Calendar.DAY_OF_WEEK) == 1) {
|
|
|
+ Week += "七";
|
|
|
+ }
|
|
|
+ if (c.get(Calendar.DAY_OF_WEEK) == 2) {
|
|
|
+ Week += "一";
|
|
|
+ }
|
|
|
+ if (c.get(Calendar.DAY_OF_WEEK) == 3) {
|
|
|
+ Week += "二";
|
|
|
+ }
|
|
|
+ if (c.get(Calendar.DAY_OF_WEEK) == 4) {
|
|
|
+ Week += "三";
|
|
|
+ }
|
|
|
+ if (c.get(Calendar.DAY_OF_WEEK) == 5) {
|
|
|
+ Week += "四";
|
|
|
+ }
|
|
|
+ if (c.get(Calendar.DAY_OF_WEEK) == 6) {
|
|
|
+ Week += "五";
|
|
|
+ }
|
|
|
+ if (c.get(Calendar.DAY_OF_WEEK) == 7) {
|
|
|
+ Week += "六";
|
|
|
+ }
|
|
|
+ return Week;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String dateCurrencyGetHH_mm(String dateStr) {
|
|
|
+ if (TextUtils.isEmpty(dateStr)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ DateFormat sdf = new SimpleDateFormat(FORMAT);
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(dateStr);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return timeFormat.format(date);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String dateCurrencyGetMM(String dateStr) {
|
|
|
+ if (TextUtils.isEmpty(dateStr)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ DateFormat sdf = new SimpleDateFormat(FORMAT);
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(dateStr);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ SimpleDateFormat timeFormat = new SimpleDateFormat("MM");
|
|
|
+ return timeFormat.format(date);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 格式化时间
|
|
|
+ * @param dateStr yyyy-MM-dd HH:mm:ss
|
|
|
+ * @return yyyy-MM-dd HH:mm:ss
|
|
|
+ */
|
|
|
+ public static Date getDate(String dateStr) {
|
|
|
+ if (TextUtils.isEmpty(dateStr)) {
|
|
|
+ return new Date();
|
|
|
+ }
|
|
|
+ DateFormat sdf = new SimpleDateFormat(FORMAT);
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(dateStr);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转化时间输入时间与当前时间的间隔
|
|
|
+ *
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String converDataTime(String time) {
|
|
|
+ if (TextUtils.isEmpty(time)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ long currentSeconds = System.currentTimeMillis() / 1000;
|
|
|
+ long timestamp = 0;
|
|
|
+ try {
|
|
|
+ timestamp = datetimeFormat.parse(time).getTime() / 1000;
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ long timeGap = timestamp - currentSeconds;// 与现在时间相差秒数
|
|
|
+ if (timeGap < 0) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ String timeStr = null;
|
|
|
+ long days = timeGap / (60 * 60 * 24);
|
|
|
+ long hours = (timeGap - days * (60 * 60 * 24)) / (60 * 60);
|
|
|
+ long minutes = (timeGap - days * (60 * 60 * 24) - hours * (60 * 60)) / (60);
|
|
|
+ if (days > 0) {
|
|
|
+ timeStr = days + "天" + hours + "小时" + minutes + "分";
|
|
|
+ } else if (hours > 0) {
|
|
|
+ timeStr = hours + "小时" + minutes + "分";
|
|
|
+ } else {
|
|
|
+ timeStr = minutes + "分";
|
|
|
+ }
|
|
|
+
|
|
|
+ return timeStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字符串转时间
|
|
|
+ *
|
|
|
+ * @param str
|
|
|
+ * @param format
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date str2Date(String str, String format) {
|
|
|
+ if (str == null || str.length() == 0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (format == null || format.length() == 0) {
|
|
|
+ format = FORMAT;
|
|
|
+ }
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
+ date = sdf.parse(str);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return date;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Calendar str2Calendar(String str) {
|
|
|
+ return str2Calendar(str, null);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Calendar str2Calendar(String str, String format) {
|
|
|
+ Date date = str2Date(str, format);
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(date);
|
|
|
+
|
|
|
+ return c;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String date2Str(Calendar c) {// yyyy-MM-dd HH:mm:ss
|
|
|
+ return date2Str(c, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String date2Str(Calendar c, String format) {
|
|
|
+ if (c == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return date2Str(c.getTime(), format);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String date2Str(Date d) {// yyyy-MM-dd HH:mm:ss
|
|
|
+ return date2Str(d, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String date2Str(Date d, String format) {// yyyy-MM-dd HH:mm:ss
|
|
|
+ if (d == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (format == null || format.length() == 0) {
|
|
|
+ format = FORMAT;
|
|
|
+ }
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
+ String s = sdf.format(d);
|
|
|
+ return s;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getCurDateStr() {
|
|
|
+
|
|
|
+ return new SimpleDateFormat(FORMAT).format(new Date());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getCurYearStr(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ date = new Date();
|
|
|
+ }
|
|
|
+ return new SimpleDateFormat("yyyy").format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getCurMonthStr(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ date = new Date();
|
|
|
+ }
|
|
|
+ return new SimpleDateFormat("MM").format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getCurDayStr(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ date = new Date();
|
|
|
+ }
|
|
|
+ return new SimpleDateFormat("dd").format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getCurDateyyyy_MMStr(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ date = new Date();
|
|
|
+ }
|
|
|
+ return new SimpleDateFormat("yyyy-MM").format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前日期的字符串格式
|
|
|
+ *
|
|
|
+ * @param format
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getCurDateStr(String format) {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ return date2Str(c, format);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 格式到秒
|
|
|
+ *
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getMillon(long time) {
|
|
|
+
|
|
|
+ return datetimeFormat.format(time);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 格式到天
|
|
|
+ *
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getDay(long time) {
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd").format(time);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 格式到毫秒
|
|
|
+ *
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getSMillon(long time) {
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(time);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字符串转换到时间格式
|
|
|
+ *
|
|
|
+ * @param dateStr 需要转换的字符串
|
|
|
+ * @param formatStr 需要格式的目标字符串 举例 yyyy-MM-dd
|
|
|
+ * @return Date 返回转换后的时间
|
|
|
+ * @throws ParseException 转换异常
|
|
|
+ */
|
|
|
+ public static Date StringToDate(String dateStr, String formatStr) {
|
|
|
+ DateFormat sdf = new SimpleDateFormat(formatStr);
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(dateStr);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转化时间输入时间与当前时间的间隔
|
|
|
+ *
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String converTime(String time) {
|
|
|
+ if (TextUtils.isEmpty(time)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ long currentSeconds = System.currentTimeMillis() / 1000;
|
|
|
+ long timestamp = 0;
|
|
|
+ try {
|
|
|
+ timestamp = datetimeFormat.parse(time).getTime() / 1000;
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ long timeGap = currentSeconds - timestamp;// 与现在时间相差秒数
|
|
|
+ String timeStr = null;
|
|
|
+ if (timeGap > 24 * 60 * 60) {// 1天以上
|
|
|
+ timeStr = timeGap / (24 * 60 * 60) + "天前";
|
|
|
+ } else if (timeGap > 60 * 60) {// 1小时-24小时
|
|
|
+ timeStr = timeGap / (60 * 60) + "小时前";
|
|
|
+ } else if (timeGap > 60) {// 1分钟-59分钟
|
|
|
+ timeStr = timeGap / 60 + "分钟前";
|
|
|
+ } else {// 1秒钟-59秒钟
|
|
|
+ timeStr = "刚刚";
|
|
|
+ }
|
|
|
+ return timeStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把字符串转化为时间格式
|
|
|
+ *
|
|
|
+ * @param timestamp
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getStandardTime(long timestamp) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日 HH:mm");
|
|
|
+ Date date = new Date(timestamp * 1000);
|
|
|
+ sdf.format(date);
|
|
|
+ return sdf.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前日期时间 日期时间格式yyyy-MM-dd HH:mm:ss
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String currentDatetime() {
|
|
|
+ return datetimeFormat.format(now());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 格式化日期时间 日期时间格式yyyy-MM-dd HH:mm:ss
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String formatDatetime(Date date) {
|
|
|
+ return datetimeFormat.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前时间 时间格式HH:mm:ss
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String currentTime() {
|
|
|
+ return timeFormat.format(now());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 格式化时间 时间格式HH:mm:ss
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String formatTime(Date date) {
|
|
|
+ return timeFormat.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前时间的<code>java.util.Date</code>对象
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date now() {
|
|
|
+ return new Date();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Calendar calendar() {
|
|
|
+ Calendar cal = GregorianCalendar.getInstance(Locale.CHINESE);
|
|
|
+ cal.setFirstDayOfWeek(Calendar.MONDAY);
|
|
|
+ return cal;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前时间的毫秒数
|
|
|
+ * 详见{@link System#currentTimeMillis()}
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static long millis() {
|
|
|
+ return System.currentTimeMillis();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前Chinese月份
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int month() {
|
|
|
+ return calendar().get(Calendar.MONTH) + 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得月份中的第几天
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int dayOfMonth() {
|
|
|
+ return calendar().get(Calendar.DAY_OF_MONTH);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 今天是星期的第几天
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int dayOfWeek() {
|
|
|
+ return calendar().get(Calendar.DAY_OF_WEEK);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 今天是年中的第几天
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int dayOfYear() {
|
|
|
+ return calendar().get(Calendar.DAY_OF_YEAR);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断原日期是否在目标日期之前
|
|
|
+ *
|
|
|
+ * @param src
|
|
|
+ * @param dst
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean isBefore(Date src, Date dst) {
|
|
|
+ return src.before(dst);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断原日期是否在目标日期之后
|
|
|
+ *
|
|
|
+ * @param src
|
|
|
+ * @param dst
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean isAfter(Date src, Date dst) {
|
|
|
+ return src.after(dst);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断两日期是否相同
|
|
|
+ *
|
|
|
+ * @param date1
|
|
|
+ * @param date2
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean isEqual(Date date1, Date date2) {
|
|
|
+ return date1.compareTo(date2) == 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断某个日期是否在某个日期范围
|
|
|
+ *
|
|
|
+ * @param beginDate 日期范围开始
|
|
|
+ * @param endDate 日期范围结束
|
|
|
+ * @param src 需要判断的日期
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean between(Date beginDate, Date endDate, Date src) {
|
|
|
+ return beginDate.before(src) && endDate.after(src);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前月的最后一天
|
|
|
+ * HH:mm:ss为0,毫秒为999
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date lastDayOfMonth() {
|
|
|
+ Calendar cal = calendar();
|
|
|
+ cal.set(Calendar.DAY_OF_MONTH, 0); // M月置零
|
|
|
+ cal.set(Calendar.HOUR_OF_DAY, 0);// H置零
|
|
|
+ cal.set(Calendar.MINUTE, 0);// m置零
|
|
|
+ cal.set(Calendar.SECOND, 0);// s置零
|
|
|
+ cal.set(Calendar.MILLISECOND, 0);// S置零
|
|
|
+ cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);// 月份+1
|
|
|
+ cal.set(Calendar.MILLISECOND, -1);// 毫秒-1
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得当前月的第一天
|
|
|
+ * HH:mm:ss SS为零
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date firstDayOfMonth() {
|
|
|
+ Calendar cal = calendar();
|
|
|
+ cal.set(Calendar.DAY_OF_MONTH, 1); // M月置1
|
|
|
+ cal.set(Calendar.HOUR_OF_DAY, 0);// H置零
|
|
|
+ cal.set(Calendar.MINUTE, 0);// m置零
|
|
|
+ cal.set(Calendar.SECOND, 0);// s置零
|
|
|
+ cal.set(Calendar.MILLISECOND, 0);// S置零
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Date weekDay(int week) {
|
|
|
+ Calendar cal = calendar();
|
|
|
+ cal.set(Calendar.DAY_OF_WEEK, week);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得周五日期
|
|
|
+ * 注:日历工厂方法{@link #calendar()}设置类每个星期的第一天为Monday,US等每星期第一天为sunday
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date friday() {
|
|
|
+ return weekDay(Calendar.FRIDAY);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得周六日期
|
|
|
+ * 注:日历工厂方法{@link #calendar()}设置类每个星期的第一天为Monday,US等每星期第一天为sunday
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date saturday() {
|
|
|
+ return weekDay(Calendar.SATURDAY);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得周日日期 注:日历工厂方法{@link #calendar()}设置类每个星期的第一天为Monday,US等每星期第一天为sunday
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date sunday() {
|
|
|
+ return weekDay(Calendar.SUNDAY);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将字符串日期时间转换成java.util.Date类型 日期时间格式yyyy-MM-dd HH:mm:ss
|
|
|
+ *
|
|
|
+ * @param datetime
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date parseDatetime(String datetime) throws ParseException {
|
|
|
+ return datetimeFormat.parse(datetime);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将字符串日期转换成java.util.Date类型 日期时间格式yyyy-MM-dd
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ * @throws ParseException
|
|
|
+ */
|
|
|
+ public static Date parseDate(String date) throws ParseException {
|
|
|
+ return dateFormat.parse(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将字符串日期转换成java.util.Date类型 时间格式 HH:mm:ss
|
|
|
+ *
|
|
|
+ * @param time
|
|
|
+ * @return
|
|
|
+ * @throws ParseException
|
|
|
+ */
|
|
|
+ public static Date parseTime(String time) throws ParseException {
|
|
|
+ return timeFormat.parse(time);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据自定义pattern将字符串日期转换成java.util.Date类型
|
|
|
+ *
|
|
|
+ * @param datetime
|
|
|
+ * @param pattern
|
|
|
+ * @return
|
|
|
+ * @throws ParseException
|
|
|
+ */
|
|
|
+ public static Date parseDatetime(String datetime, String pattern) throws ParseException {
|
|
|
+ SimpleDateFormat format = (SimpleDateFormat) datetimeFormat.clone();
|
|
|
+ format.applyPattern(pattern);
|
|
|
+ return format.parse(datetime);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把秒格式化为分种小时
|
|
|
+ *
|
|
|
+ * @param second
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String parseSecond(int second) {
|
|
|
+ if (second >= 60) {
|
|
|
+ return second / 60 + "分";
|
|
|
+ } else if (second >= 60 * 60) {
|
|
|
+ return second / 60 * 60 + "时";
|
|
|
+ } else if (second >= 60 * 60 * 24) {
|
|
|
+ return second / 60 * 60 + "天";
|
|
|
+ } else {
|
|
|
+ return second + "秒";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 比较时间大小
|
|
|
+ *
|
|
|
+ * @param begin
|
|
|
+ * @param end
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int compareDate(String begin, String end) {
|
|
|
+ DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
|
|
|
+ try {
|
|
|
+ Date beginDate = df.parse(begin);
|
|
|
+ Date endDate = df.parse(end);
|
|
|
+ if (beginDate.getTime() < endDate.getTime()) {
|
|
|
+ return 1;
|
|
|
+ } else if (beginDate.getTime() > endDate.getTime()) {
|
|
|
+ return -1;
|
|
|
+ } else {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ } catch (Exception exception) {
|
|
|
+ exception.printStackTrace();
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得年份
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int getYear(Date date) {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(date);
|
|
|
+ return c.get(Calendar.YEAR);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得月份
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int getMonth(Date date) {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(date);
|
|
|
+ return c.get(Calendar.MONTH) + 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得星期几
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int getWeek() {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
|
|
|
+ return c.get(Calendar.DAY_OF_WEEK) - 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得日期
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int getDay(Date date) {
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(date);
|
|
|
+ return c.get(Calendar.DATE);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得天数差
|
|
|
+ *
|
|
|
+ * @param begin
|
|
|
+ * @param end
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static double getDayDiff(Date begin, Date end) {
|
|
|
+ double day = 0;
|
|
|
+ if (end.getTime() < begin.getTime()) {
|
|
|
+ day = -1;
|
|
|
+ } else if (end.getTime() == begin.getTime()) {
|
|
|
+ day = 0;
|
|
|
+ } else {
|
|
|
+ day += (end.getTime() - begin.getTime()) / (24 * 60 * 60 * 1000d);
|
|
|
+ }
|
|
|
+ return day;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得天数差
|
|
|
+ *
|
|
|
+ * @param begin
|
|
|
+ * @param end
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int getDayDiffInt(Date begin, Date end) {
|
|
|
+ int day = 1;
|
|
|
+ if (end.getTime() < begin.getTime()) {
|
|
|
+ day = -1;
|
|
|
+ } else if (end.getTime() == begin.getTime()) {
|
|
|
+ day = 0;
|
|
|
+ } else {
|
|
|
+ day += (end.getTime() - begin.getTime()) / (24 * 60 * 60 * 1000d);
|
|
|
+ }
|
|
|
+ return day;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得分钟差
|
|
|
+ * 如果结束时间还没到 返回时间差
|
|
|
+ * @param begin 当前时间
|
|
|
+ * @param end 课程结束时间
|
|
|
+ * @return -2 课程已经结束 0 时间一致 课程还没结束 返回时间差
|
|
|
+ */
|
|
|
+ public static long getMinuteDiff(Date begin, Date end) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
|
|
+ long beginTime = getDate(sdf.format(begin) + ":00").getTime();
|
|
|
+ long endTime = getDate(sdf.format(end) + ":00").getTime();
|
|
|
+ long day = 0;
|
|
|
+ if (endTime < beginTime) {
|
|
|
+ day = -2;
|
|
|
+ } else if (endTime == beginTime) {
|
|
|
+ day = 0;
|
|
|
+ } else {
|
|
|
+ long time = endTime - beginTime;
|
|
|
+ day += time / (60 * 1000);
|
|
|
+ }
|
|
|
+ return day;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断给定字符串时间是否为今日
|
|
|
+ *
|
|
|
+ * @param sdate
|
|
|
+ * @return boolean
|
|
|
+ */
|
|
|
+ public static boolean isToday(String sdate) {
|
|
|
+ boolean b = false;
|
|
|
+ Date time = getDate(sdate);
|
|
|
+ Date today = new Date();
|
|
|
+ if (time != null) {
|
|
|
+ String nowDate = dateFormat.format(today);
|
|
|
+ String timeDate = dateFormat.format(time);
|
|
|
+ if (nowDate.equals(timeDate)) {
|
|
|
+ b = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return b;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static long forMatLong(String time) {
|
|
|
+ try {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
|
|
|
+ sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
|
|
|
+ Date date = sdf.parse(time);
|
|
|
+ return date.getTime();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|