Share on Google+Share on Google+

JDBC : Get year from Date

In this section, you will learn how to get year from date.

JDBC : Get year from Date

In this section, you will learn how to get year from date.

Get year from Date :

In MySql you can get year from the given Date by using year() function.

 YEAR(date) : This method returns the year of the specified date. It ranges from 1000 to 9999 or returns 0 for no date.

You can write sql query as -  sql = "SELECT YEAR('2000-12-31')"

Example :

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBCCurrentDate {
	public static void main(String[] args) {
		System.out.println("Get year from date Example...");
		Connection conn = null;
		String url = "jdbc:mysql://localhost:3306/";
		String dbName = "employees";
		String driverName = "com.mysql.jdbc.Driver";
		String userName = "root";
		String password = "root";
		Statement statement = null;
		ResultSet rs;
		try {
			conn = DriverManager
					.getConnection(url + dbName, userName, password);
			statement = conn.createStatement();
			String sql = "SELECT YEAR('2000-12-31')";
			rs = statement.executeQuery(sql);
			System.out.print("Year of given date : - ");
			while ( {

		} catch (Exception e) {

Output :

Get year from date Example...
Year of given date : - 2000



Posted on: October 12, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null