Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package softograph.mahmud.com.shaarryup.UI.Utility;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.GregorianCalendar;
- import java.util.Locale;
- import java.util.TimeZone;
- import java.util.concurrent.TimeUnit;
- public class DateUtils {
- public static long[] getTimeDifferenceByPassigTimeStamp(long timeStamp) {
- long timeStampForTheSong = timeStamp;
- if (timeStampForTheSong >= System.currentTimeMillis()) {
- return null;
- }
- long diff = System.currentTimeMillis() - timeStampForTheSong;
- long[] allTypeOfDates = new long[4];
- allTypeOfDates[0] = diff / (60 * 1000 * 60 * 24) % 60;// Sending day
- allTypeOfDates[1] = diff / (60 * 1000 * 60) % 60; // Hour of the day
- allTypeOfDates[2] = diff / (60 * 1000) % 60; // minuite of the day
- allTypeOfDates[3] = diff / 1000 % 60; // seconds of the day
- return allTypeOfDates;
- }
- public static String TimeBefore(String dateInString) {
- String timepassed = "Just now";
- try {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
- String inputString = dateInString;
- Date date = sdf.parse(inputString);
- System.out.println("in milliseconds: " + date.getTime());
- long[] time = DateUtils.getTimeDifferenceByPassigTimeStamp(date.getTime());
- if (time != null) {
- if (time[2] == 0) {
- timepassed = time[3] + " sec";
- } else if (time[1] == 0) {
- timepassed = time[2] + " min";
- } else if (time[0] == 0) {
- timepassed = time[2] + " hour";
- } else {
- timepassed = time[0] + " day";
- }
- return timepassed;
- } else {
- return timepassed;
- }
- } catch (Exception e) {
- e.printStackTrace();
- return timepassed;
- }
- }
- public static String ConvertMilliSecondsToFormattedDate(String milliSeconds) {
- String dateFormat = "dd-MM-yyyy hh:mm";
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
- Calendar calendar = Calendar.getInstance();
- calendar.setTimeInMillis(Long.parseLong(milliSeconds));
- return simpleDateFormat.format(calendar.getTime());
- }
- /* Server will take this time as GMT + 00:00 */
- public static String GetUniformGMTtime() {
- final Date currentTime = new Date();
- final SimpleDateFormat sdf
- = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.UK);
- final SimpleDateFormat sdfout
- = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- // Give it to me in GMT time.
- sdf.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
- //sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
- System.out.println("GMT --OUT-- time: " + sdfout.format(currentTime));
- return sdf.format(currentTime);
- }
- /* When user will populate this time will be converted into User local GMT time */
- public static String getLocalGMTMeaningTime(String time_from_server) {
- Date date = null;
- try {
- final SimpleDateFormat sdfout = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- date = sdfout.parse(time_from_server);
- Calendar cal = new GregorianCalendar();
- cal.setTime(date);
- //Adding 21 Hours to your Date
- cal.add(Calendar.HOUR_OF_DAY, getGMTOFFSET());
- return sdfout.format(cal.getTime());
- } catch (Exception e) {
- e.printStackTrace();
- return time_from_server;
- }
- }/* When user will populate this time will be converted into User local GMT time */
- public static Date
- getLocalGMTMeaningTimeAsDate(String time_from_server) {
- Date date = null;
- try {
- final SimpleDateFormat sdfout = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- date = sdfout.parse(time_from_server);
- Calendar cal = new GregorianCalendar();
- cal.setTime(date);
- //Adding 21 Hours to your Date
- cal.add(Calendar.HOUR_OF_DAY, getGMTOFFSET());
- return cal.getTime();
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- public static int getGMTOFFSET() {
- Calendar mCalendar = new GregorianCalendar();
- TimeZone mTimeZone = mCalendar.getTimeZone();
- int mGMTOffset = mTimeZone.getRawOffset();
- long hour = TimeUnit.HOURS.convert(mGMTOffset, TimeUnit.MILLISECONDS);
- return (int) hour;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement