Return month in Month representation from String . - Java java.time

Java examples for java.time:Month

Description

Return month in Month representation from String .

Demo Code


//package com.java2s;

import java.time.Month;

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**/* w  w  w . j  a va 2 s.  c o m*/
     * 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));
    }

    /**
     * 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));
    }

    /**
     * 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