java怎么获取一天的开始时间和结束时间
public static void main(String[] args) { Calendar c1 = new GregorianCalendar(); c1.set(Calendar.HOUR_OF_DAY, 0); c1.set(Calendar.MINUTE, 0); c1.set(Calendar.SECOND, 0); System.out.println(c1.getTime().toLocaleString()); Calendar c2 = new GregorianCalendar(); c2.set(Calendar.HOUR_OF_DAY, 23); c2.set(Calendar.MINUTE, 59); c2.set(Calendar.SECOND, 59); System.out.println(c2.getTime().toLocaleString()); }
稍微修改下
/** * @param args */ public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); Date start = calendar.getTime(); calendar.add(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.SECOND, -1); Date end = calendar.getTime(); System.out.println(start); System.out.println(end); }
或者更简单
/** * @param args * @throws ParseException */ public static void main(String[] args) throws ParseException { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); Date start = calendar.getTime(); calendar.add(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.SECOND, -1); Date end = calendar.getTime(); System.out.println(start); System.out.println(end); /* other way */ SimpleDateFormat formater = new SimpleDateFormat("yyyy/MM/dd"); SimpleDateFormat formater2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); start = formater2.parse(formater.format(new Date())+ " 00:00:00"); end = formater2.parse(formater.format(new Date())+ " 23:59:59"); System.out.println(start); System.out.println(end); }
source: http://bbs.csdn.net/topics/390562030?page=1