Java sql date from string

Java sql date from string

A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. A milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT. To conform with the definition of SQL DATE , the millisecond values wrapped by a java.sql.Date instance must be ‘normalized’ by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.

Constructor Summary

Method Summary

This method always throws an UnsupportedOperationException and should not be used because SQL Date values do not have a time component.

Obtains an instance of Date from a LocalDate object with the same year, month and day of month value as the given LocalDate .

Methods inherited from class java.util.Date

Methods inherited from class java.lang.Object

Constructor Detail

Date

@Deprecated public Date(int year, int month, int day)

Constructs a Date object initialized with the given year, month, and day. The result is undefined if a given argument is out of bounds.

Читайте также:  Java static array in class

Date

Constructs a Date object using the given milliseconds time value. If the given milliseconds value contains time information, the driver will set the time components to the time in the default time zone (the time zone of the Java virtual machine running the application) that corresponds to zero GMT.

Method Detail

setTime

public void setTime(long date)

Sets an existing Date object using the given milliseconds time value. If the given milliseconds value contains time information, the driver will set the time components to the time in the default time zone (the time zone of the Java virtual machine running the application) that corresponds to zero GMT.

valueOf

toString

getHours

This method is deprecated and should not be used because SQL Date values do not have a time component.

getMinutes

This method is deprecated and should not be used because SQL Date values do not have a time component.

getSeconds

This method is deprecated and should not be used because SQL Date values do not have a time component.

setHours

@Deprecated public void setHours(int i)

This method is deprecated and should not be used because SQL Date values do not have a time component.

setMinutes

@Deprecated public void setMinutes(int i)

This method is deprecated and should not be used because SQL Date values do not have a time component.

setSeconds

@Deprecated public void setSeconds(int i)

This method is deprecated and should not be used because SQL Date values do not have a time component.

valueOf

Obtains an instance of Date from a LocalDate object with the same year, month and day of month value as the given LocalDate . The provided LocalDate is interpreted as the local date in the local time zone.

toLocalDate

Converts this Date object to a LocalDate The conversion creates a LocalDate that represents the same date value as this Date in local time zone

toInstant

This method always throws an UnsupportedOperationException and should not be used because SQL Date values do not have a time component.

Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.

Источник

How to convert a String into a Date object using JDBC API?

The valueOf() method of the Date object accepts a String value representing a Date in JDBC escape format i.e. yyyy-mm-dd and converts the given String value into java.sql.Date object.

Date date = Date.valueOf(“date_string”);

Assume we have created a table named employee_data with the description as shown below:

+----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | Name | varchar(255) | YES | | NULL | | | Dob | date | YES | | NULL | | | Location | varchar(255) | YES | | NULL | | +----------+--------------+------+-----+---------+-------+

Following JDBC program accepts id (integer), name(String), date of birth(String), and, location(String) of the employees, converts the date of birth value passed in JDBC escape syntax format to Date object and inserts the given details in to the employee_data table. At the end it retrieves all the records in the table once and displays.

import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.util.Scanner; public class StringtoDate < public static void main(String args[])throws Exception < //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established. "); //Creating a Statement object Statement stmt = con.createStatement(); Scanner sc = new Scanner(System.in); System.out.println("Enter the number of records you need to insert: "); int num = sc.nextInt(); //Inserting values to the table String query = "INSERT INTO employee_data VALUES (?, ?, ?, ?)"; PreparedStatement pstmt = con.prepareStatement(query); for(int i=1; iSystem.out.println("data inserted"); //Creating Statement object stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from employee_data"); //Retrieving values while(rs.next()) < System.out.println("Employee_Id: "+rs.getInt("ID")); System.out.println("Employee_Name: "+rs.getString("Name")); System.out.println("Employee_DOB: "+rs.getInt("DOB")); System.out.println("Employee_Location: "+rs.getString("Location")); System.out.println(); >> >

Output

Connection established. table created. Enter the number of records you need to insert in the table: 3 Enter the Employee ID: 1001 Enter the Employee name: Krishna Enter the Employee DOB in the format yyyy-mm-dd : 1989-09-26 Enter the Employee Location : Hyderabad Enter the Employee ID: 1002 Enter the Employee name: Kasyap Enter the Employee DOB in the format yyyy-mm-dd : 1990-06-25 Enter the Employee Location : Vishakhapatnam Enter the Employee ID: 1003 Enter the Employee name: Maruthi Enter the Employee DOB in the format yyyy-mm-dd : 1995-06-06 Enter the Employee Location : Vijayawada data inserted Employee_Id: 1001 Employee_Name: Krishna Employee_DOB: 1989 Employee_Location: Hyderabad Employee_Id: 1002 Employee_Name: Kasyap Employee_DOB: 1990 Employee_Location: Vishakhapatnam Employee_Id: 1003 Employee_Name: Maruthi Employee_DOB: 1995 Employee_Location: Vijayawada

