get the end of the day having been given a date - Java java.util

Java examples for java.util:Day

Description

get the end of the day having been given a date

Demo Code


//package com.java2s;
import java.text.SimpleDateFormat;

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

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

    /**
     *get the  end  of the day having been given  a date
     * 
     * @param date
     * @return the Date(the end of the day)
     */
    public static String getEndOfDay(Date date) {
        String pattern = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DATE);
        cal.set(year, month, day, 23, 59);
        return simpleDateFormat.format(cal.getTime());
    }
}

Related Tutorials