Java Calendar Create getCalendarAtMidnight(Date d)

Here you can find the source of getCalendarAtMidnight(Date d)

Description

Set the given date to a Calendar at midnight.

License

Apache License

Return

today's Calendar at midnight.

Declaration

public static Calendar getCalendarAtMidnight(Date d) 

Method Source Code

//package com.java2s;
/**//ww w .j av  a  2  s.c o  m
 * Utils class for the stats
 * S23Y (2015). Licensed under the Apache License, Version 2.0.
 * Author: Thibaud Ledent
 */

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

public class Main {
    /**
     * Set the given date to a Calendar at midnight.
     * For example, if we have Thursday 9th April at 11:00 PM, it will return Thursday 9th April at 00:00 AM
     * @return today's Calendar at midnight.
     */
    public static Calendar getCalendarAtMidnight(Date d) {
        Calendar cal = date2Calendar(d);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        return cal;
    }

    public static Calendar date2Calendar(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal;
    }
}

Related

  1. getCalendar(String gdate)
  2. getCalendar(String str)
  3. getCalendar(String time, char sep, boolean isStart)
  4. getCalendar(String yyyy, String mm, String dd)
  5. getCalendar(TimeZone timeZone, Locale locale)
  6. getCalendarBefore(int i)
  7. getCalendarByName(String timeZone)
  8. getCalendarBySpecific(int year, int month, int date, int hour)
  9. getCalendarByValue(String timeZone)