Last active
August 29, 2015 14:01
-
-
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 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
/** | |
* 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