Sagewire Logo

Calendar Problem

3 Message(s) by 3 Author(s) originally posted in java developer


From: Me Date:   Wednesday, September 01, 2004
How do I get the actual Calendar time out of a Calendar object ?

I am trying to save UTC time to a table column. However the Calendar
object always returns the current time, not the UTC time from its
utility method s. The get() method does return the actual Calendar time.

----------------
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
String actual = "";
actual += cal.get(Calendar.YEAR );
actual += "-";
actual += (cal.get(Calendar.MONTH ) + 1);
actual += "-";
actual += cal.get(Calendar.DAY_OF_MONTH );
actual += " ";
actual += cal.get(Calendar.HOUR_OF_DAY );
actual += ":";
actual += cal.get(Calendar.MINUTE );
actual += ":";
actual += cal.get(Calendar.SECOND );
actual += ".";
actual += cal.get(Calendar.MILLISECOND );
JAVA.sql.Timestamp ts = new JAVA.sql.Timestamp(cal.getTimeInMillis());
System.out.println( actual ); // this is right
System.out.println( ts.toString() ); // this is wrong
------------------

The above will alwys print the UTC time, then the current time, yet they
should be the same.


From: stevebosman Date:   Thursday, September 09, 2004
wrote in message
How do I get the actual Calendar time out of a Calendar object?
I am trying to save UTC time to a table column. However the Calendar
object always returns the current time, not the UTC time from its
utility methods. The get() method does return the actual Calendar time.
----------------
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
String actual = "";
actual += cal.get(Calendar.YEAR );
actual += "-";
actual += (cal.get(Calendar.MONTH ) + 1);
actual += "-";
actual += cal.get(Calendar.DAY_OF_MONTH );
actual += " ";
actual += cal.get(Calendar.HOUR_OF_DAY );
actual += ":";
actual += cal.get(Calendar.MINUTE );
actual += ":";
actual += cal.get(Calendar.SECOND );
actual += ".";
actual += cal.get(Calendar.MILLISECOND );
JAVA.sql.Timestamp ts = new JAVA.sql.Timestamp(cal.getTimeInMillis());
System.out.println( actual ); // this is right
System.out.println( ts.toString() ); // this is wrong
------------------
The above will alwys print the UTC time, then the current time, yet they
should be the same.



No, they should not be the same. How does the Timestamp object know you
want a string formatted in UTC? In fact internally Timestamp uses
TimeZone.getDefault() to determine the timezone it'll print for. You
should _never_ rely on toString() to give you anything useful. Try the
following:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println( df.format(ts));

HTH
Steve


From: Wojtek Bok Date:   Wednesday, September 22, 2004
wrote in message:
wrote in message
How do I get the actual Calendar time out of a Calendar object?

I am trying to save UTC time to a table column. However the Calendar
object always returns the current time, not the UTC time from its
utility methods. The get() method does return the actual Calendar time.

----------------
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
String actual = "";
actual += cal.get(Calendar.YEAR );
actual += "-";
actual += (cal.get(Calendar.MONTH ) + 1);
actual += "-";
actual += cal.get(Calendar.DAY_OF_MONTH );
actual += " ";
actual += cal.get(Calendar.HOUR_OF_DAY );
actual += ":";
actual += cal.get(Calendar.MINUTE );
actual += ":";
actual += cal.get(Calendar.SECOND );
actual += ".";
actual += cal.get(Calendar.MILLISECOND );
JAVA.sql.Timestamp ts = new JAVA.sql.Timestamp(cal.getTimeInMillis());
System.out.println( actual ); // this is right
System.out.println( ts.toString() ); // this is wrong
------------------

The above will alwys print the UTC time, then the current time, yet they
should be the same.
No, they should not be the same. How does the Timestamp object know you
want a string formatted in UTC? In fact internally Timestamp uses
TimeZone.getDefault() to determine the timezone it'll print for. You
should _never_ rely on toString() to give you anything useful. Try the
following:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println( df.format(ts));



It isn't the format. I could care less about the format. The problem is
that obtaining the date from Calendar, either via toString() or getting
a Date object, always gets the current machine date, rather than the
stored date. the example shows that retrieving the values one by one
gets the right values, whereas the convienience methods don't. All from
the same object.

I assumed that because I set the Timezone in the Calendar object, that I
would ALWAYS get that value from it. This isn't the case.

You only get the right values by retrieving them individually.



Next Message: Junit and JAVA Servlets.


Blogs related to Calendar Problem

Domino Administration Resource Guide
Name changes, out of office agents, mail and calendar delegation, ... 6 - Domino 6 helps you solve the problem of applying and managing standard corporate ... Beginning in Notes/Domino 6, two Java-based applications became available for ...

5 challenges of web application scanning
Others rely on JavaScript (or broken JS), Java Applets, a specific web browser because ... We like to refer to this issue as the "calendar problem", as this was the spot we ... This is a similar problem to the infinite web site issue. ...

Google is not the leader in Ajax applications
Calendar: I like Google calendar, 30 Boxes along with Kiko. ... So my vote is for GMail, but I have to admit that at the moment it has lot of problems. ... with Ajax can be done with Java(if you think initial loading of the Applet and ...

General Computer Problems :: RE: referred to this forum, problems ...
MenuText = Sun Java Console : C:\Program Files\Java\jre1.5.0_06\bin\npjpi150_06. dll [HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Explorer ... {6A205B57-2567-4A2C-B881-F787FAB579A3} Microsoft DocProp Inplace Calendar Control ...

Understanding Java Calendar Object
This is the first of a series of posts I will dedicate to Calendar object in Java and how ... Java.util package contains 4 temporal classes: Calendar, Date, Timezone and ... The problem lies in how time is represented by a Date object. ...

www : feature requests
Is your request really a feature request, or is it a problem? ... For now it is working with the java class MistyBeachPGNViewer.class, thanks do Dom! ... These would be: A simple blog template, A calendar template and some kind of ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional