Java Calendar Truncate truncate(Calendar calendar)

Here you can find the source of truncate(Calendar calendar)

Description

Truncate the Calendar removing hours, minutes, seconds and milliseconds

License

Apache License

Parameter

Parameter Description
calendar The Calendar to truncate

Declaration

public static void truncate(Calendar calendar) 

Method Source Code

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

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

public class Main {
    /**/*from   w  w w  .  j a  v  a 2 s  .co  m*/
     * Truncate the date removing hours, minutes, seconds and milliseconds
     * 
     * @param date The {@link Date} to truncate
     * @return The truncated {@link Date}
     */
    public static Date truncate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        truncate(calendar);
        return calendar.getTime();
    }

    /**
     * Truncate the {@link Calendar} removing hours, minutes, seconds and milliseconds
     * 
     * @param calendar The {@link Calendar} to truncate
     */
    public static void truncate(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }

    public static Date getTime(int hour, int minutes) {
        return getTime(hour, minutes, true);
    }

    public static Date getTime(int hour, int minutes, Boolean is24Hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(is24Hour ? Calendar.HOUR_OF_DAY : Calendar.HOUR, hour);
        calendar.set(Calendar.MINUTE, minutes);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }
}

Related

  1. trim(final Calendar cal)
  2. trimCalendar(Calendar cal)
  3. trunc(Calendar calendar)
  4. truncateCal(final Calendar cal)
  5. truncCalendar(Calendar c)
  6. truncCalendarToQuarter(Calendar c)