Java parse date from TextField with a different fo

2019-09-22 02:44发布

问题:

This question already has an answer here:

  • Parsing String into mysql Date [duplicate] 4 answers

I'm facing some problems in Eclipse with dates. I'm showing a date in a TextField with the format

dd/MM/yyyy

and I need to get the text from this textfield and parse it as a date to insert it in my database into a column of type DATE I'm using MySQL and it accepts dates as

yyyy-MM-dd

I tried really many options, both with LocalDate or the older Date, but didn't find a solution. Can someone please help? Thank you!

回答1:

Prior Java 8 approach

Firstly, after retrieving your string from your TextField, you should parse it to java.util.Date:

String text = textField.getText();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
java.util.Date textFieldAsDate = null;

try {
    textFieldAsDate = sdf.parse(text);
} catch (ParseException pe) {
    // deal with ParseException
}

Afterwards, you can convert your java.util.Date into a java.sql.Date, to store it into your MySQL database, like this:

sdf = new SimpleDateFormat("yyyy-MM-dd");
java.sql.Date date = java.sql.Date.valueOf(sdf.format(textFieldAsDate));

Java 8 approach

As mentioned by @BasilBourque, the Java date and time mechanism was provided by java.util.Date, java.util.Calendar, and java.util.TimeZone classes which are now legacy.

Therefore, in order to accomplish using Java 8 what was previously mentioned, one can do the following:

String text = textField.getText();
java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern("dd/MM/yyyy");
java.time.LocalDate textFieldAsDate = java.time.LocalDate.parse(text, formatter);

Afterwards, in order to convert the LocalDate into a java.sql.Date, to store it into your MySQL database, it's possible to do the following:

java.sql.Date sqlDate = java.sql.Date.valueOf(textFieldAsDate);


回答2:

You want to use the SimpleDateFormat class

Once you have extracted it from the TextField, you can use this:

String extractedDate = "dd/MM/yyyy" // Whatever date you've extracted
Date date = new SimpleDateFormat("dd/MM/yyyy").parse(extractedDate);
String correctDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
System.out.println(correctDate); // "yyyy-MM-dd" for whatever date was extracted


回答3:

Try this:

    SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
    Date date = df.parse("2016-03-05");


回答4:

java.time

The other Answers are correct but use outdated classes. The old date-time classes such as java.util.Date/.Calendar have proven to be poorly designed, confusing, and troublesome. Avoid them. They have been supplanted by the java.time framework built into Java 8 and later.

Define a pattern by which to parse your input string.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "dd/MM/yyyy" );

Parse the input string as a LocalDate object, representing a date-only value without time-of-day nor time zone.

LocalDate localDate = LocalDate.parse( "23/01/2016" , formatter );

Someday the JDBC drivers will be updated to work directly with the java.time types. But until then we must convert to the java.sql types for transferring data to/from the database.

java.sql.Date sqlDate = java.sql.Date.valueOf( localDate );

Then use a PreparedStatement, calling setDate and passing your sqlDate to write the value into the database.

To retrieve from the database, call getDate. Immediately convert to java.time by calling java.sql.Date::toLocalDate. Minimize your use of the java.sql types. Do your business logic in java.time.

If your JDBC driver complies with JDBC 4.2 and later, you can skip the java.sql types and use the java.time types directly.

  • PreparedStatement::setObject
    myPrepStmt.setObject( … , myLocalDate )
  • ResultSet::getObject
    LocalDate ld = myResultSet.getObject( … , LocalDate.class );

This Question is really a duplicate. Search Stack Overflow for many more examples.


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
    • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
    • 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.