Creates a Date object that only contains the current date not the time. - Java java.util

Java examples for java.util:Time

Description

Creates a Date object that only contains the current date not the time.

Demo Code


//package com.java2s;
import java.util.*;

public class Main {
    public static void main(String[] argv) throws Exception {
        System.out.println(getToday());
    }//from  ww  w  .  jav  a  2s.c o  m

    /** Creates a <I>Date</I> object that only contains the current date not the time. */
    public static Date getToday() {
        return removeTimePart(new Date());
    }

    /** Remove the time part from a <I>Date</I> object.
       @param pDate <I>Date</I> object to remove the time part from.
       @return Also returns the passed in value for convenience.
     */
    public static Date removeTimePart(Date pDate) {
        Calendar pCalendar = Calendar.getInstance();

        pCalendar.setTime(pDate);

        int nYear = pCalendar.get(Calendar.YEAR);
        int nMonth = pCalendar.get(Calendar.MONTH);
        int nDay = pCalendar.get(Calendar.DAY_OF_MONTH);

        pCalendar.clear();

        pCalendar.set(nYear, nMonth, nDay);

        return pCalendar.getTime();
    }
}

Related Tutorials