Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

today's date & time

 
dale con
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
dale con
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorted it

Thanks
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic