Java Date Start getStartOfQuarter(Date dt)

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

Description

Return the first day of the set quarter from the Date given.

License

Open Source License

Declaration

public static Date getStartOfQuarter(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 {
    /**/*from  w ww  .  j av  a2 s  .co  m*/
     * Return the first day of the set quarter from the Date given. Hour, minutes,
     * seconds are set to 0:00:00 ie Either Jan, April, July or October
     * 
     * @return
     */
    public static Date getStartOfQuarter(Date dt) {
        if (dt == null)
            return null;
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(dt);

        int thisMonth = gc.get(Calendar.MONTH);
        switch (thisMonth) {
        case Calendar.JANUARY:
        case Calendar.FEBRUARY:
        case Calendar.MARCH:
            gc.set(Calendar.MONTH, Calendar.JANUARY);
            break;
        case Calendar.APRIL:
        case Calendar.MAY:
        case Calendar.JUNE:
            gc.set(Calendar.MONTH, Calendar.APRIL);
            break;
        case Calendar.JULY:
        case Calendar.AUGUST:
        case Calendar.SEPTEMBER:
            gc.set(Calendar.MONTH, Calendar.JULY);
            break;
        case Calendar.OCTOBER:
        case Calendar.NOVEMBER:
        case Calendar.DECEMBER:
            gc.set(Calendar.MONTH, Calendar.OCTOBER);
            break;
        default:
            break;
        }
        gc.setTime(getStartOfMonth(gc.getTime()));

        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. getStartOfDay(Date day)
  2. getStartOfDay(Date day)
  3. getStartOfMonth(Date date)
  4. getStartOfMonth(Date dt)
  5. getStartOfMonth(Date dt)
  6. getStartTimeByManDay(Date serviceStartTime)
  7. getStartTimeOfDate(Date date)
  8. getStartTimeOfDay(Date date)
  9. getStartTimeOfDay(Date date)