码迷,mamicode.com
首页 > 编程语言 > 详细

java怎么获取一天的开始时间和结束时间

时间:2015-08-28 12:28:14      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

	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

java怎么获取一天的开始时间和结束时间

标签:

原文地址:http://www.cnblogs.com/vaer/p/4766030.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!