Источник

Class Date

A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. A milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT.

To conform with the definition of SQL DATE , the millisecond values wrapped by a java.sql.Date instance must be ‘normalized’ by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.

Constructor Summary

Method Summary

This method always throws an UnsupportedOperationException and should not be used because SQL Date values do not have a time component.

Obtains an instance of Date from a LocalDate object with the same year, month and day of month value as the given LocalDate .

Methods declared in class java.util.Date

Methods declared in class java.lang.Object

Constructor Details

Date

Constructs a Date object initialized with the given year, month, and day. The result is undefined if a given argument is out of bounds.

Date

Constructs a Date object using the given milliseconds time value. If the given milliseconds value contains time information, the driver will set the time components to the time in the default time zone (the time zone of the Java virtual machine running the application) that corresponds to zero GMT.

Method Details

setTime

Sets an existing Date object using the given milliseconds time value. If the given milliseconds value contains time information, the driver will set the time components to the time in the default time zone (the time zone of the Java virtual machine running the application) that corresponds to zero GMT.

valueOf

toString

getHours

This method is deprecated and should not be used because SQL Date values do not have a time component.

getMinutes

This method is deprecated and should not be used because SQL Date values do not have a time component.

getSeconds

This method is deprecated and should not be used because SQL Date values do not have a time component.

setHours

This method is deprecated and should not be used because SQL Date values do not have a time component.

setMinutes

This method is deprecated and should not be used because SQL Date values do not have a time component.

setSeconds

This method is deprecated and should not be used because SQL Date values do not have a time component.

valueOf

Obtains an instance of Date from a LocalDate object with the same year, month and day of month value as the given LocalDate . The provided LocalDate is interpreted as the local date in the local time zone.

toLocalDate

toInstant

This method always throws an UnsupportedOperationException and should not be used because SQL Date values do not have a time component.

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Java sql date from string

A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. A milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT. To conform with the definition of SQL DATE , the millisecond values wrapped by a java.sql.Date instance must be ‘normalized’ by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.

Constructor Summary

Method Summary

This method always throws an UnsupportedOperationException and should not be used because SQL Date values do not have a time component.

Obtains an instance of Date from a LocalDate object with the same year, month and day of month value as the given LocalDate .

Methods declared in class java.util.Date

Methods declared in class java.lang.Object

Constructor Detail

Date

@Deprecated(since="1.2") public Date​(int year, int month, int day)

Constructs a Date object initialized with the given year, month, and day. The result is undefined if a given argument is out of bounds.

Date

Constructs a Date object using the given milliseconds time value. If the given milliseconds value contains time information, the driver will set the time components to the time in the default time zone (the time zone of the Java virtual machine running the application) that corresponds to zero GMT.

Method Detail

setTime

public void setTime​(long date)

Sets an existing Date object using the given milliseconds time value. If the given milliseconds value contains time information, the driver will set the time components to the time in the default time zone (the time zone of the Java virtual machine running the application) that corresponds to zero GMT.

valueOf

toString

getHours

@Deprecated(since="1.2") public int getHours()

This method is deprecated and should not be used because SQL Date values do not have a time component.

getMinutes

@Deprecated(since="1.2") public int getMinutes()

This method is deprecated and should not be used because SQL Date values do not have a time component.

getSeconds

@Deprecated(since="1.2") public int getSeconds()

This method is deprecated and should not be used because SQL Date values do not have a time component.

setHours

@Deprecated(since="1.2") public void setHours​(int i)

This method is deprecated and should not be used because SQL Date values do not have a time component.

setMinutes

@Deprecated(since="1.2") public void setMinutes​(int i)

This method is deprecated and should not be used because SQL Date values do not have a time component.

setSeconds

@Deprecated(since="1.2") public void setSeconds​(int i)

This method is deprecated and should not be used because SQL Date values do not have a time component.

valueOf

Obtains an instance of Date from a LocalDate object with the same year, month and day of month value as the given LocalDate . The provided LocalDate is interpreted as the local date in the local time zone.

toLocalDate

toInstant

This method always throws an UnsupportedOperationException and should not be used because SQL Date values do not have a time component.

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Оцените статью