E
Erwin Moller
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 -->
(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 -->