简体   繁体   中英

Java Date Format Conversion

I am currently pulling three form field inputs off the request object. The day, the month, and the year.

Day would be 11 for 11th day of the month Month would be 12 for december Year would be 2010 representing this year.

I need to convert this into a Java Date object, but since so much has been changed, I am not sure what the best way to store this in a java object. I need it in the format

YYYY-MM-DD HH:MM:SS
so I can compare to dates in the MySql Database.

SimpleDateFormat can convert strings to java.util.Date objects. Date class has a getTime() method that yields the date in unix time format (number of milliseconds since January 1, 1970, 00:00:00 GMT).

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
long unix_time = dateFormat.parse(date).getTime();

You can use UNIX_TIMESTAMP() in MySQL to convert to unix time as well.

mysql> SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');
        -> 1196440219

I need to convert this into a Java Date object

Instantiate a GregorianCalendar , set the appropriate fields, and call getTime on it. Since it doesn't sound like you're collecting time information, you'll probably want to specifically clear those fields or you'll get the current time of when that object was constructed.

I need it in the format [...] so I can compare to dates in the MySql Database.

You shouldn't need it in a text format to compare dates. Create the column using the database's native date/time type and use the Date type within Java. JDBC should do the conversion for you as it goes in or out of the database.

Date formatting should only be used for output to an external process or for display. See Bakkal's answer for formatting using SimpleDateFormat .

tl;dr

myPreparedStatement.setObject(         // Exchange java.time object with database directly, with JDBC 4.2 and later.
    … , 
    LocalDate.of( 2012 , 12 , 10 )     // Instantiate a `java.time.LocalDate` object.
)

java.time

The modern approach uses the java.time classes.

For a date-only value, if the database column is like the SQL-standard DATE type, use LocalDate class in Java.

LocalDate ld = LocalDate.of( 2012 , 12 , 10 ) ; 

Or use Month enum object.

LocalDate ld = LocalDate.of( 2012 , Month.DECEMBER , 10 ) ; 

With a JDBC driver compliant with JDBC 4.2 or later, you can directly exchange java.time objects with the database. No need for mere strings; use smart objects.

myPreparedStatement.setObject( … , ld ) ;

Retrieval.

LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat .

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes.

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

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM