开发者

Java Date -> to MySql Time

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 开发者_如何学PythonTime in MySql

Any suggestions...

Thx all ..


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);


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

long d = date.getTime();
java.sql.Date sqlDate = new java.sql.Date(d);


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

SimpleDateFormat sdf =  new SimpleDateFormat("HH:mm:ss" );
String time = sdf.format (  yourDate  );


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() );


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);


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 ) ;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