3

I have a Date format in java that I need to save as a Time format in MySQL. Is there a way to just get the time part from the date? I know Dat.gettime() that returns a long but I just need the Time in MySql

Any suggestions...

Thx all ..

skaffman
  • 398,947
  • 96
  • 818
  • 769
Darth Blue Ray
  • 9,445
  • 10
  • 35
  • 48

6 Answers6

3

Using a PreparedStatement together with a java.sql.Time object created from your java.util.Date should work:

java.util.Date myDate = ....
java.sql.Time theTime = new java.sql.Time(myDate.getTime());
PreparedStatement pstmt = ...
pstmt.setTime(1, theTime);
  • Correct Answer, but a couple caveats: (A) This produces a time-of-day in [UTC](https://en.wikipedia.org/wiki/Coordinated_Universal_Time) rather than [a particular time zone](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones), which may or may not be what you want. (B) The [`java.sql.Time`](http://docs.oracle.com/javase/8/docs/api/java/sql/Time.html) class is now legacy, supplanted by the [`java.time.LocalTime`](https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html) class as of [JDBC 4.2](https://jcp.org/aboutJava/communityprocess/mrel/jsr221/index2.html) and later. – Basil Bourque Sep 02 '17 at 19:06
0

Depending on the JDBC driver, it could omit the time portion of the date, better use java.sql.Timestamp.

java.sql.Timestamp mysqlDate = new java.sql.Timestamp( javaDate.getTime() );
xgMz
  • 3,334
  • 2
  • 30
  • 23
0

Get Date + Time Mysql

ResultSet rs=....;

Date tg = new Date(rs.getTimestamp("column datetime").getTime());
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm aaa");

String val = sdf.format(tg);
Anil
  • 2,539
  • 6
  • 33
  • 42
0

When exchanging data with a database, use appropriate date types and objects. Do not use mere strings or integers instead of legitimate types/classes.

java.time

The modern approach uses java.time classes. With a JDBC driver complying with JDBC 4.2 or later, you can omit the use of java.sql.Time class, and just use java.time.LocalTime directly.

The legacy class java.util.Date represents a date and a time-of-day in UTC. Convert to a modern java.time class (Instant), assign a time zone through which you want to view the value, and extract the time-of-day.

To convert, call new methods added to the old legacy classes.

Instant instant = myJavaUtilDate.toInstant() ;

Assign a time zone.

The time zone is crucial. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" ) ;  // Or "Asia/Kolkata", or "Pacific/Auckland", etc.
ZonedDateTime zdt = instant.atZone( z ) ;

Extract the time-of-day without a date.

LocalTime lt = zdt.toLocalTime() ;

Pass into your PreparedStatement by calling setObject.

String sql = "INSERT INTO tbl_ ( time_of_day_ ) VALUES ( ? ) ; " ;
PreparedStatement ps = conn.prepareStatement( sql ) ;
ps.setObject( 1 , lt ) ;  // Call `setObject` to pass a java.time object directly without converting into `java.sql.*` type.
int rows = ps.executeUpdate() ;

Fetch data in a similar manner, calling ResultSet::getObject.

LocalTime lt = myResultSet.getObject( … , LocalTime.class ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

Use the java.sql.Date to build a SQL Date.

long d = date.getTime();
java.sql.Date sqlDate = new java.sql.Date(d);
Pih
  • 2,282
  • 15
  • 20
0

You could use SimpleDateFormat to format the Time fields of your date:

SimpleDateFormat sdf =  new SimpleDateFormat("HH:mm:ss" );
String time = sdf.format (  yourDate  );
stacker
  • 68,052
  • 28
  • 140
  • 210