In this section, you will learn how to get year from date.
In this section, you will learn how to 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 { Class.forName(driverName); 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 (rs.next()) { System.out.println(rs.getInt(1)); } conn.close(); } catch (Exception e) { e.printStackTrace(); } } }
Output :
Get year from date Example... Year of given date : - 2000