简体   繁体   English

在Java中接受用户的日期并将其写入Postgresql

[英]Accepting a date from user in java and writing it to postgresql

I need to write to a column of data type Date in postgresql from a java application. 我需要从Java应用程序写入postgresql中数据类型为Date的列。 In which datatype should I accept the input so that I can use it to insert into Postgresql? 我应该接受哪种数据类型的输入,以便可以将其插入Postgresql?

LocalDate

As of JDBC 4.2 , the modern way to store a date-only value without time-of-day and without time zone is the LocalDate class. JDBC 4.2开始 ,没有日期和时区的仅日期值存储的现代方法是LocalDate类。

LocalDate ld = LocalDate.of( 2019 , Month.JANUARY , 23 ) ;

This maps to a DATE type in Postgres . 这映射到Postgres中DATE类型。

myPreparedStatement.setObject( … , ld ) ;

Retrieval. 恢复。

LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;

The legacy class java.sql.Date class is now legacy , and should never be used because of its severe design flaws. 遗留类java.sql.Date类现在是legacy ,并且由于其严重的设计缺陷而永远不应使用。

Java(旧式和现代)和标准SQL中的日期时间类型表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM