Skip to content

Instantly share code, notes, and snippets.

@rodrigoborgesdeoliveira
Created February 19, 2018 12:47
Show Gist options
  • Save rodrigoborgesdeoliveira/b4aad486d0cbad17cef98a0e38169ad6 to your computer and use it in GitHub Desktop.
Save rodrigoborgesdeoliveira/b4aad486d0cbad17cef98a0e38169ad6 to your computer and use it in GitHub Desktop.
Manipulate Date in java.
/**
* Returns a {@link Date} corresponding to the given Date plus time passed in milliseconds.
*
* @param startingDate The base Date used to get the future Date.
* @param timePassed The time (in milliseconds) to be added to the <b>startingDate</b>
* @return The {@link Date} in the future after <b>timePassed</b> milliseconds from <b>startingDate</b>.
*/
public Date dateAfterMillis(Date startingDate, long timePassed) {
Date endingDate = new Date();
endingDate.setTime(startingDate.getTime() + timePassed);
return endingDate;
}
/**
* Converts given string to date.
*
* @param rawDate The string to be converted.
* @return {@link Date} converted from the given string.
*/
public Date dateFromString(String rawDate) {
if (rawDate.equals("")) {
return new Date(); // Will return the current time
}
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = new Date();
try {
date = format.parse(rawDate);
} catch (ParseException e) {
Log.e(TAG, "Could not parse the date to format");
}
return date;
}
/**
* Converts the given date to local timezone in the format "hh:mm a".
*
* @param date The {@link Date} to be converted.
* @return A {@link String} in the format "hh:mm a" for the converted date.
*/
public String dateToLocalTimeZone(Date date) {
SimpleDateFormat format = new SimpleDateFormat("hh:mm a", Locale.getDefault());
format.setTimeZone(TimeZone.getDefault());
return format.format(date);
}
/**
* Returns how many milliseconds are left to reach the given date.
*
* @param endDate {@link Date} to calculate how many milliseconds are left to get to it.
* @return The time left, in milliseconds, to get to the given date.
*/
public long millisLeftToDate(Date endDate) {
return endDate.getTime() - (new Date()).getTime();
}
/**
* Converts given date to a string.
*
* @param date The date to be converted.
* @return {@link String} converted from the given date.
*/
public String stringFromDate(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateString;
dateString = format.format(date);
return dateString;
}
/**
* Returns the time passed since given date.
*
* @param pastDate The basis {@link Date} from the past.
* @return A {@link String} in the format "H:MM:SS" or "MM:SS" corresponding to the time passed.
*/
public String timeSinceDate(Date pastDate) {
long timePassed = (new Date()).getTime() - pastDate.getTime();
return DateUtils.formatElapsedTime(timePassed / 1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment