Tuesday, August 17, 2010

Date and Calendar

Courtesy of Sun Web Learning Center


Given the following valid code fragment from classes in the java.util package:

11.  Date d = new Date(1700000000000L);
12.  System.out.println(d.toString());     // output line 1
13.  Calendar c1 = Calendar.getInstance();
14.  c1.setTime(d);
15.  Calendar c2 = Calendar.getInstance();
16.  c2.setTime(d);
17.  c1.add(Calendar.MONTH, 10);
18.  c2.roll(Calendar.MONTH, 10);
19.  System.out.println(c1.getTime().toString());
20.  System.out.println(c2.getTime().toString());

If the first line of output is Tue Nov 14 15:13:20 MST 2023, what is the result?


A

Thu Sep 14 15:13:20 MDT 2023
Thu Sep 14 15:13:20 MDT 2023

B

Thu Sep 14 15:13:20 MDT 2023
Sat Sep 14 15:13:20 MDT 2024

C

Sat Sep 14 15:13:20 MDT 2024
Thu Sep 14 15:13:20 MDT 2023

D

Sat Sep 14 15:13:20 MDT 2024
Sat Sep 14 15:13:20 MDT 2024


C is correct. The add() method adds time increments and increments days, and months and years at the same time. The roll() method adds time increments without incrementing the larger time elements.

No comments:

Post a Comment