Java Utililty Methods Date Before

List of utility methods to do Date Before

Description

The list of methods to do Date Before are organized into topic(s).

Method

DategetBeforeOrAfterDateByDayNumber(int dayNumber)
get Before Or After Date By Day Number
return getBeforeOrAfterDateByDayNumber(new Date(), dayNumber);
DategetDateBefore(Date d, int day)
get Date Before
Calendar now = Calendar.getInstance();
now.setTime(d);
now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = simpleDateFormat.parse(simpleDateFormat.format(now.getTime()));
return date;
DategetDateBefore(Date d, int day)
get Date Before
Calendar now = Calendar.getInstance();
now.setTime(d);
now.set(Calendar.HOUR_OF_DAY, 0);
now.set(Calendar.MINUTE, 0);
now.set(Calendar.SECOND, 0);
now.set(Calendar.MILLISECOND, 0);
now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
return now.getTime();
...
DategetDateBefore(Date date, int days)
Get date before the input date according to param-days
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(java.util.Calendar.DAY_OF_YEAR, days);
return calendar.getTime();
StringgetDateBefore(int offsetDay, String strFormat)
get Date Before
String strDay = "";
long nNow = getTimestamp();
long mydate = nNow - (long) offsetDay * 24 * 3600 * 1000;
SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
strDay = sdf.format(new Date(mydate));
return strDay;
DategetDateBefore(int year, int month, int day)
get Date Before
Date result = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(result);
calendar.add(Calendar.YEAR, year);
calendar.add(Calendar.MONTH, month);
calendar.add(Calendar.DAY_OF_MONTH, day);
result = calendar.getTime();
return result;
...
StringgetDateBefore(String d, int day)
get Date Before
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Calendar now = Calendar.getInstance();
try {
    now.setTime(format.parse(d));
    now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
} catch (ParseException e) {
    e.printStackTrace();
return format.format(now.getTime());
DategetDateBeforeHours(Date date, int hours)
get Date Before Hours
return getDateAfterHours(date, 0 - hours);
DategetDateBeforeNextMonth(Date date, Integer month, Integer day)
get Date Before Next Month
if (date == null || month == null || day == null) {
    return date;
Calendar cal = Calendar.getInstance(Locale.CHINA);
cal.setTime(date);
cal.add(Calendar.MONTH, month);
cal.add(Calendar.DATE, day);
return cal.getTime();
...
DategetDateBeforeOrAfter(int iDate)
get Date Before Or After
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, iDate);
return cal.getTime();