Java examples for java.util:Day
get the beginning of the day having been given a date
//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(getStartOfDay(date)); }/*from w ww . j a va2 s . c o m*/ /** * get the beginning of the day having been given a date * * @param date * @return the date(the beginning of the date) */ public static String getStartOfDay(Date date) { String pattern = "yyyy-MM-dd HH:mm:ss"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); Calendar calendar = Calendar.getInstance(); 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, 0, 0); return simpleDateFormat.format(cal.getTime()); } }