1

I have a graphical user interface which takes some user inputs, it takes the current date also. Then I need to store them in a database. Everything is fine but I cannot understand the way how should I parse the input string of date field to mysql date for inserting it into the database.

I have a code like this.

Date date = txtToday.getText();

I know this should be parsed in to a type which is compatible with Data data type. This Date type is from java.sql.Date. How can I overcome this issue.?

vigamage
  • 1,975
  • 7
  • 48
  • 74

4 Answers4

2

MySQL’s default DATE field format is: YYYY-MM-DD

Whereas in Java, the Date class’ (available in java.util package) default format is,dow mon dd hh:mm:ss zzz yyyy

so use this:

    Date date = txtToday.getText();
    String pattern = "yyyy-MM-dd";
    SimpleDateFormat formatter = new SimpleDateFormat(pattern);
    String mysqlDateString = formatter.format(date);
    System.out.println("Java's Default Date Format: " + date);
    System.out.println("Mysql's Default Date Format: " + mysqlDateString);
Yoganand.N
  • 907
  • 1
  • 10
  • 24
1
try {
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
    Date dob = null;
    dob = sdf.parse(txtToday.getText());
    java.sql.Date sqlDate = new java.sql.Date(dob.getTime());
    c1.setDOB(sqlDate); //use this sqlDate for inserting into Database
    } catch (ParseException e) {
          System.out.println("Parse exception,incorrect input in the textbox");
          e.printStackTrace();
}
SparkOn
  • 8,806
  • 4
  • 29
  • 34
Sagar D
  • 2,588
  • 1
  • 18
  • 31
  • java.text.ParseException: Unparseable date: "" – vigamage Aug 21 '14 at 17:19
  • This is the right answer. It's assumes txtToday.getText() is returning the string that the user entered, in the format "dd-MM-yyy". You are apparently returning blank instead. Please provide the calling and implementation code. – Ted Bigham Aug 21 '14 at 18:39
  • @user3892439 be clear with your requirements, I've assumed that txtToday is the textbox in which user is entering the date in dd-MM-yyyy format – Sagar D Aug 21 '14 at 18:42
0

If this line of code works...

Date date = txtToday.getText();

Then just use the time to convert it to a sql Date...

java.sql.Date sqlDate = new java.sql.Date(date.getTime());
Ted Bigham
  • 4,237
  • 1
  • 26
  • 31
0

See my Answer to a duplicate Question.

Using java.time

Use the modern java.time classes rather than the troublesome legacy date-time classes.

No need to use java.sql.Date. That class is replaced by LocalDate. The LocalDate class represents a date-only value without time-of-day and without time zone.

JDBC drivers that comply with JDBC 4.2 can deal directly with java.time types by calling:

For presentation of the LocalDate to the user, generate a String for display in your user-interface. Use a DateTimeFormatter to automatically localize. To localize, specify:

  • FormatStyle to determine how long or abbreviated should the string be.
  • Locale to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.

Example:

Locale l = Locale.CANADA_FRENCH ; 
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.MEDIUM ).withLocale( l );
String output = zdt.format( f );

You can go the other direction as well, parsing an input string to get a date.

LocalDate ld = LocalDate.parse( input , f ) ;

Trap for the exception thrown if the user’s input is faulty or unexpected.

try{ 
    LocalDate ld = LocalDate.parse( input , f ) ;
    myPrepStmt.setObject( … , ld ) ;
} catch ( DateTimeParseException e ) {
    … // Handle the error condition of faulty/unexpected input by user.
}

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.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154