Return list of months, based on given month and number of trace back. - Java java.time

Java examples for java.time:Month

Description

Return list of months, based on given month and number of trace back.

Demo Code


//package com.java2s;

import java.time.Month;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class Main {
    /**/*from   w w  w .  j a  v a  2s .c  om*/
     * Return list of months, based on given month and number of trace back.
     * @param date
     * @param traceBack
     */
    public static List<Month> getMonths(Date date, int traceBack) {
        int bulan = getMonthInt(date);
        int start = bulan - traceBack;

        List<Month> months = new ArrayList<>();
        for (int i = (start + 1); i <= bulan; i++)
            months.add(getMonth(i));

        return months;
    }

    /**
     * Return month in {@code int} representation from {@code Calendar}.<br />
     * @param calendar
     * @return month in {@code int} representation
     */
    public static int getMonthInt(Calendar calendar) {
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * Return month in {@code int} representation from {@code String}.
     * @param month
     * @return month in {@code int} representation
     */
    public static int getMonthInt(String month) {
        return Integer.parseInt(month);
    }

    /**
     * Return month in {@code int} representation from {@code Date}.
     * @param date
     * @return month in {@code int} representation
     */
    public static int getMonthInt(Date date) {
        return getMonthInt(getCalendar(date));
    }

    /**
     * Return month in {@code Month} representation from {@code String}.
     * @param month
     * @return month in {@code Month} representation
     */
    public static Month getMonth(String month) {
        return Month.of(getMonthInt(month));
    }

    /**
     * Return month in {@code Month} representation from {@code int}.
     * @param month
     * @return month in {@code Month} representation
     */
    public static Month getMonth(int month) {
        if (month < 1)
            month += 12;

        if (month > 12)
            month -= 12;

        return Month.of(month);
    }

    /**
     * Return month in {@code Month} representation from {@code Calendar}.
     * @param calendar
     * @return month in {@code Month} representation
     */
    public static Month getMonth(Calendar calendar) {
        return Month.of(getMonthInt(calendar));
    }

    /**
     * Return month in {@code Month} representation from {@code Date}.
     * @param date
     * @return month in {@code Month} representation
     */
    public static Month getMonth(Date date) {
        return Month.of(getMonthInt(date));
    }

    /**
     * Create default calendar.<br />
     * @return default calendar
     */
    public static Calendar getCalendar() {
        return Calendar.getInstance();
    }

    /**
     * Create calendar using date.<br />
     * @param date
     * @return calendar
     */
    public static Calendar getCalendar(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        return cal;
    }
}

Related Tutorials