[MySQL] [java] Receive date and time

Overview

I will introduce how to get the date and time registered in MySQL with java, and how to register the date and time from java to MySQL. See here for how to connect to MySQL. https://qiita.com/QiitaD/items/d605b07e849e3bec0722

Columns defined in MySQL

Define the date and time with the timestamp method. The column name is "date_time" and the format is "yyyy-MM-dd HH: mm: ss".

Get in java from MySQL

 String mySql = "select * from table_name"; // SQL statement to get data with date and time
 ResultSet rs = stmt.executeQuery (mySql); // SQL execution, get data
    while (rs.next()) {
 rs.getTimestamp ("date_time"); // Get date and time information from the acquired data
    }        

Register the date and time from java to MySQL

Register the current time as an example.

    String mySql = "insert into table (date_time) values ('" + getNowDateTime(){ + "')";
    stmt.executeUpdate(mySql);
    /**
 * Get the current date and time in yyyy / MM / dd HH: mm: ss format. <br>
     */
    public static String getNowDateTime(){
        final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final Date date = new Date(System.currentTimeMillis());
        return df.format(date);
    }

See here for how to get the current time. https://qiita.com/zuccyi/items/d9c185588a5628837137

Where in trouble

It's not a problem, but there are two points to note for beginners. ① SQL execution statement When fetching data with select, it is executeQuery, but when updating, insert, delete, it is executeUpdate.

② Single quotation It's easy to forget, but in this sample you have to enclose the date and time in'(single quotes).

Please be aware of the above two because I often stumbled when I was a super beginner.

Impressions

In this article, I introduced an example of how to handle the date and time, but there are other ways to use it because there are various ways to handle the date and time. (Date, LocaleDateTime, etc.) If you change the type, conversion becomes troublesome, so think carefully before using it.

Reference URL

Date and time type https://www.dbonline.jp/mysql/type/index4.html

Difference between timestamp and datetime https://qiita.com/ykawakami/items/2449a24e3b82ff0cbab6

timestamp exception https://ts0818.hatenablog.com/entry/2017/08/11/155011

Recommended Posts

[MySQL] [java] Receive date and time
Date and time
[Java] Date / time operations
Java release date and EOL summary
Handle Java 8 date and time API with Thymeleaf with Spring Boot
What is the LocalDateTime class? [Java beginner] -Date and time class-
Sample code to parse date and time with Java SimpleDateFormat
[Java] Use ResolverStyle.LENIENT to handle the date and time nicely
The relationship between strict Java date checking and daylight savings time
Explanation of Ruby Time and Date objects
[Java] How to set the Date time to 00:00:00
Java 8 to start now ~ Date time API ~
Beginners create web applications with Java and MySQL (adding at any time)
[Java] How to get the current date and time and specify the display format
Java and JavaScript
Date () and Calendar ()
XXE and Java
Format the date and time given by created_at
Date and time acquisition method using DateAndTime API
The date time of java8 has been updated
java algorithm Artery Time rounding up and down
[Java] How to use Calendar class and Date class
[Java] Get and display the date 10 days later using the Time API added from Java 8.
Parse the date and time string formatted by the C asctime function in Java
Getters and setters (Java)
[Java] Thread and Runnable
Java true and false
Change date and time to Japanese notation in Rails
[Java] String comparison and && and ||
[Java] Connect to MySQL
Accurate time measurement (Java)
Java basic date manipulation
[Rails] Precautions when comparing date and time with DateTime
Learn for the first time java # 3 expressions and operators
Usability of date and time classes in each language
Java --Serialization and Deserialization
[Java] Arguments and parameters
timedatectl and Java TimeZone
[Java] Branch and repeat
Date manipulation in Java 8
[Java] Variables and types
java (classes and instances)
[MySQL + Java] Numbering procedure
java: Add date [Note]
Handling of date and time in Ruby. Use Date and Time properly.
[Java] Overload and override
[Java] Date type conversion
Set the date and time from the character string with POI
Difficult to handle minimum values for Java8 LocalDateTime and Java6 Date
[Rails] Search method when date and time have each column
A Simple CRUD Sample Using Java Servlet / JSP and MySQL
Study Java # 2 (\ mark and operator)
java mail pop3 imap receive
AWS and on-premise time lag
Java version 8 and later features
[Template] MySQL connection with Java
[Java] Difference between == and equals
[Java] Stack area and static area
[Java] Generics classes and generics methods
Java programming (variables and data)
Java encryption and decryption PDF