1

I am writing a program that has a header where the current time is displayed. I want to allow the user to modify date/time settings. But after the user modifications I cannot get the calendar to stay up-to-date. It always shows the value entered by the user. For example:

Calendar cal = Calendar.getInstance();
System.out.println(cal.getTime());
cal.set(Calendar.MONTH, Calendar.SEPTEMBER);
try {
    Thread.sleep(2000);
} catch (InterruptedException e) {
    e.printStackTrace();
}
System.out.println(cal.getTime());

Let's say the first output is

Mon Feb 18 11:33:07 CET 2013

What I want here is to get after 2 seconds a second output

Mon Sep 18 11:33:09 CET 2013 // Month = Sep and Seconds = 09

What I get instead is

Mon Sep 18 11:33:07 CET 2013 // Month = Sep, seconds don't change, still 07!!

If I add cal = Calendar.getInstance(); before the second output, I'll get

Mon Feb 18 11:33:09 CET 2013 // Seconds = 09, month doesn't change, still Feb!!

I think there must be an easy and obvious implementation that I cannot find.

UPDATE: I cannot use DateFormat on the embedded system I am working on.

Limbo Exile
  • 1,321
  • 2
  • 21
  • 41

4 Answers4

3

The Calendar is not updating because the time for the Calendar will be set while creating the object or when you are explicitly setting the time. In your case its set after this line.

Calendar cal = Calendar.getInstance();

If you really wants to do that, you need to find the elapsed time and increment the calendar time as below,

long startTime = System.currentTimeMillis(); // Process start time

try {
    Thread.sleep(2000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

// Calculating elapse time
long elapsedTime = System.currentTimeMillis() - startTime;
// Incrementing the calendar time based on elapsed time
cal.setTimeInMillis(cal.getTime().getTime() + elapsedTime);
Jayamohan
  • 12,734
  • 2
  • 27
  • 41
2

Calendar and Date are just snapshots. They don't update on their own after you've created them.

Whichever you chose, you'll have to create a new one every time you want to update the display. And then make your adjustments to that new snapshot.

If you want Java to change your computer's time for you when they change the month, then see this post: How can I set the System Time in Java?

Community
  • 1
  • 1
David Lavender
  • 8,021
  • 3
  • 35
  • 55
1

Instead of adding the Months and then trying to calculate the time again with the correct seconds, you should store the months as an offset. Every time you wish to get the updated time calculate the real time and then add the offset time.

You can calculate the offset time as a long and add it to the real time. However here is a simpler example -

Calendar start = Calendar.getInstance();
System.out.println(start);
//add offset
int monthsOffset = 3;
//get new time
start.add(Calendar.MONTH, monthsOffset);
System.out.println(start);
Tom
  • 15,798
  • 4
  • 37
  • 48
0

For your problem you need to use Date() not calander.

Reason: Calander works like calandar, your instance points to a purticular instance of calander (like a physical one) which doesnt change.

But where as Date() is dynamic, as shown below

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
       //get current date time with Date()
       Date date = new Date();
       System.out.println(dateFormat.format(date));

UPDATE: in response to the comment "as the problem is specific to embedded systems"

Please refer This Answer

Community
  • 1
  • 1
TheWhiteRabbit
  • 15,480
  • 4
  • 33
  • 57
  • I forgot to mention that I work on an embedded system where not all Java libraries are implemented, for example I can use Date and Calendar, but not DateFormat or SimpleDateFormat. Is there a solution that doesn't need these classes? – Limbo Exile Feb 18 '13 at 10:55