Add time to calendar

Possible duplicate:
How to subtract X days from a date using a Java calendar?

It should be fairly simple to answer, but I want to know: if, for example, you added 50 seconds to the Calendar, and the current seconds were 20, would you add 1 minute and set the seconds to 10 or set the seconds to 70? Here is some code to better show what I mean:

Calendar cal; public void test(){ cal.add(Calendar.SECOND, 50); } 

So, let's say that when this code worked, the current second was 20, and the current minute was 10. Will it go up to 11 minutes, 10 seconds or 10 minutes, 70 seconds? Thanks in advance.

+4
source share
2 answers

I have not tried it, but I expect it to go 1 minute and 10 seconds.

... now I have tried and obviously confirmed:

 public class Test { public static void main(String[] args) { Calendar cal = GregorianCalendar.getInstance(); System.out.println("Minutes : "+ cal.get(Calendar.MINUTE)); System.out.println("Seconds :" + cal.get(Calendar.SECOND)); cal.add(Calendar.SECOND, 50); System.out.println("Minutes : "+ cal.get(Calendar.MINUTE)); System.out.println("Seconds :" + cal.get(Calendar.SECOND)); } 

}

Printed on my console:

 Minutes : 11 Seconds :33 Minutes : 12 Seconds :23 
+6
source

The time you describe is only its format. When reused, the date value is stored as long . By calling the Calendar#add(type,value) method with the correct parameters, you simply add the / substrac value to the long numuber.

The value 0 of this number represents posix time, January 1, 1970.

And the value is 1349591726 , after formatting 2012-10-07 06:35:26Z

0
source

Source: https://habr.com/ru/post/1438357/


All Articles