Return month of now in Month representation from Date . - Java java.time

Java examples for java.time:Month

Description

Return month of now in Month representation from Date .

Demo Code


//package com.java2s;

import java.time.Month;

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

public class Main {
    /**//from w ww .  j a v a 2s  . c om
     * Return month of now in {@code Month} representation from {@code Date}.
     * @return month in {@code Month} representation
     */
    public static Month getMonthNow() {
        return Month.of(getMonthIntNow());
    }

    /**
     * Return month of now in {@code int} representation from {@code Date}.
     * @return month in {@code int} representation
     */
    public static int getMonthIntNow() {
        return getMonthInt(getCalendar(getNow()));
    }

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

    /**
     * Return today in date representation. Along with hour, minute, second, and other element.<br />
     * @return today
     */
    public static Date getNow() {
        return getCalendar().getTime();
    }
}

Related Tutorials