Java string valueof date

Java sql.Date valueOf() метод с примером

Давайте создадим таблицу с диспетчеризацией имен в базе данных MySQL с помощью оператора CREATE следующим образом:

CREATE TABLE dispatches( ProductName VARCHAR(255), CustomerName VARCHAR(255), DispatchDate date, DeliveryTime time, Price INT, Location VARCHAR(255));

Теперь мы вставим 5 записей в таблицу диспетчеризации, используя операторы INSERT.

insert into dispatches values('Key-Board', 'Raja', DATE('2019-09-01'), TIME('11:00:00'), 7000, 'Hyderabad'); insert into dispatches values('Earphones', 'Roja', DATE('2019-05-01'), TIME('11:00:00'), 2000, 'Vishakhapatnam'); insert into dispatches values('Mouse', 'Puja', DATE('2019-03-01'), TIME('10:59:59'), 3000, 'Vijayawada'); insert into dispatches values('Mobile', 'Vanaja', DATE('2019-03-01'), TIME('10:10:52'), 9000, 'Chennai'); insert into dispatches values('Headset', 'Jalaja', DATE('2019-04-06'), TIME('11:08:59'), 6000, 'Goa');

Следующая программа JDBC устанавливает соединение с базой данных и вставляет новую запись в таблицу диспетчеризации.

import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Time; public class Date_valueOf < public static void main(String args[]) throws SQLException < //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. "); //Inserting values to a table String query = "INSERT INTO dispatches VALUES (?, ?, ?, ?, ?, ?)"; PreparedStatement pstmt = con.prepareStatement(query); pstmt.setString(1, "Watch"); pstmt.setString(2, "Rajan"); Date date = Date.valueOf("2019-01-26"); pstmt.setDate(3, date); pstmt.setTime(4, new Time(1567315800000L)); pstmt.setInt(5, 4000); pstmt.setString(6, "Chennai"); pstmt.execute(); //Retrieving data Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from dispatches"); while(rs.next()) < System.out.print("Name: "+rs.getString("ProductName")+", "); System.out.print("Customer Name: "+rs.getString("CustomerName")+", "); System.out.print("Dispatch Date: "+rs.getDate("DispatchDate")+", "); System.out.print("Delivery Time: "+rs.getTime("DeliveryTime")+", "); System.out.print("Price: "+rs.getInt("Price")+", "); System.out.print("Location: "+rs.getString("Location")); System.out.println(); >> >

Здесь, в этой программе мы берем значение даты в формате String и преобразуем его в объект java.util.Date с помощью метода valueOf ().

Читайте также:  Html template design online

Вывод

Connection established. Name: Key-Board, Customer Name: Raja, Dispatch Date: 2019-09-01, Delivery Time: 11:00:00, Price: 7000, Location: Hyderabad, Name: Earphones, Customer Name: Roja, Dispatch Date: 2019-05-01, Delivery Time: 11:00:00, Price: 2000, Location: Vishakhapatnam, Name: Mouse, Customer Name: Puja, Dispatch Date: 2019-03-01, Delivery Time: 10:59:59, Price: 3000, Location: Vijayawada, Name: Mobile, Customer Name: Vanaja, Dispatch Date: 2019-03-01, Delivery Time: 10:10:52, Price: 9000, Location: Chennai, Name: Headset, Customer Name: Jalaja, Dispatch Date: 2019-04-06, Delivery Time: 11:08:59, Price: 6000, Location: Goa, Name: Watch, Customer Name: Rajan, Dispatch Date: 2019-01-26, Delivery Time: 11:00:00, Price: 4000, Location: Chennai

Средняя оценка 0 / 5. Количество голосов: 0

Спасибо, помогите другим — напишите комментарий, добавьте информации к статье.

Видим, что вы не нашли ответ на свой вопрос.

Напишите комментарий, что можно добавить к статье, какой информации не хватает.

Источник

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 string valueof 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 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.

Источник

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.

Источник

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