Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.33 KB | None | 0 0
  1. import android.content.Context;
  2. import android.text.format.DateUtils;
  3.  
  4. import com.example.jonathan.medsblaprueba.R;
  5. import com.example.jonathan.medsblaprueba.models.chat.ChatAbstract;
  6.  
  7. import java.text.ParseException;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Calendar;
  10. import java.util.Date;
  11. import java.util.Locale;
  12.  
  13.  
  14. /**
  15. * Created by jonathan on 27/03/17.
  16. */
  17.  
  18. public class UtilsDate {
  19.  
  20. private static final int SECONDS_IN_MILIS = 1000;
  21. private static final int MINUTES_IN_MILIS = SECONDS_IN_MILIS * 60;
  22.  
  23. private static final java.lang.String DEFAULT_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
  24.  
  25. public static boolean moreDifferenceThanAMinute(String previousMessageDate, String actualMessageDate) {
  26. SimpleDateFormat simpleDateFormat =
  27. new SimpleDateFormat(DEFAULT_TIME_FORMAT);
  28. try {
  29. Date actual = simpleDateFormat.parse(actualMessageDate);
  30. Date previous = simpleDateFormat.parse(previousMessageDate);
  31. long different = actual.getTime() - previous.getTime();
  32.  
  33. long elapsedMinutes = different / MINUTES_IN_MILIS;
  34. return elapsedMinutes > 1;
  35. } catch (ParseException e) {
  36. e.printStackTrace();
  37. }
  38. return false;
  39. }
  40.  
  41. public static boolean isSameDay(String stringDate1, String stringDate2) {
  42. SimpleDateFormat simpleDateFormat =
  43. new SimpleDateFormat(DEFAULT_TIME_FORMAT, Locale.getDefault());
  44. try {
  45. Date date1 = simpleDateFormat.parse(stringDate1);
  46. Calendar calendar1 = Calendar.getInstance();
  47. calendar1.setTime(date1);
  48.  
  49. Date date2 = simpleDateFormat.parse(stringDate2);
  50. Calendar calendar2 = Calendar.getInstance();
  51. calendar2.setTime(date2);
  52.  
  53. return calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH) &&
  54. calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH) &&
  55. calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR);
  56. } catch (ParseException e) {
  57. return false;
  58. }
  59. }
  60.  
  61. /** e.g. 20 April 2017 **/
  62. public static String getFormatDateDayMonthYearLong(String stringDate) {
  63. SimpleDateFormat simpleDateFormat =
  64. new SimpleDateFormat(DEFAULT_TIME_FORMAT, Locale.getDefault());
  65. try {
  66. Date date = simpleDateFormat.parse(stringDate);
  67. Calendar calendar = Calendar.getInstance();
  68. calendar.setTime(date);
  69. return calendar.get(Calendar.DAY_OF_MONTH) + " " +
  70. calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault()) + " " +
  71. calendar.get(Calendar.YEAR);
  72. } catch (ParseException e) {
  73. e.printStackTrace();
  74. }
  75. return "";
  76. }
  77.  
  78. /** e.g. 3.16 **/
  79. public static String getFormatDateTime(String stringDate) {
  80. SimpleDateFormat simpleDateFormat =
  81. new SimpleDateFormat(DEFAULT_TIME_FORMAT, Locale.getDefault());
  82. try {
  83. Date date = simpleDateFormat.parse(stringDate);
  84. Calendar calendar = Calendar.getInstance();
  85. calendar.setTime(date);
  86. return getMinuteHour(calendar);
  87. } catch (ParseException e) {
  88. e.printStackTrace();
  89. }
  90. return "";
  91. }
  92.  
  93. public static String getFormatDateCompleteMessage(String stringDate) {
  94. SimpleDateFormat simpleDateFormat =
  95. new SimpleDateFormat(DEFAULT_TIME_FORMAT, Locale.getDefault());
  96. try {
  97. Date date = simpleDateFormat.parse(stringDate);
  98. Calendar calendar = Calendar.getInstance();
  99. calendar.setTime(date);
  100. if (DateUtils.isToday(date.getTime())){
  101. return getMinuteHour(calendar);
  102. }
  103. return getDayMonthYear(calendar) + ", " + getMinuteHour(calendar);
  104. } catch (ParseException e) {
  105. e.printStackTrace();
  106. }
  107. return "";
  108. }
  109.  
  110. /** e.g. 15:27 **/
  111. private static String getMinuteHour(Calendar calendar) {
  112. int minute = calendar.get(Calendar.MINUTE);
  113. String minuteString = minute < 10 ? "0" + minute : String.valueOf(minute);
  114. return calendar.get(Calendar.HOUR_OF_DAY) + "." + minuteString;
  115. }
  116.  
  117. public static String getCurrentDate(){
  118. SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_TIME_FORMAT);
  119. return sdf.format(new Date());
  120. }
  121.  
  122. /** e.g. 27/04/2017 **/
  123. private static String getDayMonthYear(Calendar calendar) {
  124. return calendar.get(Calendar.DAY_OF_MONTH) + "/" + calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.YEAR);
  125. }
  126.  
  127. public static boolean isSooner(String date1, String date2) {
  128. SimpleDateFormat simpleDateFormat =
  129. new SimpleDateFormat(DEFAULT_TIME_FORMAT, Locale.getDefault());
  130. try {
  131. Date dateAux1 = simpleDateFormat.parse(date1);
  132. Date dateAux2 = simpleDateFormat.parse(date2);
  133.  
  134. return dateAux1.compareTo(dateAux2) <= 0;
  135. } catch (ParseException e) {
  136. return true;
  137. }
  138. }
  139.  
  140. public static boolean moreDifferenceThanAMinuteTs(String previousMessageDate, String actualMessageDate) {
  141. double different = Long.valueOf(actualMessageDate) - Long.valueOf(previousMessageDate);
  142.  
  143. double elapsedMinutes = different / MINUTES_IN_MILIS;
  144. return elapsedMinutes > 1;
  145. }
  146.  
  147. public static boolean isSameDayTs(String stringDate1, String stringDate2) {
  148. Calendar cal1 = Calendar.getInstance();
  149. cal1.setTimeInMillis(Long.valueOf(stringDate1));
  150.  
  151. Calendar cal2 = Calendar.getInstance();
  152. cal2.setTimeInMillis(Long.valueOf(stringDate2));
  153.  
  154. return isSameDay(cal1, cal2);
  155. }
  156.  
  157. public static boolean isSameDay(Calendar cal1, Calendar cal2) {
  158. return cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH) &&
  159. cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) &&
  160. cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR);
  161. }
  162.  
  163. public static boolean isToday(Calendar cal) {
  164. return isSameDay(cal, Calendar.getInstance());
  165. }
  166.  
  167. public static boolean isYesterday(Calendar cal) {
  168. return isBeforeDay(cal, Calendar.getInstance());
  169. }
  170.  
  171. public static boolean isBeforeDay(Calendar cal1, Calendar cal2) {
  172. if (cal1.get(Calendar.ERA) < cal2.get(Calendar.ERA)) return true;
  173. if (cal1.get(Calendar.ERA) > cal2.get(Calendar.ERA)) return false;
  174. if (cal1.get(Calendar.YEAR) < cal2.get(Calendar.YEAR)) return true;
  175. if (cal1.get(Calendar.YEAR) > cal2.get(Calendar.YEAR)) return false;
  176. return cal1.get(Calendar.DAY_OF_YEAR) < cal2.get(Calendar.DAY_OF_YEAR);
  177. }
  178.  
  179. /** e.g. 20 April 2017 **/
  180. public static String getFormatDateDayMonthYearLongTs(String stringDate, Context context) {
  181. Calendar calendar = Calendar.getInstance();
  182. calendar.setTimeInMillis(Long.valueOf(stringDate));
  183.  
  184. if (isToday(calendar)){
  185. return context.getResources().getString(R.string.today);
  186. }
  187.  
  188. if (isYesterday(calendar)){
  189. return context.getResources().getString(R.string.yesterday);
  190. }
  191.  
  192. // if (isYesterday(calendar)){
  193. // return context.getResources().getString(R.string.today);
  194. // }
  195.  
  196. return calendar.get(Calendar.DAY_OF_MONTH) + " " +
  197. calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault()) + " " +
  198. calendar.get(Calendar.YEAR);
  199. }
  200.  
  201. /** e.g. 3.16 **/
  202. public static String getFormatDateTimeTs(String stringDate) {
  203. Calendar calendar = Calendar.getInstance();
  204. calendar.setTimeInMillis(Long.valueOf(stringDate));
  205. return getMinuteHour(calendar);
  206. }
  207.  
  208. public static String getFormatDateCompleteMessageTs(String stringDate) {
  209. Calendar calendar = Calendar.getInstance();
  210. calendar.setTimeInMillis(Long.valueOf(stringDate));
  211. if (DateUtils.isToday(Long.valueOf(stringDate))) {
  212. return getMinuteHour(calendar);
  213. }
  214. return getDayMonthYear(calendar) + ", " + getMinuteHour(calendar);
  215. }
  216.  
  217.  
  218.  
  219. public static boolean isSoonerTs(String date1, String date2) {
  220. return Long.valueOf(date1) < Long.valueOf(date2);
  221. }
  222.  
  223. public static String getCurrentTimeMillis() {
  224. return String.valueOf(System.currentTimeMillis());
  225. }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement