码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-03-14 21:31:52      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

这是数据库中存放的是long类型的时间戳,将时间戳转换为Date类型后. 想要查询这一天中的数据:
    /**
     * @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);
    }

or:

    /**
     * @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);
    }

 

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

标签:

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

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