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

intgetYearMonth(Date date)
get Year Month
Calendar c = Calendar.getInstance();
c.setTime(date);
return (c.get(Calendar.YEAR)) * 100 + c.get(Calendar.MONTH) + 1;
DategetYearMonth(String dateString)
get Year Month
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Date date;
try {
    date = sdf.parse(dateString);
} catch (ParseException e) {
    e.printStackTrace();
    return null;
return date;
ListgetYearMonth(String year)
get Year Month
if (null == year || "".equals(year.trim())) {
    year = yearFormat.format(new Date());
List<String> yearMonth = new ArrayList<String>();
int currentYear = Integer.parseInt(year);
int lastYear = Integer.parseInt(year) - 1;
for (int j = currentYear; j >= lastYear; j--) {
    for (int i = 12; i >= 1; i--) {
...
StringgetYearMonthDate()
get Year Month Date
return new SimpleDateFormat("yyyyMMdd").format(new Date());
StringgetYearMonthDateByMisSecond(long misSecond)
get Year Month Date By Mis Second
if (misSecond < 0) {
    throw new IllegalArgumentException("The misSecond must not be negative");
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(misSecond);
java.util.Date date = cal.getTime();
return getStrDate(date, "yyyy-MM-dd");
StringgetYearMonthDay()
get Year Month Day
CALENDAR.setTime(new Date());
int year = CALENDAR.get(Calendar.YEAR);
int month = CALENDAR.get(Calendar.MONTH) + 1;
return year + "" + month;
StringgetYearMonthDay(Date date)
get Year Month Day
SimpleDateFormat formatter = new SimpleDateFormat(FORMAT12);
String currentTime = formatter.format(date);
return currentTime;
DategetYearMonthDay(final int year, final int month, final int day)
Return normalized year month day date (desired day in month at 00:00:00.000 pm)
final Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
...
Date[]getYearMonthList(int year)
return array including 12 months of one year
Calendar cal = Calendar.getInstance();
if (year > 0)
    cal.set(Calendar.YEAR, year);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
...
ListgetYearMonths(List time)
Get year months
List<String> yms = new ArrayList<>();
SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
String ym;
for (Date t : time) {
    ym = format.format(t);
    if (!yms.contains(ym)) {
        yms.add(ym);
return yms;