5

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

This should be fairly simple to answer, but I want to know: if you, for example, added 50 seconds to a Calendar, and the current seconds were 20, would it add 1 minute and set the seconds to 10, or set seconds to 70? Here's a bit of code to better demonstrate what I mean:

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

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

Community
  • 1
  • 1
Theway2cool1
  • 85
  • 2
  • 2
  • 6

2 Answers2

8

I neither try it, but I expect it will pass to 1 minute and 10 seconds.

...now i tried and obviously is 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 to my console :

Minutes : 11
Seconds :33
Minutes : 12
Seconds :23
obe6
  • 1,743
  • 15
  • 23
  • I just have hope thay you underand why it doing that way. – Damian Leszczyński - Vash Oct 08 '12 at 07:26
  • Sure because otherwise it will break the contract of add/get methods of Calendar class, your post is true, but I think these are just 'internals', how Calendar class store its data should not affect/interest to the user of the class. – obe6 Oct 08 '12 at 07:32
  • I do not agree with the last sentence. That user should not be interests in the logic behind some class. If you would know how time is represent in computers, you would not have to write a poor example that only show result but not the reason why is happening so. If you do not know the basics how you can be a good developer ? – Damian Leszczyński - Vash Oct 08 '12 at 07:51
  • Read better my sentence.I did not say what you wrote. – obe6 Oct 08 '12 at 07:56
0

The time that you are describing is only the format of it. In reallity the value of date is stored as long number. By invoking Calendar#add(type,value) method with proper parameters you just add/substrac value to that long numuber.

The 0 value of that number represent the posix time, 1 January 1970

And the value 1349591726, represent after formating 2012-10-07 06:35:26Z

  • Calendar is not about format, that is DateFormat. – obe6 Oct 08 '12 at 07:43
  • @obe6, You probably miss understoot the answer. – Damian Leszczyński - Vash Oct 08 '12 at 07:53
  • no...I understood that your answer is wrong on two fronts : 1) Calendar is not about format 2) The client (of a class) should not be interested/affected of internal rapresentation of private class data (oo encapsulation?) but must simply respect the contract of the class is using – obe6 Oct 08 '12 at 08:12
  • You are missing the point. If OP wonder about result will be 11:20 or 10:70 is about format. Not about date at all. Calendar is only example of class for date. The contract you are writing have no appliance here as the answer is more about how date is generally stored in computers not only by that class. That is why is wrote that you not understood the answer. The contract that must be repected is how that class handle the date modification and when you add 20 seconds it is not possible to have 70 as such value do not exists in date world for seconds. – Damian Leszczyński - Vash Oct 08 '12 at 08:45