Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > What a mess: Date, milliseconds, GregorianCalendar

Reply
Thread Tools

What a mess: Date, milliseconds, GregorianCalendar

 
 
Erwin Moller
Guest
Posts: n/a
 
      08-19-2004
Hi group,

(Sorry for complaining)
Is it just me or do dates, milliseconds, GregorianCalendar completely
confusing?

I needed to build a simple JSP that displays a simple countdown for an
election.
It should show how many days untill elections start.
During elections it should show how many days are left to vote, and when the
election closingdate is over it should say: elections are passed.

This took me 4 hours and a lot of frustration. the API wasn't really a help
either.
I came up with the following huge script. (at the end)
I code such things in PHP on 3 lines...

I was just wondering: Am I missing something completely or is working with
dates really confusing?
(Like the double getTime().getTime() constructions.)

What is your experience?

Thanks for your time.
And again, sorry for the ranting.
I love working with Java, but the Date-related logic just isn't compatible
with my brains.

Regards,
Erwin Moller


-------------------


<%@ page import="java.util.Date" %>
<%@ page import="java.util.Calendar" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.Calendar" %>
<%@ page import="java.util.GregorianCalendar" %>
<%
// this file represents a countdontcounter

// We have 2 dates:
// startElectionDate = startdate of elections
// endElectionDate = enddate of elections

// format them: (yyyy,mm,dd)
// warning, month is 0 based! 0 = januari, 1 = februari
GregorianCalendar startElectionGregorianCalendar = new
GregorianCalendar(2004, 7 , 2);
GregorianCalendar endElectionGregorianCalendar = new
GregorianCalendar(2004, 8, 1);

GregorianCalendar nowGregorianCalendar = new GregorianCalendar();

long mssecInADay = (1000*60*60*24);

// if <0 already passed
double dDaysToStartDate = (double)
(startElectionGregorianCalendar.getTime().getTime( ) -
nowGregorianCalendar.getTime().getTime()) / mssecInADay;

// if <0 already passed
double dDaysToEndDate = (double)
(endElectionGregorianCalendar.getTime().getTime() -
nowGregorianCalendar.getTime().getTime()) / mssecInADay;

String strDisplay = "";
if (dDaysToStartDate >0){
strDisplay = ""+dDaysToStartDate+" days untill elections.";
} else {
if (dDaysToEndDate >0) {
strDisplay = "Elections running. you have "+dDaysToEndDate+" days
to vote left.";
} else {
strDisplay = "Voting is over.";
}
}
%>
<!-- Start countdown -->
<%= strDisplay %>
<!-- End countdown -->

 
Reply With Quote
 
 
 
 
Jacob
Guest
Posts: n/a
 
      08-19-2004
Erwin Moller wrote:

> (Sorry for complaining)
> Is it just me or do dates, milliseconds, GregorianCalendar completely
> confusing?


You're allowed to complain!

It's not a mess as it contains a lot of complex logic to handle
the complicated world of calendars, days and time, but the API
is not suitable for the many simple cases like yours.

The main problem (I think) is that dates and times are mixed into
the same class: java.util.Date. It is difficult to work with *dates*
without getting into all kind of time issues.

You can look at http://geosoft.no/software/day/Day.java.html
where only the *day* part of a calendar is exposed, and a set of
simple date arithmetic methods like you requests has been included.

 
Reply With Quote
 
 
 
 
Paul Lutus
Guest
Posts: n/a
 
      08-19-2004
Erwin Moller wrote:

> Hi group,
>
> (Sorry for complaining)
> Is it just me or do dates, milliseconds, GregorianCalendar completely
> confusing?


It is calendars that are confusing. The classes that deal with this topic
merely reflect the true complexity of human calendars.

>
> I needed to build a simple JSP that displays a simple countdown for an
> election.
> It should show how many days untill elections start.
> During elections it should show how many days are left to vote, and when
> the election closingdate is over it should say: elections are passed.
>
> This took me 4 hours and a lot of frustration. the API wasn't really a
> help either.
> I came up with the following huge script. (at the end)
> I code such things in PHP on 3 lines...
>
> I was just wondering: Am I missing something completely or is working with
> dates really confusing?


Yes.

> (Like the double getTime().getTime() constructions.)
>
> What is your experience?


Here's my experience (not really that different from your own):

// CalendarCountdown.java

import java.util.GregorianCalendar;

