10

How can I send the date only from Java into a MySQL table that has a column of the DATE data type? I can achieve that by saving string into the database, but is there a better way?

I am working on attendance of employees without time in and time out. So I only need to store the date.

1
  • @nhgrif: Yes, you are right. Look here.
    – Keppil
    Commented Jan 19, 2014 at 15:50

2 Answers 2

24

For storing only the date, you need the DATE data type.


MySQL has three data types for storing date and time values:

  • DATE is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format.
  • DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD hh:mm:ss' format.
  • TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.

More details can be found in the official MySQL Reference Manual
The DATE, DATETIME, and TIMESTAMP Types, where the above quote is from.

3
  • 1
    I know about Data datatype in MySQL but how can I send value from Java, Sorry If I was not clear enough but I want to know how can I get current Date from Java to store in (Date)MySQL Commented Jan 19, 2014 at 15:53
  • Why don't you use the server's date: CURRENT_DATE()?
    – anon
    Commented Jan 19, 2014 at 16:08
  • That helped, THANKS I was struggling to find java datatype for past 2 hours. I used CURRENT_DATE() to store and to retrieve I used WHERE DATE(date) = CURRENT_DATE() [found from another post] Commented Jan 19, 2014 at 16:41
2
  SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd"); 
  String input = "2014-01-19";  // capture the value you pass from attendance application
  Date t; 
  try { 
      t = ft.parse(input); 
      System.out.println(t); 
  } catch (ParseException e) { 
      System.out.println("Unparseable using " + ft); 
  }
1
  • 5
    You could provide a basic explaination next time so newbies can know what's actually your solution about
    – nKn
    Commented Jan 19, 2014 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.