Java Utililty Methods Day of Month

List of utility methods to do Day of Month

Description

The list of methods to do Day of Month are organized into topic(s).

Method

StringgetLastdayOfMonth(String year, String month)
get Lastday Of Month
return year + month + getNumberOfDays(year, month);
StringgetLastDayOfMonth(String year, String month)
get Last Day Of Month
String LastDay = "";
Calendar cal = Calendar.getInstance();
Date date_;
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(year + "-" + month + "-14");
cal.setTime(date);
int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, value);
date_ = cal.getTime();
...
intgetLastDayOfTheMonth(String dateOfString)
get Last Day Of The Month
Calendar calendar = Calendar.getInstance();
SimpleDateFormat yearMonthFormat = new SimpleDateFormat("yyyy-MM");
Date date = null;
try {
    date = yearMonthFormat.parse(dateOfString);
} catch (ParseException e) {
    e.printStackTrace();
calendar.setTime(date);
final int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
return lastDay;
StringgetLastDayOfXMonth(Date date, String xMonth)
date + x month and get the last day of the month
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DATE, 1);
cal.add(Calendar.MONTH, Integer.parseInt(xMonth) + 1);
cal.add(Calendar.DATE, -1);
return new SimpleDateFormat("yyyyMMdd").format(cal.getTime());
DategetToDate(boolean invoiceSearchFromFirstDayOfMonth)
get To Date
if (invoiceSearchFromFirstDayOfMonth) {
    Calendar calendar = Calendar.getInstance();
    int month = calendar.get(Calendar.MONTH);
    calendar.set(Calendar.MONTH, month - 1);
    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    return toMidnight(calendar.getTime());
} else {
    return new Date();
...
intgetWeekOfFirstDayOfMonth(Date date)
get Week Of First Day Of Month
return getWeek(getFirstDayOfMonth(date));
intgetWeekOfLastDayOfMonth(Date date)
get Week Of Last Day Of Month
return getWeek(getLastDayOfMonth(date));
booleanisGivenDayOfMonth(java.util.Date date, int dayOfMonth)
isGivenDayOfMonth: returns whether the given date is the given day of month.
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date);
return (c.get(java.util.Calendar.DAY_OF_MONTH) == dayOfMonth);
booleanisSameDayOfMonth(Date dt1, Date dt2)
Does it have the same day of month
if (dt1 == null || dt2 == null)
    return false;
GregorianCalendar gc1 = new GregorianCalendar();
gc1.setTime(dt1);
GregorianCalendar gc2 = new GregorianCalendar();
gc2.setTime(dt2);
return (gc1.get(Calendar.DAY_OF_MONTH) == gc2.get(Calendar.DAY_OF_MONTH));
DatenewDate(int year, int month, int dayOfMonth)
new Date
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
return calendar.getTime();