开发者

Java Date Format Conversion

开发者 https://www.devze.com 2023-01-06 09:35 出处:网络
I am currently pulling three form field inputs off the request object. The day, the month, and the year.

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 mont开发者_JS百科h 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?

  • Java SE 8, Java SE 9, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

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.

0

精彩评论

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

关注公众号