Description
Dates Extracts the month of the given Date
starting at 1 (January=1, February=2, ...).
License
Apache License
Parameter
Parameter | Description |
---|
date | the date to extract the month from - must not be null. |
Exception
Parameter | Description |
---|
NullPointerException | if given Date is null |
Return
the month of the given Date
starting at 1 (January=1, February=2, ...)
Declaration
public static int monthOf(Date date)
Method Source Code
//package com.java2s;
/**/* ww w .j a va2s . c o m*/
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2016 the original author or authors.
*/
import java.util.Calendar;
import java.util.Date;
public class Main {
/**
* Dates Extracts the month of the given Date <b>starting at 1</b> (January=1, February=2, ...).
*
* @param date the date to extract the month from - must not be null.
* @return the month of the given Date <b>starting at 1</b> (January=1, February=2, ...)
* @throws NullPointerException if given Date is null
*/
public static int monthOf(Date date) {
return toCalendar(date).get(Calendar.MONTH) + 1; // based 1 month (January=1)
}
/**
* Converts the given Date to Calendar, returns null if the given Date is null.
*
* @param date the date to convert to a Calendar.
* @return the Calendar corresponding to the given Date or null if the given Date is null.
*/
public static Calendar toCalendar(Date date) {
if (date == null) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar;
}
}
Related
- getNextMonthsByStartDate(Date date, int month)
- month(Date date)
- month(Date inDate, TimeZone timeZone)
- month(final Date date)
- monthBegin(final Date date)