Java Utililty Methods Year Month

List of utility methods to do Year Month

Description

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

Method

StringgetFirstDay(String yearMonthStr, int range)
get First Day
try {
    DateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    Date date = sdf.parse(yearMonthStr + "01");
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.MONTH, -(range - 1));
    Date first = calendar.getTime();
    return sdf.format(first);
...
StringgetFirstDayByMonthWeek(int year, int month, int week)
get First Day By Month Week
DateFormat df = new SimpleDateFormat("yyyy-MM-dd ");
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month - 1);
c.set(Calendar.WEEK_OF_MONTH, week);
int firstDayofweek = c.getFirstDayOfWeek();
Calendar c1 = Calendar.getInstance();
c1.set(Calendar.YEAR, year);
...
StringgetFirstDayOfMonth(int year, int month)
get First Day Of Month
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
StringgetFirstDayOfMonth(int year, int month, Integer day)
get First Day Of Month
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
if (day == null) {
    cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
} else {
    cal.set(Calendar.DAY_OF_MONTH, day);
StringBuffer sb = new StringBuffer();
String date = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
sb.append(date).append(" 00:00");
return sb.toString();
CalendargetFirstTimeOfDay(int year, int month, int day)
get First Time Of Day
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
return cal;
...
StringgetFirstYearMonth()
get First Year Month
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-01-1");
Date curDate = new Date(System.currentTimeMillis());
return formatter.format(curDate);
intgetMonthDays(Integer year, Integer month)
get Month Days
Calendar c = Calendar.getInstance();
c.set(year, month - 1, 1);
return c.getActualMaximum(Calendar.DAY_OF_MONTH);
IntegergetMonthDays(Integer year, Integer month)
get Month Days
if (year != null && year > 0 && month != null && month > 0) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, year);
    c.set(Calendar.MONTH, month);
    c.set(Calendar.DATE, 1);
    c.add(Calendar.DATE, -1);
    return c.get(Calendar.DATE);
return 0;
ListgetPerMonthByCuttentYear()
get Per Month By Cuttent Year
String year = getCurrentYear();
Calendar a = Calendar.getInstance();
int month = a.get(Calendar.MONTH) + 1;
List<String> months = new ArrayList<String>();
for (int i = month; i > 0; i--) {
    months.add(year + "-" + String.format("%02d", i));
return months;
...
MapgetQuarterToYearMonthDay(Integer year, Integer quarter)
QuarterToYearMonthDay
if (year != null && year > 0 && quarter != null && quarter > 0) {
    Map<String, String> map = new HashMap<String, String>();
    if (quarter == 1) {
        map.put("startTime", year + "-01-" + getMonthDays(year, 1) + " 00:00:00");
        map.put("endTime", year + "-03-" + getMonthDays(year, 3) + " 23:59:59");
    } else if (quarter == 2) {
        map.put("startTime", year + "-04-" + getMonthDays(year, 4) + " 00:00:00");
        map.put("endTime", year + "-06-" + getMonthDays(year, 6) + " 23:59:59");
...