Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert String type to time type and store it in database

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to convert the String type to the time type into the database", so the editor summarizes the following, detailed contents, clear steps, and a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to convert the String type to the time type and save it in the database".

Convert String to time and store it in the database

I haven't tried to coexist String type conversions into date type fields in the database for a long time. I happened to encounter and solve this small problem today, so I wrote it down to make a deep impression.

Usually, we usually set the time processing field in the database to characters such as char, which has the advantage of being easy to read and store, saving the trouble of converting types.

However, sometimes it is necessary to set the field to date type for storage time, and so on.

Here's an example.

My field name is chusheng_time, which is the date of birth, and the type is set to date.

The type in java is the string type

As shown below:

I use the traditional jdbc interface provided by java to connect to the database

The specific code is:

PreparedStatement statement = database.connection.prepareStatement (sql); try {SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); / / set the date format java.util.Date d = sdf.parse (chushengTime); / / Note that java.util.Date mysql is converted here, and the database also provides its own date schema. Do not confuse statement.setDate (4, new java.sql.Date (d.getTime (). / / here is to convert the time of until.date to sql.date type, this step is necessary} catch (ParseException e) {/ / TODO Auto-generated catch block e.printStackTrace (); System.out.println ("error");}

Generally speaking, it is the time when the string type time is first converted to the java.util.Date type in the java background, and when the database is inserted, it is forced to convert to the java.sql.Date type, so that it can be inserted into the database normally.

Description of the problem of converting numbers of String type to time and date format

Convert String type number format to time and date format, such as String time1= "1585107267188", to 2020-03-25 11:34:27

Go straight to the code:

Package com.jwdntjfx.Test;import java.text.SimpleDateFormat;import java.util.Date;/** * got a string of numbers such as: String time1= "1585107267188"; * convert to date format * / public class test {public static void main (String [] args) {/ * * define the date format you want to export * / SimpleDateFormat sdf1=new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss") SimpleDateFormat sdf2=new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss"); SimpleDateFormat sdf3=new SimpleDateFormat ("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy String time3=sdf1.format (time2); String time4=sdf2.format (time2); String time5=sdf3.format (time2); System.out.println (time3); / / the output result is 2020-03-25 11:34:27 System.out.println (time4); / / the output result is 2020-03-25 11:34:27 System.out.println (time5) / / the output result is 11:34:27 on March 25, 2020}} the above is about the content of this article on "how to convert String type to time type and store it in the database". I believe everyone has a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report