Skip to main content

Can anyone please help me understand how to use the Calendar class?

2 replies [Last post]
sugar_babee
Offline
Joined: 2006-06-23
Points: 0

Im supposed to be able to use the Calendar class for a project to store an initial time and then add time to it to get a new time...I just dont understand how to initialize the time...do i use getTime() or getTimeInMillis() or what? I dont get it at all, its so confusing. Any clarifications would be greatly appreciated.

Thanks!

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
dnbphysicist
Offline
Joined: 2007-10-14
Points: 0

If all you need to do is store an initial time and add a new time to it, why not just store them as ints, and then convert them back to strings later with the formatting WX:YZ?

Message was edited by: dnbphysicist

dnbphysicist
Offline
Joined: 2007-10-14
Points: 0

Ok, I read the API a little bit: getTimeInMillis() returns the time as a long. Then you can add the other time you need as a long giving you the final result as a numerical long value.

Here is a simple program that returns the current time in milliseconds

import java.util.*;

public class Cal {

public static void main(String[] args) {
Calendar rightnow = Calendar.getInstance();
long x;

x = rightnow.getTimeInMillis();

System.out.println(x);

}

}

You could also use the setTimeInMillis(long startingtime)
to set the time also.

Message was edited by: dnbphysicist