开发者

Comparing MySQL and Java Time

I have a datetime field in MySQL which I access through calling result.getString('date'), now I would like to check weather the current date and time in Java has exceeded the MySQL time or is before the MySQL time to check weather a result is activated or not.

Datetime from MySQL has the form: 2011-12-30 17:10:00, how can I c开发者_如何学编程ompare this string to the Java time?


In Java, you can construct a Date from a string, using a SimpleDateFormat:

String text = "2011-12-30 17:10:00";
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(text);
Date now = new Date();

if (date.after(now))
{
    // do stuff
}

You can instead perform similar operations in the database, using basic arithmetic and (I think) equality operators: +, -, >, <, etc., as well as MySQL's date and time functions.

...though I'm curious to know why you are using getString() instead of ResultSet#getTimestamp().


One other word of advice: consider using Joda Time instead of java.util.Date and java.util.Calendar. See Should I use Java date and time classes or go with a 3rd party library like Joda Time?


Prior to JDK 8

You can use ResultSet.getDate('date') to retreive a Date object. Then use the method Date.before() or Date.after() to check.

JDK 8 or later

Refer to Basil Bourque's answer below.


Why don't you just use getDate('date') instead of getString() and use Date's before() or after() ?


tl;dr

JDBC 4.2 and later:

Boolean isFutureTask = myResultSet.getObject( "myDateColumn" , Instant.class )
                                  .isAfter( Instant.now() ) ;

Older JDBC:

Boolean isFutureTask = myResultSet.getTimestamp( … )
                                  .toInstant()
                                  .isAfter( Instant.now() ) ;

java.time

The other answers were correct but use outmoded classes. The java.util.Date/.Calendar classes have been supplanted by the java.time framework built into Java 8 and later.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

Date-time types

You should be storing your date-time values as a date-time type in your database. I believe that would be TIMESTAMP type in MySQL (but I'm a Postgres man myself).

Then use the java.sql.Timestamp type to move data in/out of the database.

java.sql.Timestamp ts = myResultSet.getTimestamp( 1 );

Immediately convert from java.sql to java.time. The java.sql classes are part of those old date-time classes that have proven to be poorly designed, confusing, and troublesome. Use them only until JDBC drivers are updated to directly deal with java.time types.

The old java.util.Timestamp has new methods for converting to/from an Instant. An Instant is a moment on the timeline in UTC with a resolution of nanoseconds.

java.time.Instant instant = ts.toInstant();

Get the current moment.

Instant now = Instant.now();

Compare.

Boolean isStoredDateTimePast = now.isAfter( instant );

In JDBC 4.2 and later, your JDBC driver may be able to directly access the value from the database as an Instant object via the ResultSet::getObject method.


If you must parse that string, convert it to standard ISO 8601 format. Replace the space in the middle with a T. Assuming the string represents an offset-from-UTC of zero, append Z for Zulu which means UTC. The java.time classes parse/generate strings by default in ISO 8601 formats.

String inputOriginal = "2011-12-30 17:10:00".
String input = inputOriginal.replace( " " , "T" ) + "Z" ;
Instant instant = Instant.parse( input );


You can use the Joda DateTime api that is very easy for comparing dates, we have the same requirement in use that takes mysql datetime format and creates joda datetime objects for comparison.


java.util.Calendar.getInstance().getTime().getTime() will get you the current date and time in milliseconds

ResultSet.getDate('name of column').getTime()

will get you the date and time from the database....

u can compare/make logical operations like you do with numbers..... for example, this will check whether the system time is greater than the datetime from the database

java.util.Calendar.getInstance().getTime().getTime() > new java.sql.Date(WIDTH).getTime();

you can look up in the API documentation for more information on these methods....

Hope it helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