Java Date Start getStartOfDay(Date date)

Here you can find the source of getStartOfDay(Date date)

Description

Set the time component as the start of the day.

License

Open Source License

Parameter

Parameter Description
date The Date to get the start of the day

Return

The date with the time component reset to 00:00:00.

Declaration

public static Date getStartOfDay(Date date) 

Method Source Code

//package com.java2s;
/*//from  w w w. j a  va2 s.co  m
 * CONFIDENTIAL AND PROPRIETARY SOURCE CODE OF
 * Institute of Systems Science, National University of Singapore
 *
 * Copyright 2012 Team 4(Part-Time), ISS, NUS, Singapore. All rights reserved.
 * Use of this source code is subjected to the terms of the applicable license
 * agreement.
 *
 * -----------------------------------------------------------------
 * REVISION HISTORY
 * -----------------------------------------------------------------
 * DATE             AUTHOR          REVISION      DESCRIPTION
 * 10 March 2012    Chen Changfeng   0.1            Class creating
 *                                        
 *                                        
 *                                        
 *                                        
 * 
 */

import java.util.*;

public class Main {
    private static String timezone = "Asia/Singapore";

    /**
     * Set the time component as the start of the day.
     * <p>
     * The Time Component of the date returned will be set to
     * 00:00:00.
     * <p>
     * @param date The Date to get the start of the day
     * @return The date with the time component reset to 00:00:00.
     */
    public static Date getStartOfDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        // Clear the time component
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);

        Date start = getDate(year, month, day);
        return start;
    }

    /**
     * Retrieves the value of the field in the Date.
     * Some common fields that is likely to be used include :
     * <p>
     * <li>Calendar.YEAR - retrieves the year value
     * <li>Calendar.MONTH - retrieves the month value ( 1 - 12 )
     * <li>Calendar.DAY_OF_MONTH - retrieve the day value ( 1 - 31 )
     * <li>Calendar.HOUR - retrieves the hours value in 12 hour format ( 1 - 12 )
     * <li>Calendar.HOUR_OF_DAY - retrieves the hours value in 24 hour format ( 0 - 23 )
     * <li>Calendar.MINUTE - retrieves the minutes value ( 0 - 59 )
     * <li>Calendar.AM_PM - retrieves the am/pm value ( 0 = am; 1 = pm )
     * <p>
     * @param  date  The Date object to extract value from.
     * @param  field A Calendar constant to retrieve the field value from the Date
     * object.
     * @return The value of the field that is requested.
     * @throws ArrayIndexOutOfBoundsException - if specified field is out of
     * range (<code>field</code> &lt; 0 || <code>field</code> &gt;= <code>Calendar.FIELD_COUNT</code>).
     * @see java.util.Calendar
     */
    public static int get(Date date, int field) {

        Calendar cal = Calendar.getInstance();

        cal.setTime(date);

        int value = cal.get(field);

        // Add 1 if the field is Calendar.MONTH since Calendar returns
        // the month value starting from 0.
        if (Calendar.MONTH == field)
            value += 1;

        // If it is 12 am/pm, the value will be 0. Need to change it to 12 for ease of display.
        if (Calendar.HOUR == field && value == 0)
            value = 12;

        return value;
    }

    /**
     * Returns the date instance based on the current system date
     * <p>
     * @return Current System date.
     */
    public static Date getDate() {
        TimeZone tz = TimeZone.getTimeZone(timezone);
        return Calendar.getInstance(tz).getTime();
    }

    public static Date getDate(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();

        // Clear all fields first
        cal.clear();

        cal.set(year, month - 1, day);

        return cal.getTime();
    }
}

Related

  1. getStartDateOfCurrMonth()
  2. getStartDay(Date date)
  3. getStartDayOfNextMonth(Date date)
  4. getStartOfDate(final Date date)
  5. getStartOfDay(Date date)
  6. getStartOfDay(Date day)
  7. getStartOfDay(Date day)
  8. getStartOfMonth(Date date)
  9. getStartOfMonth(Date dt)