Remove the time part from a Date object. - Java java.util

Java examples for java.util:Time

Description

Remove the time part from a Date object.

Demo Code


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

public class Main {
    public static void main(String[] argv) throws Exception {
        Date pDate = new Date();
        System.out.println(removeTimePart(pDate));
    }//www.  j  a  v  a 2s.c  om

    /** 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