public class CalendarCountdown {

public static void main(String args[])
{
final long millisToDays = 86400000;
GregorianCalendar today = new GregorianCalendar();
// remember: months are numbered 0 - 11 in this class
// GregorianCalendar(year, month-1, day of month)
GregorianCalendar electionDay = new GregorianCalendar(2004,10,15);
long days = (electionDay.getTimeInMillis() - today.getTimeInMillis())
/ millisToDays;
String s = (days > 0)?"It is " + days + " days until election day."
days == 0)?"Election day is today."
:"Election day has passed.";
System.out.println(s);

}
}

--
Paul Lutus
http://www.arachnoid.com

 
Reply With Quote
 
Paul Lutus
Guest
Posts: n/a
 
      08-19-2004
Jacob wrote:

> Erwin Moller wrote:
>
>> (Sorry for complaining)
>> Is it just me or do dates, milliseconds, GregorianCalendar completely
>> confusing?

>
> You're allowed to complain!
>
> It's not a mess as it contains a lot of complex logic to handle
> the complicated world of calendars, days and time, but the API
> is not suitable for the many simple cases like yours.
>
> The main problem (I think) is that dates and times are mixed into
> the same class: java.util.Date.


That is not a problem, as most of Date has been deprecated, and it appears
people are being advised to avoid Date for now projects.

--
Paul Lutus
http://www.arachnoid.com

 
Reply With Quote
 
Jacob
Guest
Posts: n/a
 
      08-19-2004
Paul Lutus wrote:
> Jacob wrote:
>
>
>>Erwin Moller wrote:
>>
>>
>>>(Sorry for complaining)
>>>Is it just me or do dates, milliseconds, GregorianCalendar completely
>>>confusing?

>>
>>You're allowed to complain!
>>
>>It's not a mess as it contains a lot of complex logic to handle
>>the complicated world of calendars, days and time, but the API
>>is not suitable for the many simple cases like yours.
>>
>>The main problem (I think) is that dates and times are mixed into
>>the same class: java.util.Date.

>
>
> That is not a problem, as most of Date has been deprecated, and it appears
> people are being advised to avoid Date for now projects.


java.util.Date as such is not deprecated (as I know of),
though many of its access functions are.
And I am not sure using a GregorianCalendar instead
improves matters?

 
Reply With Quote
 
Paul Lutus
Guest
Posts: n/a
 
      08-19-2004
Jacob wrote:

/ ...

>> That is not a problem, as most of Date has been deprecated, and it
>> appears people are being advised to avoid Date for now projects.

>
> java.util.Date as such is not deprecated (as I know of),
> though many of its access functions are.


Because so many of Date's methods are deprecated (23 altogether), that's
saying the same thing.

> And I am not sure using a GregorianCalendar instead
> improves matters?


Yes, of course it does. GregorianCalendar was written to address the
deficiencies in Date. Unless by "matters" you mean making things simpler
for the programmer.

--
Paul Lutus
http://www.arachnoid.com

 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      08-19-2004
Erwin Moller wrote:

> I was just wondering: Am I missing something completely or is working with
> dates really confusing?
> (Like the double getTime().getTime() constructions.)
>
> What is your experience?


Java's date/time API are confusing, inconsistent, and sometimes
counter-intuitive. I have found that the API docs do help me, but they
can't solve the underlying problem that the API themselves are not well
designed. The API and docs can be used fairly easily once one has
become familiar with them, however.

> <%@ page import="java.util.Date" %>
> <%@ page import="java.util.Calendar" %>
> <%@ page import="java.text.SimpleDateFormat" %>
> <%@ page import="java.util.Date" %>
> <%@ page import="java.util.Calendar" %>
> <%@ page import="java.util.GregorianCalendar" %>


You import Date and Calendar twice, but don't explicitly use either one.
You import SimpleDateFormat but don't use it at all. You only need
the last (GregorianCalendar) import.

> <%
> // this file represents a countdontcounter
>
> // We have 2 dates:
> // startElectionDate = startdate of elections
> // endElectionDate = enddate of elections
>
> // format them: (yyyy,mm,dd)
> // warning, month is 0 based! 0 = januari, 1 = februari
> GregorianCalendar startElectionGregorianCalendar = new
> GregorianCalendar(2004, 7 , 2);
> GregorianCalendar endElectionGregorianCalendar = new
> GregorianCalendar(2004, 8, 1);


