*
The moose likes Beginning Java and the fly likes today's date & time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "today Watch "today New topic
Author

today's date & time

dale con
Ranch Hand

Joined: Apr 15, 2005
Posts: 93
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
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

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.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
dale con
Ranch Hand

Joined: Apr 15, 2005
Posts: 93
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?
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

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).
dale con
Ranch Hand

Joined: Apr 15, 2005
Posts: 93
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();
dale con
Ranch Hand

Joined: Apr 15, 2005
Posts: 93
Sorted it

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: today's date & time