Java Today today()

Here you can find the source of today()

Description

Returns a date with the current day (no time).

License

Mozilla Public License

Return

Current date.

Declaration

public static Date today() 

Method Source Code

//package com.java2s;
/**/*from w w  w . j  a v a2 s .c om*/
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
 * If a copy of the MPL was not distributed with this file, You can obtain one at
 * http://mozilla.org/MPL/2.0/.
 * 
 * This Source Code Form is also subject to the terms of the Health-Related Additional
 * Disclaimer of Warranty and Limitation of Liability available at
 * http://www.carewebframework.org/licensing/disclaimer.
 */

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

public class Main {
    /**
     * Returns a date with the current day (no time).
     * 
     * @return Current date.
     */
    public static Date today() {
        return stripTime(now());
    }

    /**
     * Strips the time component from a date.
     * 
     * @param date Original date.
     * @return Date without the time component.
     */
    public static Date stripTime(Date date) {
        return addDays(date, 0, true);
    }

    /**
     * Returns a date with the current time.
     * 
     * @return Current date and time.
     */
    public static Date now() {
        return new Date();
    }

    /**
     * Adds specified number of days to date and, optionally strips the time component.
     * 
     * @param date Date value to process.
     * @param daysOffset # of days to add.
     * @param stripTime If true, strip the time component.
     * @return Input value the specified operations applied.
     */
    public static Date addDays(Date date, int daysOffset, boolean stripTime) {
        if (date == null) {
            return null;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setLenient(false); // Make sure the calendar will not perform
        // automatic correction.
        calendar.setTime(date); // Set the time of the calendar to the given
        // date.

        if (stripTime) { // Remove the hours, minutes, seconds and milliseconds.
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
        }

        calendar.add(Calendar.DAY_OF_MONTH, daysOffset);
        return calendar.getTime();

    }
}

Related

  1. today()
  2. today()
  3. today()
  4. today()
  5. today()
  6. today()
  7. today()
  8. today()
  9. today()