Java Day of Month getDayOfMonth(final Date date)

Here you can find the source of getDayOfMonth(final Date date)

Description

Get the day of the month by given date.

License

Apache License

Parameter

Parameter Description
date date to be handled.

Return

the day of the month by given date.

Declaration

public static int getDayOfMonth(final Date date) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    private static final ThreadLocal<Calendar> calendarCache = new ThreadLocal<Calendar>();

    /**// w ww  .ja  v a 2s . c  o  m
     * Get the day of the month by given date. The first day of the month has
     * value 1.
     * 
     * @param date date to be handled.
     * @return the day of the month by given date.
     */
    public static int getDayOfMonth(final Date date) {

        return getNumberOfGranularity(Calendar.DAY_OF_MONTH, date);
    }

    private static int getNumberOfGranularity(final int granularity, final Date date) {

        Calendar calendar = buildCalendar(date);
        return calendar.get(granularity);
    }

    /**
     * Gets a calendar using the default time zone and locale. The Calendar
     * returned is based on the current time in the default time zone with the
     * default locale.
     * 
     * @return a Calendar object.
     */
    private static Calendar buildCalendar() {

        Calendar calendar = calendarCache.get();
        if (calendar == null) {
            calendar = GregorianCalendar.getInstance();
            calendarCache.set(calendar);
        }
        return calendar;
    }

    /**
     * Gets a calendar using the default time zone and locale. The Calendar
     * returned is based on the given time in the default time zone with the
     * default locale.
     * 
     * @return a Calendar object use given date.
     */
    private static Calendar buildCalendar(final Date date) {

        Calendar calendar = buildCalendar();
        calendar.setTime(date);
        return calendar;
    }
}

Related

  1. getDayOfMonth(Date date)
  2. getDayOfMonth(Date date)
  3. getDayOfMonth(Date date)
  4. getDayOfMonth(Date date)
  5. getDayOfMonth(Date dt)
  6. getFirstDayOfMonth(Date date)
  7. getFirstDayOfMonth(Date date)
  8. getFirstDayOfMonth(java.util.Date date)
  9. getFirstMiddleDayOfMonth(Date date)