Created
July 11, 2017 07:16
-
-
Save SteveKamau72/f2b691dc68559ca2395f7259a2ac2993 to your computer and use it in GitHub Desktop.
Get time differences between current time and future a date.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.girlathome.utilities; | |
import android.util.Log; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.Locale; | |
/** | |
* Created by steve on 6/12/17. | |
*/ | |
public class TimeTask { | |
public String dateTimeDifference(String dateTime) { | |
String dateStart = getCurrentTimeStamp24HRS(); | |
String dateStop = dateTime; | |
String dateTimeDiff = "0"; | |
//HH converts hour in 24 hours format (0-23), day calculation | |
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); | |
Date d1 = null; | |
Date d2 = null; | |
try { | |
d1 = format.parse(dateStart); | |
d2 = format.parse(dateStop); | |
//in milliseconds | |
long diff = d2.getTime() - d1.getTime(); | |
long diffSeconds = diff / 1000 % 60; | |
long diffMinutes = diff / (60 * 1000) % 60; | |
long diffHours = diff / (60 * 60 * 1000) % 24; | |
long diffDays = diff / (24 * 60 * 60 * 1000); | |
Log.d("Date-", diffDays + " days, "); | |
Log.d("Date-", diffHours + " hours, "); | |
Log.d("Date-", diffMinutes + " minutes, "); | |
Log.d("Date-", diffSeconds + " seconds."); | |
if (diffDays > 1) { | |
Log.d("Date-", diffDays + " days, "); | |
dateTimeDiff = diffDays + " days"; | |
} else if (diffDays == 1) { | |
dateTimeDiff = diffDays + " day"; | |
} else if (diffHours > 1) { | |
Log.d("Date-", diffHours + " hours, "); | |
dateTimeDiff = diffHours + " hours"; | |
} else if (diffHours == 1) { | |
Log.d("Date-", diffHours + " hours, "); | |
dateTimeDiff = diffHours + " hour"; | |
} else if (diffMinutes >= 1) { | |
Log.d("Date-", diffSeconds + " seconds."); | |
dateTimeDiff = diffMinutes + " min"; | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
Log.d("Date-", " error, "); | |
} | |
return dateTimeDiff; | |
} | |
public String getCurrentTimeStamp24HRS() { | |
SimpleDateFormat sdfDate = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");//dd/MM/yyyy | |
Date now = new Date(); | |
return sdfDate.format(now); | |
} | |
public String formatIntoDayOfWeek(String strCurrentDate) throws ParseException { | |
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm a", Locale.ENGLISH); | |
Date newDate = format.parse(strCurrentDate); | |
format = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm a", Locale.ENGLISH); | |
return format.format(newDate); | |
} | |
public String formatInto24HRS(String strCurrentDate) throws ParseException { | |
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm a", Locale.ENGLISH); | |
Date newDate = format.parse(strCurrentDate); | |
format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.ENGLISH); | |
return format.format(newDate); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use it like so:
TimeTask timeTask = new TimeTask(); timeTask.dateTimeDifference("07/08/2017 10:21:22);