|
@@ -1,72 +1,17 @@
|
|
|
package com.sheep.gamegroup.dateview;
|
|
package com.sheep.gamegroup.dateview;
|
|
|
|
|
|
|
|
-import android.util.Log;
|
|
|
|
|
-
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
import java.util.Calendar;
|
|
import java.util.Calendar;
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
-
|
|
|
|
|
|
|
+import java.util.Locale;
|
|
|
/**
|
|
/**
|
|
|
- * Created by lmt on 16/7/6.
|
|
|
|
|
|
|
+ * Created by realicing on 2018/04/.
|
|
|
|
|
+ * realicing@sina.com
|
|
|
*/
|
|
*/
|
|
|
public class DateUtil {
|
|
public class DateUtil {
|
|
|
|
|
|
|
|
- public static final String ymdhms = "yyyy-MM-dd HH:mm:ss";
|
|
|
|
|
- public static final String ymd = "yyyy-MM-dd";
|
|
|
|
|
-
|
|
|
|
|
- public static String monthNumToMonthName(String month) {
|
|
|
|
|
- String m = month;
|
|
|
|
|
- if ("1".equals(month)) {
|
|
|
|
|
- m = "一月份";
|
|
|
|
|
- } else if ("2".equals(month)) {
|
|
|
|
|
- m = "二月份";
|
|
|
|
|
- } else if ("3".equals(month)) {
|
|
|
|
|
- m = "三月份";
|
|
|
|
|
- } else if ("4".equals(month)) {
|
|
|
|
|
- m = "四月份";
|
|
|
|
|
- } else if ("5".equals(month)) {
|
|
|
|
|
- m = "五月份";
|
|
|
|
|
- } else if ("6".equals(month)) {
|
|
|
|
|
- m = "六月份";
|
|
|
|
|
- } else if ("7".equals(month)) {
|
|
|
|
|
- m = "七月份";
|
|
|
|
|
- } else if ("8".equals(month)) {
|
|
|
|
|
- m = "八月份";
|
|
|
|
|
- } else if ("9".equals(month)) {
|
|
|
|
|
- m = "九月份";
|
|
|
|
|
- } else if ("10".equals(month)) {
|
|
|
|
|
- m = "十月份";
|
|
|
|
|
- } else if ("11".equals(month)) {
|
|
|
|
|
- m = "十一月份";
|
|
|
|
|
- } else if ("12".equals(month)) {
|
|
|
|
|
- m = "十二月份";
|
|
|
|
|
- }
|
|
|
|
|
- return m;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static String getTomorrow() {
|
|
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
|
|
- calendar.add(Calendar.DAY_OF_MONTH, 1);
|
|
|
|
|
- int year = calendar.get(Calendar.YEAR);
|
|
|
|
|
- int month = calendar.get(Calendar.MONTH) + 1;
|
|
|
|
|
- int day = calendar.get(Calendar.DAY_OF_MONTH);
|
|
|
|
|
- return year + "-" + (month > 9 ? month : ("0" + month)) + "-" + day;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static int getYear() {
|
|
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
|
|
- return calendar.get(Calendar.YEAR);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public static String getToday() {
|
|
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
|
|
- int year = calendar.get(Calendar.YEAR);
|
|
|
|
|
- int month = calendar.get(Calendar.MONTH) + 1;
|
|
|
|
|
- int day = calendar.get(Calendar.DAY_OF_MONTH);
|
|
|
|
|
- return year + "-" + (month > 9 ? month : ("0" + month)) + "-" + day;
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
public static List<Integer> getDateForString(String date) {
|
|
public static List<Integer> getDateForString(String date) {
|
|
|
String[] dates = date.split("-");
|
|
String[] dates = date.split("-");
|
|
@@ -77,126 +22,112 @@ public class DateUtil {
|
|
|
return list;
|
|
return list;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static Calendar getCalendar(Date date) {
|
|
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
|
|
- calendar.setTime(date);
|
|
|
|
|
- return calendar;
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- public static String formatDate(String date, String format) {
|
|
|
|
|
- String resultD = date;
|
|
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
|
|
- try {
|
|
|
|
|
- Date d = sdf.parse(date);
|
|
|
|
|
- resultD = sdf.format(d);
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- }
|
|
|
|
|
- return resultD;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 时间戳格式转换
|
|
|
|
|
+ */
|
|
|
|
|
+ private static String dayNames[] = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
|
|
|
|
|
|
|
|
- public static String formatDate(long milliseconds, String format) {
|
|
|
|
|
- String resultD = "";
|
|
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
|
|
- try {
|
|
|
|
|
- Date d = new Date(milliseconds);
|
|
|
|
|
- resultD = sdf.format(d);
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- }
|
|
|
|
|
- return resultD;
|
|
|
|
|
|
|
+ public static String getTimeText(long millis) {
|
|
|
|
|
+ return getTimeText(millis, "yy/MM/dd HH:mm", "yy/MM/dd HH:mm", false);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- public static Date formatDateStr(String date, String format) {
|
|
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
|
|
- Date date1 = null;
|
|
|
|
|
- try {
|
|
|
|
|
- date1 = sdf.parse(date);
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
|
|
+ public static String getTimeText1(long millis) {
|
|
|
|
|
+ return getTimeText(millis, "M月d日 %s HH:mm", "yyyy年M月d日 %s HH:mm", true);
|
|
|
|
|
+ }
|
|
|
|
|
+ public static String getTimeText(long millis,String timeF, String yearTimeF, boolean isShowAP) {
|
|
|
|
|
+ millis = millis < 10_000_000_000L ? millis * 1000 : millis;//秒的时间戳为10位数,要x1000 10_000_000_000L对应时间为2286-11-21 01:46:40
|
|
|
|
|
+ String result;
|
|
|
|
|
+ Calendar todayCalendar = Calendar.getInstance();
|
|
|
|
|
+ Calendar otherCalendar = Calendar.getInstance();
|
|
|
|
|
+ otherCalendar.setTimeInMillis(millis);
|
|
|
|
|
+
|
|
|
|
|
+ String timeFormat;
|
|
|
|
|
+ String yearTimeFormat;
|
|
|
|
|
+ String am_pm = "";
|
|
|
|
|
+ if(isShowAP) {
|
|
|
|
|
+ int hour = otherCalendar.get(Calendar.HOUR_OF_DAY);
|
|
|
|
|
+ if (hour >= 0 && hour < 6) {
|
|
|
|
|
+ am_pm = "凌晨";
|
|
|
|
|
+ } else if (hour >= 6 && hour < 12) {
|
|
|
|
|
+ am_pm = "早上";
|
|
|
|
|
+ } else if (hour == 12) {
|
|
|
|
|
+ am_pm = "中午";
|
|
|
|
|
+ } else if (hour > 12 && hour < 18) {
|
|
|
|
|
+ am_pm = "下午";
|
|
|
|
|
+ } else if (hour >= 18) {
|
|
|
|
|
+ am_pm = "晚上";
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ timeFormat = isShowAP ? String.format(Locale.CHINA, timeF, am_pm) : timeF;//"M月d日 " + am_pm + "HH:mm";
|
|
|
|
|
+ yearTimeFormat = isShowAP ? String.format(Locale.CHINA, yearTimeF, am_pm) : yearTimeF;//"yyyy年M月d日 " + am_pm + "HH:mm";
|
|
|
|
|
+
|
|
|
|
|
+ boolean yearTemp = todayCalendar.get(Calendar.YEAR) == otherCalendar.get(Calendar.YEAR);
|
|
|
|
|
+ if (yearTemp) {
|
|
|
|
|
+ int todayMonth = todayCalendar.get(Calendar.MONTH);
|
|
|
|
|
+ int otherMonth = otherCalendar.get(Calendar.MONTH);
|
|
|
|
|
+ if (todayMonth == otherMonth) {//表示是同一个月
|
|
|
|
|
+ int temp = todayCalendar.get(Calendar.DATE) - otherCalendar.get(Calendar.DATE);
|
|
|
|
|
+ switch (temp) {
|
|
|
|
|
+ case 0:
|
|
|
|
|
+ result = am_pm+getHourAndMin(millis);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 1:
|
|
|
|
|
+ result = "昨天 " +am_pm+ getHourAndMin(millis);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 2:
|
|
|
|
|
+ case 3:
|
|
|
|
|
+ case 4:
|
|
|
|
|
+ case 5:
|
|
|
|
|
+ case 6:
|
|
|
|
|
+ int dayOfMonth = otherCalendar.get(Calendar.WEEK_OF_MONTH);
|
|
|
|
|
+ int todayOfMonth = todayCalendar.get(Calendar.WEEK_OF_MONTH);
|
|
|
|
|
+ if (dayOfMonth == todayOfMonth) {//表示是同一周
|
|
|
|
|
+ int dayOfWeek = otherCalendar.get(Calendar.DAY_OF_WEEK);
|
|
|
|
|
+ if (dayOfWeek != 1) {//判断当前是不是星期日 如想显示为:周日 12:09 可去掉此判断
|
|
|
|
|
+ result = dayNames[otherCalendar.get(Calendar.DAY_OF_WEEK) - 1]+am_pm + getHourAndMin(millis);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ result = getTime(millis, timeFormat);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ result = getTime(millis, timeFormat);
|
|
|
|
|
+ }
|
|
|
|
|
+ break;
|
|
|
|
|
+ default:
|
|
|
|
|
+ result = getTime(millis, timeFormat);
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ result = getTime(millis, timeFormat);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ result = getYearTime(millis, yearTimeFormat);
|
|
|
}
|
|
}
|
|
|
- return date1;
|
|
|
|
|
|
|
+ return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 通过年份和月份 得到当月的日子
|
|
|
|
|
- *
|
|
|
|
|
- * @param year
|
|
|
|
|
- * @param month
|
|
|
|
|
- * @return
|
|
|
|
|
|
|
+ * 当天的显示时间格式
|
|
|
*/
|
|
*/
|
|
|
- public static int getMonthDays(int year, int month) {
|
|
|
|
|
- month++;
|
|
|
|
|
- switch (month) {
|
|
|
|
|
- case 1:
|
|
|
|
|
- case 3:
|
|
|
|
|
- case 5:
|
|
|
|
|
- case 7:
|
|
|
|
|
- case 8:
|
|
|
|
|
- case 10:
|
|
|
|
|
- case 12:
|
|
|
|
|
- return 31;
|
|
|
|
|
- case 4:
|
|
|
|
|
- case 6:
|
|
|
|
|
- case 9:
|
|
|
|
|
- case 11:
|
|
|
|
|
- return 30;
|
|
|
|
|
- case 2:
|
|
|
|
|
- if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
|
|
|
|
|
- return 29;
|
|
|
|
|
- } else {
|
|
|
|
|
- return 28;
|
|
|
|
|
- }
|
|
|
|
|
- default:
|
|
|
|
|
- return -1;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ public static String getHourAndMin(long time) {
|
|
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("HH:mm", Locale.CHINA);
|
|
|
|
|
+ return format.format(new Date(time));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 返回当前月份1号位于周几
|
|
|
|
|
- *
|
|
|
|
|
- * @param year 年份
|
|
|
|
|
- * @param month 月份,传入系统获取的,不需要正常的
|
|
|
|
|
- * @return 日:1 一:2 二:3 三:4 四:5 五:6 六:7
|
|
|
|
|
|
|
+ * 不同一周的显示时间格式
|
|
|
*/
|
|
*/
|
|
|
- public static int getFirstDayWeek(int year, int month) {
|
|
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
|
|
- calendar.set(year, month, 1);
|
|
|
|
|
- Log.d("DateView", "DateView:First:" + calendar.getFirstDayOfWeek());
|
|
|
|
|
- return calendar.get(Calendar.DAY_OF_WEEK);
|
|
|
|
|
|
|
+ public static String getTime(long time, String timeFormat) {
|
|
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat(timeFormat, Locale.CHINA);
|
|
|
|
|
+ return format.format(new Date(time));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static String getDayWeek(int year, int month, int day) {
|
|
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
|
|
- calendar.set(year, month, day);
|
|
|
|
|
- Log.d("DateView", "DateView:First:" + calendar.getFirstDayOfWeek());
|
|
|
|
|
-
|
|
|
|
|
- switch (calendar.get(Calendar.DAY_OF_WEEK)) {
|
|
|
|
|
- case 1:
|
|
|
|
|
- return "周日";
|
|
|
|
|
-
|
|
|
|
|
- case 2:
|
|
|
|
|
- return "周一";
|
|
|
|
|
-
|
|
|
|
|
- case 3:
|
|
|
|
|
- return "周二";
|
|
|
|
|
-
|
|
|
|
|
- case 4:
|
|
|
|
|
- return "周三";
|
|
|
|
|
-
|
|
|
|
|
- case 5:
|
|
|
|
|
- return "周四";
|
|
|
|
|
-
|
|
|
|
|
- case 6:
|
|
|
|
|
- return "周五";
|
|
|
|
|
-
|
|
|
|
|
- case 7:
|
|
|
|
|
- return "周六";
|
|
|
|
|
-
|
|
|
|
|
- default:
|
|
|
|
|
- return "";
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 不同年的显示时间格式
|
|
|
|
|
+ */
|
|
|
|
|
+ public static String getYearTime(long time, String yearTimeFormat) {
|
|
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat(yearTimeFormat, Locale.CHINA);
|
|
|
|
|
+ return format.format(new Date(time));
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
}
|
|
}
|