Forums Register Login

today's date & time

+Pie Number of slices to send: Send
Hello

I'm using this to get today's dat and time

Date now = Calendar.getInstance().getTime();

which returns this 'Tue May 17 15:39:01 BST 2005'

I'm bringing a date back from a db table and need to compare the difference between the 2 dates.

Firstly i need to make sure the date is the same

Secondly i need to see if the java time has passed the time in the db value

I also need to see if the java time is <= 1 hour of the db time


So from the db the date is '2005-05-17 16:38:56'
Java i get this 'Tue May 17 15:38:56 BST 2005'

The date is the same, the java time hasn't gone beyond the time in the db value and there's an hour left before the java time is the same as the db time

Can someone show me how i do this

Many thanks
+Pie Number of slices to send: Send
Well, the value obtained from the DB should be a java.sql.Timestamp value (a subclass of java.util.Date). The date in memory is a java.util.Date. They both have a .getTime() method that returns the time in milliseconds since the epoch. Once you have those values, calculating the rest is easy. You can take the difference between the two dates in milliseconds and then convert to whatever units you then need.
+Pie Number of slices to send: Send
this gets me the date/time back from my db table rs.getString("datetime");

and this give me today's date and time from java

Date now = Calendar.getInstance().getTime();


The format coming back are different - would i have to convert them to the same formet before doing the comparison?
+Pie Number of slices to send: Send
do a rs.getTimestamp("datetime") instead. Don't convert them to Strings; a date by itself does not have a format; it's only when we view the date does it receive one (either the default or one assigned by a DateFormat class).
+Pie Number of slices to send: Send
Hello

When i bbring the date back from the db i'm doing this

if (rs.next())
{
sqlResult = rs.getString("value") + "|" + rs.getTimestamp("expirydate")
}

So in the next bit of code i have to split the results, so i'm doing this

String [] sqlResults = s.split("\\|");

String value = sqlResults[0];
String expiryDate = sqlResults[1];

so i'm using a string for the date

How do i get around this so it's a date?

when i calc the difference betweeen the dates will it be like this

long dateDifference = expiryDate.getTime() - now.getTime();
+Pie Number of slices to send: Send
Sorted it

Thanks
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1283 times.
Similar Threads
How to manipulate date / time using Calendar and then put the modified data inside the Date class?
timezone issue
Deployment of EJBs on JBoss [$Proxy92 ??]
Date Displaying issue
Where does currentTimeMillis get the time from
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:20:13.