Number of days since registration
I code a little Console program and now I store the date they joined in a database like.
CreateDate
2011-04-15 17:52:57
Now I want to do a check like this: a function that gets how many days the guy have been registered.
if(player.getDaysSinceRegistrat开发者_运维百科ion) {
Thanks for any help.
joda-time has an easy way to do this:
Days.daysBetween(new DateTime(registeredDate), new DateTime()).getDays();
Without 3rd party libraries:
(System.currentTimeMillis() - registeredDate.getTime()) / MILLIS_PER_DAY;
First of all, don't store dates in databases in textual form.
You should save them as database dates (a type like numbers or varchars), as this will allow you to both ask the database do date calculations as well - the exact way to do so is database specific - as have it automatically pulled up in a Java date object by the JDBC driver which is much easier to work with than strings. See Bozho's answer for suggestions to do this in the Java layer.
精彩评论