Skip to content

Instantly share code, notes, and snippets.

@azakordonets
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azakordonets/5bc361c5116cc5a5ac62 to your computer and use it in GitHub Desktop.
Save azakordonets/5bc361c5116cc5a5ac62 to your computer and use it in GitHub Desktop.
get date as a string basing on date format and number of days since now
/**
* This method allows to generate time stamp with predefined format, but not only from today, but from date that we
* pass as a parameter to the method.
* If days = 0 , then we return current date stamp
* If number is > 0, then we return date in the future
* If number is < 0 then we return date from the past
* @param days number of days since stated date
* @param format it's a format in which date string will be returned
* @return date as a String
*/
public static String getDate(int days, String format) {
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
Date currentDate = new Date();
if (days == 0){
Date newDate = new Date(currentDate.getTime() + TimeUnit.MINUTES.toMillis(1));
return dateFormat.format(newDate);
} else {
Date newDate = new Date(currentDate.getTime() +TimeUnit.DAYS.toMillis(days)+ TimeUnit.HOURS.toMillis(1));
return dateFormat.format(newDate);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment