Guest User

Untitled

a guest
May 23rd, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. package itomych.com.jti.util;
  2.  
  3. import android.text.format.DateFormat;
  4.  
  5. import java.text.ParseException;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Calendar;
  8. import java.util.Date;
  9. import java.util.Locale;
  10.  
  11. /**
  12. *
  13. * DateUtils class.
  14. *
  15. * TODO 18-05-2018 (alexischenko): most of this class code is crap, please do not use it
  16. * Note
  17.  
  18. *
  19. * Most of DateUtils methods served ProfileEdit screen needs which at the current moment (2018)are not included to screen classes ()
  20. */
  21.  
  22. public class DateUtils {
  23.  
  24. /* T
  25. sd
  26. */
  27.  
  28. public static final String DATE_FORMAT_PATTERN = "dd.MM.yyyy";
  29. public static final String YYYY_MM_DD_T_HH_MM_SS_Z = "yyyy-MM-dd'T'HH:mm:ss'Z'";
  30. public static final String DD_MM_YYYY = "dd/MM/yyyy";
  31.  
  32. private static final String DATE_TIME_PATTERN = "yyyy-MM-dd'T'HH:mm:ss";
  33.  
  34. /**
  35. * used to display user b-day date on UI
  36. */
  37. private static final String SHORT_DATE_FORMAT_PATTERN = "dd/MM/yyyy";
  38.  
  39. public static String getCurrentDateFormatted() {
  40. Date c = Calendar.getInstance().getTime();
  41. SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT_PATTERN);
  42. return df.format(c);
  43. }
  44.  
  45. /**
  46. * Method used by current class getShortDate() only
  47. *
  48. */
  49. private static Date getDate(String date) {
  50. try {
  51. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_TIME_PATTERN, Locale.getDefault());
  52. simpleDateFormat.setTimeZone(Calendar.getInstance().getTimeZone());
  53. return simpleDateFormat.parse(date);
  54. } catch (ParseException e) {
  55. return new Date();
  56. }
  57. }
  58.  
  59. /**
  60. * Method used by current class getShortDate() only
  61. *
  62. */
  63. private static String format(Date date, String pattern) {
  64. try {
  65. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, Locale.getDefault());
  66. simpleDateFormat.setTimeZone(Calendar.getInstance().getTimeZone());
  67. return simpleDateFormat.format(date);
  68. } catch (Exception e) {
  69. return "";
  70. }
  71. }
  72.  
  73. /**
  74. * Used by UserProfileFragment
  75. * TODO: 18-05-2018: move this method to User (jti.data.model) class
  76. */
  77. public static String getShortDate(String date) {
  78. return format(getDate(date), SHORT_DATE_FORMAT_PATTERN);
  79. }
  80.  
  81. public static String getDateSubstractYears(int yearsToAdd) {
  82. try {
  83. SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_PATTERN);
  84. Date date = sdf.parse(getCurrentTime());
  85.  
  86. Calendar calendar = Calendar.getInstance();
  87. calendar.setTime(date);
  88. calendar.add(Calendar.YEAR, yearsToAdd);
  89.  
  90. return sdf.format(calendar.getTime());
  91.  
  92. } catch (Exception e) {
  93. e.printStackTrace();
  94. return "";
  95. }
  96. }
  97.  
  98. public static String getCurrentTime() {
  99. Calendar calendar = Calendar.getInstance();
  100. SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_PATTERN);
  101. return dateFormat.format(calendar.getTime());
  102. }
  103.  
  104. public static long getTimestamp(String date) {
  105. try {
  106. Date localDate = new SimpleDateFormat(DATE_FORMAT_PATTERN, Locale.getDefault()).parse(date);
  107. return localDate.getTime();
  108. } catch (ParseException e) {
  109. e.printStackTrace();
  110. return -1;
  111. }
  112. }
  113.  
  114. public static int getYear(String formattedDate) {
  115. SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_PATTERN);
  116. try {
  117. Date date = format.parse(formattedDate);
  118. return Integer.parseInt((String) DateFormat.format("yyyy", date));
  119. } catch (ParseException e) {
  120. e.printStackTrace();
  121. return -1;
  122. }
  123. }
  124.  
  125. public static int getMonth(String formattedDate) {
  126. SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_PATTERN);
  127. try {
  128. Date date = format.parse(formattedDate);
  129. return Integer.parseInt((String) DateFormat.format("MM", date));
  130. } catch (ParseException e) {
  131. e.printStackTrace();
  132. return -1;
  133. }
  134. }
  135.  
  136. public static int getDayOfMonth(String formattedDate) {
  137. SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_PATTERN);
  138. try {
  139. Date date = format.parse(formattedDate);
  140. return Integer.parseInt((String) DateFormat.format("dd", date));
  141. } catch (ParseException e) {
  142. e.printStackTrace();
  143. return -1;
  144. }
  145. }
  146.  
  147. public static String getFormattedDate(int year, int month, int day) {
  148. Calendar calendar = Calendar.getInstance();
  149. calendar.set(year, month, day, 0, 0);
  150.  
  151. SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_PATTERN);
  152. return dateFormat.format(calendar.getTime());
  153. }
  154.  
  155.  
  156. }
Add Comment
Please, Sign In to add comment