Java parse date from TextField with a different fo

2019-09-22 02:21发布

This question already has an answer here:

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!

4条回答
叛逆
2楼-- · 2019-09-22 02:31

Try this:

    SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
    Date date = df.parse("2016-03-05");
查看更多
闹够了就滚
3楼-- · 2019-09-22 02:31

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.

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?

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.

查看更多
该账号已被封号
4楼-- · 2019-09-22 02:33

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);
查看更多
祖国的老花朵
5楼-- · 2019-09-22 02:46

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
查看更多
登录 后发表回答