从现有的Date对象java(午夜角情况)减去6小时 [英] Subtract 6 hours from an existing Date object java (midnight corner case)

查看:102
本文介绍了从现有的Date对象java(午夜角情况)减去6小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用日历 api。我主要担心的是

I'll be using the Calendar api for this. My primary concern is that

            Date birthDate = (...say Apr 20th 0300hrs)
            Calendar cal = Calendar.getInstance();
            cal.setTime(birthDate);
            cal.add(Calendar.HOUR, -6);
            Date newDate = cal.getTime();

newDate 将于2009年4月19日(9月9日) PM)?

Will newDate be Apr 19th 2100hrs (9 PM) ?

推荐答案

否。

运行以下代码后:

Date birthDate = (...say Apr 20th 0300hrs)
Calendar cal = Calendar.getInstance();
cal.setTime(birthDate);
cal.add(Calendar.HOUR, -6);

你可以保证在4月20日0300hrs之前将cal设置为6小时,但不是设置为'2009年4月19日'21。'

you are guaranteed that cal is set to six hours before 'Apr 20th 0300hrs', but not that it is set to 'Apr 19th 2100hrs'.

这篇关于从现有的Date对象java(午夜角情况)减去6小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