Java Date to Minute getMinuteOfHour(final Date date)

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

Description

Get the minute within the hour by given date.
E.g.: at 10:04:15.250 PM the MINUTE is 4.

License

Apache License

Parameter

Parameter Description
date date to be handled.

Return

minute within the hour by given date.

Declaration

public static int getMinuteOfHour(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 w  w. j ava2  s  .c o m*/
     * Get the minute within the hour by given date.<br>
     * E.g.: at 10:04:15.250 PM the MINUTE is 4.
     * 
     * @param date date to be handled.
     * @return minute within the hour by given date.
     */
    public static int getMinuteOfHour(final Date date) {

        return getNumberOfGranularity(Calendar.MINUTE, 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. getMinute(Date d)
  2. getMinute(Date d)
  3. getMinute(Date date)
  4. getMinute(Date date)
  5. getMinute(java.util.Date date)
  6. getMinutes(Date d)
  7. getMinutes(Date time)
  8. getMinutes(final Date date)
  9. getMinutesInDay(Date d)