Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. package softograph.mahmud.com.shaarryup.UI.Utility;
  2.  
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. import java.util.GregorianCalendar;
  7. import java.util.Locale;
  8. import java.util.TimeZone;
  9. import java.util.concurrent.TimeUnit;
  10.  
  11.  
  12. public class DateUtils {
  13.  
  14. public static long[] getTimeDifferenceByPassigTimeStamp(long timeStamp) {
  15.  
  16. long timeStampForTheSong = timeStamp;
  17. if (timeStampForTheSong >= System.currentTimeMillis()) {
  18. return null;
  19. }
  20. long diff = System.currentTimeMillis() - timeStampForTheSong;
  21.  
  22. long[] allTypeOfDates = new long[4];
  23. allTypeOfDates[0] = diff / (60 * 1000 * 60 * 24) % 60;// Sending day
  24. allTypeOfDates[1] = diff / (60 * 1000 * 60) % 60; // Hour of the day
  25. allTypeOfDates[2] = diff / (60 * 1000) % 60; // minuite of the day
  26. allTypeOfDates[3] = diff / 1000 % 60; // seconds of the day
  27. return allTypeOfDates;
  28. }
  29.  
  30. public static String TimeBefore(String dateInString) {
  31. String timepassed = "Just now";
  32.  
  33. try {
  34. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  35. sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
  36.  
  37. String inputString = dateInString;
  38.  
  39. Date date = sdf.parse(inputString);
  40. System.out.println("in milliseconds: " + date.getTime());
  41.  
  42. long[] time = DateUtils.getTimeDifferenceByPassigTimeStamp(date.getTime());
  43. if (time != null) {
  44. if (time[2] == 0) {
  45. timepassed = time[3] + " sec";
  46.  
  47. } else if (time[1] == 0) {
  48. timepassed = time[2] + " min";
  49.  
  50. } else if (time[0] == 0) {
  51. timepassed = time[2] + " hour";
  52.  
  53. } else {
  54. timepassed = time[0] + " day";
  55.  
  56. }
  57. return timepassed;
  58. } else {
  59. return timepassed;
  60.  
  61. }
  62. } catch (Exception e) {
  63. e.printStackTrace();
  64. return timepassed;
  65. }
  66.  
  67. }
  68.  
  69. public static String ConvertMilliSecondsToFormattedDate(String milliSeconds) {
  70.  
  71. String dateFormat = "dd-MM-yyyy hh:mm";
  72. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
  73. Calendar calendar = Calendar.getInstance();
  74. calendar.setTimeInMillis(Long.parseLong(milliSeconds));
  75. return simpleDateFormat.format(calendar.getTime());
  76. }
  77.  
  78. /* Server will take this time as GMT + 00:00 */
  79. public static String GetUniformGMTtime() {
  80. final Date currentTime = new Date();
  81.  
  82. final SimpleDateFormat sdf
  83. = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.UK);
  84. final SimpleDateFormat sdfout
  85. = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  86.  
  87. // Give it to me in GMT time.
  88. sdf.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
  89. //sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
  90. System.out.println("GMT --OUT-- time: " + sdfout.format(currentTime));
  91. return sdf.format(currentTime);
  92. }
  93.  
  94. /* When user will populate this time will be converted into User local GMT time */
  95. public static String getLocalGMTMeaningTime(String time_from_server) {
  96.  
  97. Date date = null;
  98. try {
  99. final SimpleDateFormat sdfout = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  100.  
  101. date = sdfout.parse(time_from_server);
  102.  
  103. Calendar cal = new GregorianCalendar();
  104. cal.setTime(date);
  105.  
  106. //Adding 21 Hours to your Date
  107. cal.add(Calendar.HOUR_OF_DAY, getGMTOFFSET());
  108. return sdfout.format(cal.getTime());
  109.  
  110. } catch (Exception e) {
  111. e.printStackTrace();
  112. return time_from_server;
  113. }
  114. }/* When user will populate this time will be converted into User local GMT time */
  115.  
  116. public static Date
  117. getLocalGMTMeaningTimeAsDate(String time_from_server) {
  118.  
  119. Date date = null;
  120. try {
  121. final SimpleDateFormat sdfout = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  122.  
  123. date = sdfout.parse(time_from_server);
  124.  
  125. Calendar cal = new GregorianCalendar();
  126. cal.setTime(date);
  127.  
  128. //Adding 21 Hours to your Date
  129. cal.add(Calendar.HOUR_OF_DAY, getGMTOFFSET());
  130. return cal.getTime();
  131.  
  132. } catch (Exception e) {
  133. e.printStackTrace();
  134. return null;
  135. }
  136. }
  137.  
  138. public static int getGMTOFFSET() {
  139. Calendar mCalendar = new GregorianCalendar();
  140. TimeZone mTimeZone = mCalendar.getTimeZone();
  141. int mGMTOffset = mTimeZone.getRawOffset();
  142. long hour = TimeUnit.HOURS.convert(mGMTOffset, TimeUnit.MILLISECONDS);
  143. return (int) hour;
  144. }
  145.  
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement