Java DatabaseMetaData getTimeDateFunctions() method with example



In this article, we will learn how to retrieve the list of time and date functions in the database using the getTimeDateFunctions() method of the DatabaseMetaData class in Java.

getTimeDateFunctions()

The getTimeDateFunctions() method retrieves the list of time and date functions supported by the current database. The names returned by this method are the Open CLI time and date function names.

Steps to Retrieve Time and Date Functions in Database

To get the list of the time and date functions supported by the underlying database ?

  • Step 1: Register the Driver: Make sure your database is up and running. Register the driver using the registerDriver() method of the DriverManager class. Pass an object of the driver class corresponding to the underlying database.
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
  • Step 2: Establish a Connection: Get the connection object using the getConnection() method of the DriverManager class. Pass the URL, the database, and the user name, and password of a user in the database, as String variables.
String url = "jdbc:mysql://localhost/mydatabase";
Connection con = DriverManager.getConnection(url, "root", "password");
System.out.println("Connection established......");
  • Step 3: Retrieve the DatabaseMetaData Object: Get the DatabaseMetaData object with respect to the current connection using the getMetaData() method of the Connection interface.
DatabaseMetaData metaData = con.getMetaData();

  • Step 4: Get the List of Time and Date Functions: get the list of time and date functions supported by the underlying database by invoking the getTimeDateFunctions() method of the DatabaseMetaData class.
String timeDateFunctions = metaData.getTimeDateFunctions();
StringTokenizer tokenizer = new StringTokenizer(timeDateFunctions, ",");
while(tokenizer.hasMoreElements()) {
System.out.println(tokenizer.nextToken());
}

Example

The following JDBC program establishes a connection with the MySQL database and, retrieves the list of time and date functions supported by the underlying database.

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.StringTokenizer;
public class DatabaseMetadata_getTimeDateFunctions {
	public static void main(String args[]) throws SQLException {
//Registering the Driver
		DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//Getting the connection
		String url = "jdbc:mysql://localhost/mydatabase";
		Connection con = DriverManager.getConnection(url, "root", "password");
		System.out.println("Connection established......");
//Retrieving the meta data object
		DatabaseMetaData metaData = con.getMetaData();
//Retrieving the list of time and date functions
		String numeric_functions = metaData.getTimeDateFunctions();
		StringTokenizer tokenizer = new StringTokenizer(numeric_functions, ",");
		while(tokenizer.hasMoreElements()) {
			System.out.println(tokenizer.nextToken());
		}
		System.out.println(" ");
	}
}

Output

Connection established......
DAYOFWEEK
WEEKDAY
DAYOFMONTH
DAYOFYEAR
MONTH
DAYNAME
MONTHNAME
QUARTER
WEEK
YEAR
HOUR
MINUTE
SECOND
PERIOD_ADD
PERIOD_DIFF
TO_DAYS
FROM_DAYS
DATE_FORMAT
TIME_FORMAT
CURDATE
CURRENT_DATE
CURTIME
CURRENT_TIME
NOW
SYSDATE
CURRENT_TIMESTAMP
UNIX_TIMESTAMP
FROM_UNIXTIME
SEC_TO_TIME
TIME_TO_SEC
Updated on: 2025-01-23T22:53:19+05:30

101 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements