Resets the time in the specified datetime object to 23:59:59.900. - Java java.util

Java examples for java.util:Time

Description

Resets the time in the specified datetime object to 23:59:59.900.

Demo Code


//package com.java2s;
import java.util.Calendar;
import java.util.Date;

public class Main {
    public static void main(String[] argv) throws Exception {
        Date date = new Date();
        System.out.println(normalizeTimeUp(date));
    }/* w  w w  .j  a va2 s.  co m*/

    /**
     * Resets the time in the specified datetime object to 23:59:59.900.
     *
     * @param date
     *            the date to operate on
     * @return the altered datetime object
     */
    @Deprecated
    public static Date normalizeTimeUp(Date date) {
        date = normalizeTimeDown(date);
        date.setTime(date.getTime() + 86399900);

        return date;
    }

    /**
     * Resets the time in the specified datetime object to 23:59:59.900.
     *
     * @param calendar
     *            the datetime object to operate on
     * @return the altered datetime object
     */
    public static Calendar normalizeTimeUp(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 900);

        return calendar;
    }

    /**
     * Resets the time in the specified datetime object to 0:00:00.000.
     *
     * @param date
     *            the date to operate on
     * @return the altered datetime object
     */
    @Deprecated
    public static Date normalizeTimeDown(Date date) {
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
        date.setTime((date.getTime() / 1000) * 1000);

        return date;
    }

    /**
     * Resets the time in the specified datetime object to 0:00:00.000.
     *
     * @param calendar
     *            the datetime object to operate on
     * @return the altered datetime object
     */
    public static Calendar normalizeTimeDown(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar;
    }
}

Related Tutorials