year Floor - Java java.util

Java examples for java.util:Year

Description

year Floor

Demo Code


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main{
    public static void main(String[] argv) throws Exception{
        Calendar calendar = Calendar.getInstance();
        yearFloor(calendar);//from   w w w. j  a v  a 2  s .co m
    }
    public static void yearFloor(Calendar calendar) {
        monthFloor(calendar);
        calendar.set(Calendar.MONTH, 0);
    }
    public static void monthFloor(Calendar calendar) {
        dayFloor(calendar);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
    }
    public static void monthFloor(Calendar calendar, int number) { //number param for quarter and half
        dayFloor(calendar);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        if (number > 1) {
            int month = calendar.get(Calendar.MONTH);
            calendar.set(Calendar.MONTH, (month / number) * number);
        }
    }
    public static void dayFloor(Calendar calendar) {
        hourFloor(calendar);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
    }
    public static void hourFloor(Calendar calendar) {
        minuteFloor(calendar);
        calendar.set(Calendar.MINUTE, 0);
    }
    public static void hourFloor(Calendar calendar, int number) {
        minuteFloor(calendar);
        calendar.set(Calendar.MINUTE, 0);
        if (number > 1) {
            int hours = calendar.get(Calendar.HOUR);
            calendar.set(Calendar.HOUR, (hours / number) * number);
        }
    }
}

Related Tutorials