Your code would be clearer (and you wouldn't need the warning comment)
if you used the month number static constants declared in Calendar
instead of plain int arguments.

> GregorianCalendar nowGregorianCalendar = new GregorianCalendar();


You only use that calendar to get the current time, so you could just as
well say:

long now = System.currentTimeMillis();

> long mssecInADay = (1000*60*60*24);
>
> // if <0 already passed
> double dDaysToStartDate = (double)
> (startElectionGregorianCalendar.getTime().getTime( ) -
> nowGregorianCalendar.getTime().getTime()) / mssecInADay;


Instead of .getTime().getTime() you might want to use .getTimeInMillis()

> // if <0 already passed
> double dDaysToEndDate = (double)
> (endElectionGregorianCalendar.getTime().getTime() -
> nowGregorianCalendar.getTime().getTime()) / mssecInADay;
>
> String strDisplay = "";
> if (dDaysToStartDate >0){
> strDisplay = ""+dDaysToStartDate+" days untill elections.";
> } else {
> if (dDaysToEndDate >0) {
> strDisplay = "Elections running. you have "+dDaysToEndDate+" days
> to vote left.";
> } else {
> strDisplay = "Voting is over.";
> }
> }
> %>
> <!-- Start countdown -->
> <%= strDisplay %>
> <!-- End countdown -->


My earlier comments notwithstanding, that doesn't seem particularly big
or complicated.


John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Babu Kalakrishnan
Guest
Posts: n/a
 
      08-19-2004
Paul Lutus wrote:
> Jacob wrote:
>
>
>>>That is not a problem, as most of Date has been deprecated, and it
>>>appears people are being advised to avoid Date for now projects.

>>
>>java.util.Date as such is not deprecated (as I know of),
>>though many of its access functions are.

>
>
> Because so many of Date's methods are deprecated (23 altogether), that's
> saying the same thing.
>


Actually it is not the same thing.

As mentioned in the javadoc comments of the Date class, it is only the methods
of Date that perform the conversion from/to the human readable year/month....
(and the Locale specific stuff that takes care of TimeZones and daylight saving)
that have been deprecated in favour of the Calendar class. For performing any
relative time computations as what the OP wanted, Date is what he would really
need to use. (The getTimeInMillis() function does use a Date object internally
and is only a shortcut to calendar.getTime().getTime()).

It is another matter that without the deprecated functions, the Date class is
simply a wrapper around a longint - but it is certainly not deprecated, and its
use is essential for several applications (like date formatting, SQL date/time
functions etc)

BK
 
Reply With Quote
 
Paul Lutus
Guest
Posts: n/a
 
      08-19-2004
Babu Kalakrishnan wrote:

> Paul Lutus wrote:
>> Jacob wrote:
>>
>>
>>>>That is not a problem, as most of Date has been deprecated, and it
>>>>appears people are being advised to avoid Date for now projects.
>>>
>>>java.util.Date as such is not deprecated (as I know of),
>>>though many of its access functions are.

>>
>>
>> Because so many of Date's methods are deprecated (23 altogether), that's
>> saying the same thing.
>>

>
> Actually it is not the same thing.


Because Date's remaining non-deprecated methods have some residual
usefulness, I guess I agree. But if it were a deprecation Olypmpics, Date
would lose (or win, depending on your outlook).

--
Paul Lutus
http://www.arachnoid.com

 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      08-19-2004
Paul Lutus wrote:
> Because Date's remaining non-deprecated methods have some residual
> usefulness, I guess I agree. But if it were a deprecation Olypmpics, Date
> would lose (or win, depending on your outlook).


In my view, it's not so much that Date has remaining useful methods (it
really doesn't do much versus long), but that it's used in the standard
API as a marker that a parameter represents a date/time, versus some
other kind of number. There's no indication that it's use as such
should be discouraged in other code as well, and I consider it good form
versus declaring a lot of parameters of type long, or certainly versus
writing your own wrapper for a date/time to compensate.

It speaks volumes, for example, that the replacement for all of that
lost functionality of the Date class is now provided by classes that
can't reasonable be used without Date (java.text.DateFormat and
java.util.Calendar, for example).

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GregorianCalendar - it's a bomb ;-) [code insight] Xandau Java 9 06-25-2004 12:45 PM
Convert from java.sql.Date to GregorianCalendar TT \(Tom Tempelaere\) Java 17 02-01-2004 01:04 AM
GregorianCalendar Andreas N Rasmussen Java 5 12-06-2003 12:31 AM
GregorianCalendar applet and StreamCorruptedException Nigel Wade Java 10 11-24-2003 05:15 PM
GregorianCalendar and how to subtract MONTH from a date. Manoj Nair Java 4 09-04-2003 05:49 AM



Advertisments