Subtract 6 hours from an existing Date object java (midnight corner case)

前端 未结 3 1004
慢半拍i
慢半拍i 2021-01-29 16:38

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

            Date birthDate = (...say Apr 20th 0300hrs)
            Calendar ca         


        
相关标签:
3条回答
  • 2021-01-29 17:13

    Second parameter in Calendar's .add method is indeed the delta, or the change, so yes, this would subtract 6 hours.

    read more here: enter link description here

    0 讨论(0)
  • 2021-01-29 17:26

    Yes.

    Reference for calendar.add

    http://www.tutorialspoint.com/java/util/calendar_add.htm

    0 讨论(0)
  • 2021-01-29 17:33

    No.

    After running the following code:

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

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

    0 讨论(0)
提交回复
热议问题