package com.future.driver.utils; import android.annotation.SuppressLint; import android.text.TextUtils; import androidx.annotation.StringDef; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * Created by Administrator on 2017/3/24. */ @SuppressLint("SimpleDateFormat") public class DateUtil { public static String getTime(@TYPE String type, long time) { return getTime(type, time, false); } public static String splitDate(String date) { if (TextUtils.isEmpty(date)) return date; String[] split = date.split(" "); if (split.length > 1) return split[0]; return date; } /** * 30分钟以内的显示【刚刚】; * 30分钟至1个小时的显示【30分钟】; * 1个小时到2个小时的为【1小时】; * 2个小时以上且是今天的,展示为【今天】; * 其余的展示发货日期。 * * @param type * @param time * @param isCustom 自定义 返回 * @return */ public static String getTime(@TYPE String type, long time, boolean isCustom) { SimpleDateFormat format; long differTime = (System.currentTimeMillis() - time) / (60 * 1000); long currentTime = System.currentTimeMillis(); Calendar calendar1 = Calendar.getInstance(); calendar1.setTimeInMillis(currentTime); long productTime = Long.valueOf(time); Calendar calendar2 = Calendar.getInstance(); calendar2.setTimeInMillis(productTime); if (isCustom) { if (differTime < 60 && differTime >= 0) { return differTime+"分钟前"; //"1小时内" } else if (differTime < 24 * 60 && calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH)) { return differTime/60+"小时前";//今天 } else if (differTime < 24 * 60 && calendar1.get(Calendar.DAY_OF_MONTH) != calendar2.get(Calendar.DAY_OF_MONTH)) { return "昨天"; } else if (differTime >= 24 * 60 && differTime < 60 * 24 * 2) { return "前天";//今天 }else { format = new SimpleDateFormat(type); return format.format(new Date(time)); } } else { format = new SimpleDateFormat(type); return format.format(new Date(time)); } } /*** * 返回秒 * @param time * @return */ public static String getDifferTime(long time) { long differTime = time / (1000); long f = differTime / 60; long m = differTime % 60; if (f == 0) { return m + ""; } else { return f + ":" + m; } } /* * 将时间转换为时间戳 */ public static long dateToStamp(@TYPE String type, String s) throws ParseException { if (TextUtils.isEmpty(s)) return new Date().getTime(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(type); Date date = simpleDateFormat.parse(s); return date.getTime(); } public static final String TYPE0 = "yyyy-MM-dd HH:mm:ss"; public static final String TYPE1 = "yyyy-MM-dd"; public static final String TYPE2 = "yyyy年MM月dd日 HH:mm:ss "; public static final String TYPE3 = "yyyy/MM/dd"; public static final String TYPE4 = "yyyy-MM-dd HH:mm"; public static final String TYPE5 = "HH:mm"; public static final String TYPE6 = "dd号 HH:mm"; public static final String TYPE7 = "MM月dd日"; public static final String TYPE8 = "yyyy/MM/dd HH:mm:ss "; public static final String TYPE9 = "MM月dd日 HH:mm"; @StringDef({ TYPE0, TYPE1, TYPE2, TYPE3, TYPE4, TYPE5, TYPE6, TYPE7, TYPE8, TYPE9 }) @Retention(RetentionPolicy.SOURCE) @interface TYPE { } public static String getAstro(int Calendar_month, int Calendar_day) { String[] astro = new String[]{"摩羯座", "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座"}; int[] arr = new int[]{20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22};// 两个星座分割日 int index = Calendar_month; // 所查询日期在分割日之前,索引-1,否则不变 if (Calendar_day < arr[Calendar_month - 1]) { index = index - 1; } return astro[index]; } public static String getWeek(long time){ String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; Calendar calendar=Calendar.getInstance(); calendar.setTimeInMillis(time); return weekDays[calendar.get(Calendar.DAY_OF_WEEK)-1]; } }