Skip to content

Instantly share code, notes, and snippets.

@trplll
Created August 31, 2017 19:21
Show Gist options
  • Save trplll/8280dd5e181a2ed46791e15a63fccbcf to your computer and use it in GitHub Desktop.
Save trplll/8280dd5e181a2ed46791e15a63fccbcf to your computer and use it in GitHub Desktop.
Simple static method to return formatted string with today's date , or the date calculated based on an integer parameter
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class dateexample {
public static void main(String[] args) {
System.out.println(getdate());
System.out.println(getdate(0));
System.out.println(getdate(1));
System.out.println(getdate(-1));
}
public static String getdate() {
return getdate(0);
}
public static String getdate(Integer dayOffset) {
if (dayOffset == null)
dayOffset = 0;
long offset = dayOffset;
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(System.currentTimeMillis() + (offset * (1000 * 60 * 60 * 24))));
return (new SimpleDateFormat("MM/dd/yyyy").format(cal.getTime()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment