return a date with stripped time - Java java.util

Java examples for java.util:Time

Description

return a date with stripped time

Demo Code


//package com.java2s;

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

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

    /**
     * return a date with stripped time
     * 
     * @return java.util.Date
     */
    public static Date getToday() {
        return stripTimeInfo(getNow());
    }

    public static Date stripTimeInfo(Date datetime) {
        if (datetime == null) {
            return null;
        }

        Calendar calendar = getCalendar();
        calendar.setTime(datetime);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * returns a date with time
     * 
     * @return java.util.Date
     */
    public static Date getNow() {
        return new Date(getCurrentSystemTime());
    }

    public static Calendar getCalendar() {
        return GregorianCalendar.getInstance();
    }

    public static long getCurrentSystemTime() {
        return System.currentTimeMillis();
    }
}

Related Tutorials