Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store only date without time in a database

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

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

like image 581
Yousuf Soomro Avatar asked Dec 02 '22 20:12

Yousuf Soomro


2 Answers

For storing only date you need 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

like image 60
nhgrif Avatar answered Dec 14 '22 16:12

nhgrif


  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); 
  }
like image 20
Sudhir K Bansal Avatar answered Dec 14 '22 16:12

Sudhir K Bansal