Java Day End getEndOfYear(Date dt)

Here you can find the source of getEndOfYear(Date dt)

Description

Return the End of year (last day of month & year) Hour, minutes, seconds are set to 23:59:59

License

Open Source License

Declaration

public static Date getEndOfYear(Date dt) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /** Return the End of year (last day of month & year) 
     *  Hour, minutes, seconds are set to 23:59:59
     * @return//from  w  w w .j  a v a2s .  c o m
     */
    public static Date getEndOfYear(Date dt) {
        if (dt == null)
            return null;
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(getStartOfYear(dt));
        // Last month
        gc.set(Calendar.MONTH, 11);
        gc.setTime(getEndOfMonth(gc.getTime()));
        return gc.getTime();
    }

    /**
     * Return the first day of the year from the Date given. Hour,
     * minutes, seconds are set to 0:00:00
     * 
     * @return
     */
    public static Date getStartOfYear(Date dt) {
        if (dt == null)
            return null;
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(dt);
        gc.set(Calendar.MONTH, 0);
        gc.setTime(getStartOfMonth(gc.getTime()));

        return gc.getTime();
    }

    /** Return the End (last day of year) of the given Date
     *  Hour, minutes, seconds are set to 23:59:59
     * @return
     */
    public static Date getEndOfMonth(Date dt) {
        if (dt == null)
            return null;
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(getStartOfMonth(dt));
        // Add a month
        gc.add(Calendar.MONTH, 1);
        // Take a day for last of of month
        gc.add(Calendar.DATE, -1);
        gc.set(Calendar.HOUR_OF_DAY, 23);
        gc.set(Calendar.MINUTE, 59);
        gc.set(Calendar.SECOND, 59);
        gc.set(Calendar.MILLISECOND, 999);
        return gc.getTime();
    }

    /**
     * Return the first day of the month from the Date given Hour, minutes,
     * seconds are set to 0:00:00
     * 
     * @return
     */
    public static Date getStartOfMonth(Date dt) {
        if (dt == null)
            return null;
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(dt);
        gc.set(Calendar.DAY_OF_MONTH, 1);
        gc.set(Calendar.HOUR_OF_DAY, 0);
        gc.set(Calendar.MINUTE, 0);
        gc.set(Calendar.SECOND, 0);
        gc.set(Calendar.MILLISECOND, 0);
        return gc.getTime();
    }
}

Related

  1. getEndOfDay(Date when)
  2. getEndOfDay(final Date date, final TimeZone timeZone)
  3. getEndOfDay(Long date)
  4. getEndOfHour(Date day)
  5. getEndOfTheDate(Date date)
  6. getEndQuaterly(Date startDate)
  7. getEndSixMonthly(Date startDate)
  8. getEndTimeOfDay(Date date)
  9. getEndTimeOfDayOrNow(Date date)