Java Month Calculate truncateMonth(Date d)

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

Description

Truncate to begin of day: 0 hour 0 minute 0 second 0 millisecond

License

Apache License

Parameter

Parameter Description
d The date to be truncated

Return

the result truncated date

Declaration

public static Date truncateMonth(Date d) 

Method Source Code

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

import java.util.*;

public class Main {
    /**/*from  ww w. j a v  a2s.  c om*/
     * Truncate to begin of day: 0 hour 0 minute 0 second 0 millisecond
     * @param d The date to be truncated
     * @return the result truncated date
     */
    public static Date truncateMonth(Date d) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(d);
        truncateMonth(ca);
        return ca.getTime();
    }

    /**
     * Truncate to begin of day: 0 hour 0 minute 0 second 0 millisecond
     * @param d The date to be truncated
     * @return the result truncated dateTimeZone timeZone
     */
    public static Date truncateMonth(Date d, TimeZone timeZone) {
        Calendar ca = Calendar.getInstance(timeZone);
        ca.setTime(d);
        truncateMonth(ca);
        return ca.getTime();
    }

    /**
     * Truncate to begin of month: 0 hour 0 minute 0 second 0 millisecond
     * @param ca The calendar to be truncated
     */
    public static Calendar truncateMonth(Calendar ca) {
        ca.set(Calendar.DAY_OF_MONTH, 1);
        truncate(ca);
        return ca;
    }

    /**
     * Truncate to begin of day: 0 hour 0 minute 0 second 0 millisecond
     * @param d The date to be truncated
     * @return the result truncated date
     */
    public static Date truncate(Date d) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(d);
        truncate(ca);
        return ca.getTime();
    }

    /**
     * Truncate to begin of day: 0 hour 0 minute 0 second 0 millisecond
     * @param d The date to be truncated
     * @return the result truncated date
     */
    public static Date truncate(Date d, TimeZone zone) {
        Calendar ca = Calendar.getInstance(zone);
        ca.setTime(d);
        truncate(ca);
        return ca.getTime();
    }

    /**
     * Truncate to begin of day: 0 hour 0 minute 0 second 0 millisecond
     * @param ca The calendar to be truncated
     */
    public static Calendar truncate(Calendar ca) {
        truncateHour(ca);
        ca.set(Calendar.HOUR_OF_DAY, 0);
        return ca;
    }

    public static void truncateHour(Calendar ca) {
        ca.set(Calendar.MILLISECOND, 0);
        ca.set(Calendar.SECOND, 0);
        ca.set(Calendar.MINUTE, 0);
    }
}

Related

  1. nextMonth(Date date, int months)
  2. nextMonths(int diff)
  3. startOfMonth(Date d)
  4. startOfMonth(Date date)
  5. startOfMonthDate(Date date)