package org.nhooo.example.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.DatabaseMetaData; import java.sql.SQLException; public class DateTimeFunction { private static final String URL = "jdbc:mysql://localhost/nhooo"; private static final String USERNAME = "root"; private static final String PASSWORD = ""; public static void main(String[] args) { try (Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD)) { DatabaseMetaData meta = connection.getMetaData(); // 获取数据库支持的日期和时间函数 String[] functions = meta.getTimeDateFunctions().split(",\\s*"); for (String function : functions) { System.out.println("Function = " + function); } } catch (SQLException e) { e.printStackTrace(); } } }
MySQL数据库支持的日期和时间功能。
Function = DAYOFWEEK Function = WEEKDAY Function = DAYOFMONTH Function = DAYOFYEAR Function = MONTH Function = DAYNAME Function = MONTHNAME Function = QUARTER Function = WEEK Function = YEAR Function = HOUR Function = MINUTE Function = SECOND Function = PERIOD_ADD Function = PERIOD_DIFF Function = TO_DAYS Function = FROM_DAYS Function = DATE_FORMAT Function = TIME_FORMAT Function = CURDATE Function = CURRENT_DATE Function = CURTIME Function = CURRENT_TIME Function = NOW Function = SYSDATE Function = CURRENT_TIMESTAMP Function = UNIX_TIMESTAMP Function = FROM_UNIXTIME Function = SEC_TO_TIME Function = TIME_TO_SEC
Maven依赖
<!-- https://search.maven.org/remotecontent?filepath=mysql/mysql-connector-java/8.0.17/mysql-connector-java-8.0.17.jar --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.17</version> </dependency>