• 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

  • 相关阅读:
    effective C++ 条款 18:让接口容易被正确使用,不易被误用
    effective C++ 条款 24:若所有参数皆需类型转换,请为此采用nonmember函数
    PHP 归并排序
    IOS+H5页面自定义按钮无效
    vscode安装
    php 单例模式
    Warning: popen() has been disabled for security reasons in OS/Guess.php on line 241
    php 魔术方法
    php快速排序
    mysql 查询字段为空显示默认值
  • 原文地址:https://www.cnblogs.com/vaer/p/4766030.html
Copyright © 2020-2023  润新知