937d543f24ea70b2c111a9d7fe3b9839088b452b.svn-base 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package com.example.myapplication.utils;
  2. import android.text.TextUtils;
  3. import com.orhanobut.logger.Logger;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Calendar;
  7. import java.util.Date;
  8. import java.util.Locale;
  9. public class DateUtils {
  10. private static final String UNIT_MONTH = "月前";
  11. private static final String UNIT_DAY = "天前";
  12. private static final String UNIT_HOUR = "小时前";
  13. private static final String UNIT_MINUTE = "分钟前";
  14. public static final String TYPE_CHAR_FORMAT_DAY = "yyyy年MM月dd日";
  15. public static final String TYPE_FULL_COMMON_FORMAT = "yyyy-MM-dd HH:mm:ss";
  16. public static final String TYPE_COMMON_FORMAT_DAY = "yyyy-MM-dd";
  17. public static final String TYPE_FORMAT_MONTH = "yyyy-MM";
  18. public static final String TYPE_MONTHY_DAY = "MM-dd";
  19. public static String formatDate(String oldDateType, String newDateType, String value) {
  20. final SimpleDateFormat format = new SimpleDateFormat(oldDateType);
  21. Logger.d("values: " + value);
  22. Date date = null;
  23. try {
  24. date = format.parse(value);
  25. } catch (ParseException e) {
  26. Logger.e("format date error,please check it.");
  27. return value;
  28. }
  29. final SimpleDateFormat newFormat = new SimpleDateFormat(newDateType);
  30. return newFormat.format(date);
  31. }
  32. /**
  33. * 传入时间及格式
  34. *
  35. * @param format 格式
  36. * @param value 时间
  37. * @return 特定格式时间
  38. */
  39. public static String getShowTime(String format, String value) {
  40. SimpleDateFormat sf = new SimpleDateFormat(format);
  41. try {
  42. Date date = sf.parse(value);
  43. long targetTime = date.getTime();
  44. return getShowTime(targetTime);
  45. } catch (ParseException e) {
  46. return 1 + UNIT_MINUTE;
  47. }
  48. }
  49. /**
  50. * 传入时间及格式
  51. *
  52. * @param time long类型时间
  53. * @return 特定格式时间
  54. */
  55. public static String getShowTime(long time) {
  56. Date nowDate = new Date();
  57. long nowTime = nowDate.getTime();
  58. long differTime = nowTime - time;
  59. long minute = 1000 * 60;
  60. if (differTime <= 0 || differTime < minute) {
  61. return 1 + UNIT_MINUTE;
  62. }
  63. long hour = 1000 * 60 * 60;
  64. if (differTime < hour) {
  65. long showMinute = (differTime / minute) % 60;
  66. return (showMinute == 0 ? 1 : showMinute) + UNIT_MINUTE;
  67. }
  68. long day = 24 * hour;
  69. if (differTime < day) {
  70. long showHour = (differTime / hour) % 24;
  71. return (showHour == 0 ? 1 : showHour) + UNIT_HOUR;
  72. }
  73. long month = 30 * day;
  74. if (differTime < month) {
  75. long showDay = (differTime / day) % 30;
  76. return (showDay == 0 ? 1 : showDay) + UNIT_DAY;
  77. }
  78. long year = 12 * month;
  79. if (differTime < year) {
  80. long showMonth = (differTime / month) % 12;
  81. return (showMonth == 0 ? 1 : showMonth) + UNIT_MONTH;
  82. }
  83. return 12 + UNIT_MONTH;
  84. }
  85. public static long getDateToLong(String format, String date) {
  86. SimpleDateFormat sf = new SimpleDateFormat(format);
  87. try {
  88. Date parse = sf.parse(date);
  89. return parse.getTime();
  90. } catch (ParseException e) {
  91. Logger.e(e.getLocalizedMessage());
  92. return new Date().getTime();
  93. }
  94. }
  95. public static String getDataByFormat(String format, long data) {
  96. SimpleDateFormat sf = new SimpleDateFormat(format);
  97. return sf.format(new Date(data));
  98. }
  99. public static int getMonthByDate() {
  100. // 获取日期实例
  101. Calendar calendar = Calendar.getInstance();
  102. // 这里要注意,月份是从0开始。
  103. int month = calendar.get(Calendar.MONTH);
  104. Logger.d("getMonthByDate : " + (month + 1));
  105. return month + 1;
  106. }
  107. public static int getYearByDate() {
  108. // 获取日期实例
  109. Calendar calendar = Calendar.getInstance();
  110. // 这里要注意,月份是从0开始。
  111. int year = calendar.get(Calendar.YEAR);
  112. return year;
  113. }
  114. public static int getMonthByDate(long date) {
  115. // 获取日期实例
  116. Calendar calendar = Calendar.getInstance();
  117. // 将日历设置为指定的时间
  118. calendar.setTimeInMillis(date);
  119. // 这里要注意,月份是从0开始。
  120. int month = calendar.get(Calendar.MONTH);
  121. Logger.d("getMonthByDate : " + (month + 1));
  122. return month + 1;
  123. }
  124. public static int getDayByDate() {
  125. // 获取日期实例
  126. Calendar calendar = Calendar.getInstance();
  127. // 这里要注意,月份是从0开始。
  128. int day = calendar.get(Calendar.DAY_OF_MONTH);
  129. return day;
  130. }
  131. public static String getPreOrNextMonth(long time, boolean isPre) {
  132. Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
  133. ca.setTimeInMillis(time);
  134. Date now = ca.getTime();
  135. if (isPre) {
  136. ca.add(Calendar.MONTH, -1); //月份减1
  137. } else {
  138. ca.add(Calendar.MONTH, +1); //月份减1
  139. }
  140. Date lastMonth = ca.getTime(); //结果
  141. SimpleDateFormat sf = new SimpleDateFormat(TYPE_COMMON_FORMAT_DAY);
  142. return sf.format(lastMonth);
  143. }
  144. public static String getPreOrNextMonth(long time, boolean isPre, String formatType) {
  145. Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
  146. ca.setTimeInMillis(time);
  147. Date now = ca.getTime();
  148. if (isPre) {
  149. ca.add(Calendar.MONTH, -1); //月份减1
  150. } else {
  151. ca.add(Calendar.MONTH, +1); //月份减1
  152. }
  153. Date lastMonth = ca.getTime(); //结果
  154. SimpleDateFormat sf = new SimpleDateFormat(formatType);
  155. return sf.format(lastMonth);
  156. }
  157. public static String getPreOrNextDay(long time, boolean isPre) {
  158. Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
  159. ca.setTimeInMillis(time);
  160. Date now = ca.getTime();
  161. if (isPre) {
  162. ca.add(Calendar.DATE, -1);
  163. } else {
  164. ca.add(Calendar.DATE, +1);
  165. }
  166. Date lastMonth = ca.getTime(); //结果
  167. SimpleDateFormat sf = new SimpleDateFormat(TYPE_COMMON_FORMAT_DAY);
  168. return sf.format(lastMonth);
  169. }
  170. public static Date getPreOrNextDayToDate(long time, boolean isPre) {
  171. Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
  172. ca.setTimeInMillis(time);
  173. Date now = ca.getTime();
  174. if (isPre) {
  175. ca.add(Calendar.DATE, -1);
  176. } else {
  177. ca.add(Calendar.DATE, +1);
  178. }
  179. Date lastMonth = ca.getTime(); //结果
  180. return lastMonth;
  181. }
  182. public static String[] WEEK_DAYS = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
  183. /**
  184. * 获取日期是周几
  185. *
  186. * @param dt 日期
  187. * @return 周几的角标
  188. */
  189. public static int getWeekOfDate(Date dt) {
  190. Calendar cal = Calendar.getInstance();
  191. cal.setTime(dt);
  192. int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
  193. if (w < 0)
  194. w = 0;
  195. return w;
  196. }
  197. /**
  198. * 获取每月多少天
  199. *
  200. * @param formatType 日期的格式化
  201. * @param date 日期
  202. * @return 当月总天数
  203. */
  204. public static int getDaysByMonth(String formatType, String date) {
  205. Calendar c = Calendar.getInstance();
  206. if (date == null) {
  207. return c.getActualMaximum(Calendar.DAY_OF_MONTH);
  208. } else {
  209. SimpleDateFormat sf = new SimpleDateFormat(formatType);
  210. try {
  211. Date d = sf.parse(date);
  212. c.setTime(d);
  213. return c.getActualMaximum(Calendar.DAY_OF_MONTH);
  214. } catch (ParseException e) {
  215. return 30;
  216. }
  217. }
  218. }
  219. public static String convertStringToDateForCST(String currentTimeType, String time) {
  220. if (TextUtils.isEmpty(time)) {
  221. return null;
  222. }
  223. try {
  224. SimpleDateFormat formatter = new SimpleDateFormat("EEE d MMM yyyy", Locale.ENGLISH);
  225. SimpleDateFormat sdt = new SimpleDateFormat(currentTimeType);
  226. Date date = sdt.parse(time);
  227. return formatter.format(date);
  228. } catch (Exception e) {
  229. e.printStackTrace();
  230. return null;
  231. }
  232. }
  233. }