无法将日期类型 yyyy-MM-dd 转换为 Java 中的 LocalDateTime。

huangapple go评论56阅读模式
英文:

Cannot convert date type yyyy-MM-dd to LocalDateTime in Java

问题

我从用户那里获取一个日期输入,其类型为yyyy-MM-dd(例如:2023-02-26)。我尝试将其转换为LocalDateTime格式,以便得到类似以下格式的内容,2023-02-26 22:03:05.034981

我尝试过在线上提供的示例,如下所示。

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime createdDate = LocalDateTime.parse(projectSearchCriteria.getCreatedDate(), formatter);

但是出现以下错误

Caused by: java.time.format.DateTimeParseException: Text '2023-02-26' could not be parsed at index 10

对于如何解决这个问题有任何想法吗。

====================== 附加信息 ===================

我已将字符串输入转换为LocalDate

但仍然出现以下错误

这是因为数据库中有LocalDateTime格式

是否有特定的方法可以使用2023-02-26在数据库中找到类似2023-02-26 22:03:05.034981的内容?

英文:

I am getting a date input from the user which is of type yyyy-MM-dd (e.g: 2023-02-26). I am trying to convert it to LocalDateTime format so that I will get something like this format, 2023-02-26 22:03:05.034981

I have tried examples available online like below.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime createdDate =  LocalDateTime.parse(projectSearchCriteria.getCreatedDate(), formatter);

But getting below error

Caused by: java.time.format.DateTimeParseException: Text '2023-02-26' could not be parsed at index 10

Any ideas on how to get this issue fixed.

====================== Additional Info ===================

I have converted so that the string input is now parsed to a LocalDate

无法将日期类型 yyyy-MM-dd 转换为 Java 中的 LocalDateTime。

But still it gives the below error

无法将日期类型 yyyy-MM-dd 转换为 Java 中的 LocalDateTime。

This is because I have the LocalDateTime format in the database

无法将日期类型 yyyy-MM-dd 转换为 Java 中的 LocalDateTime。

Is there any specific way how I can use the 2023-02-26 to find something like 2023-02-26 22:03:05.034981 in the database?

答案1

得分: 1

tl;dr

LocalDateTime.of(
    LocalDate.parse("2023-02-26"),
    LocalTime.parse("22:03:05.034981")
)

Details

LocalDateTime format so that I will get something like this format, 2023-02-26 22:03:05.034981

你混淆了日期时间对象和格式化文本。日期时间对象不是文本,没有格式

日期时间类可用于解析文本片段,提取含义并实例化日期时间对象。但原始文本将被丢弃。日期时间对象和String对象是完全不同的。

I am getting a date input from the user which is of type yyyy-MM-dd (e.g: 2023-02-26)

因此,将输入文本解析为LocalDate,这是一个仅包含日期而没有时间和时间区域或UTC偏移的值。

LocalDate ld = LocalDate.parse("2023-02-26");

显然,您想要附加一个特定的时间(您未清楚描述原因)。首先,解析有关时间的文本输入。

LocalTime lt = LocalTime.parse("22:03:05.034981");

👉 将这两者合并为LocalDateTime

LocalDateTime ldt = LocalDateTime.of(ld, lt);

Is there any specific way how I can use the 2023-02-26 to find something like 2023-02-26 22:03:05.034981 in the database?

您是否想在数据库中搜索与日期和时间完全匹配的内容?

首先,请确保数据库表列的数据类型类似于SQL数据类型TIMESTAMP WITHOUT TIME ZONE。验证WITHOUT部分,而不是WITH

然后,使用通过准备好的语句传递值的?语法编写SQL查询。

String sql =
    """
    SELECT * 
    FROM some_table_
    WHERE date_time_ = ?
    ;
    """
;

查询。

myPreparedStatement.setObject(1, ldt);

检索找到的行。

LocalDateTime ldt = myResultSet.getObject(, LocalDateTime.class);

另外...请注意,LocalDateTime对象是模糊的和不明确的。在没有区域或偏移的情况下,LocalDateTime对象无法表示一个时刻,不是时间轴上的一个点。

英文:

tl;dr

LocalDateTime.of( 
    LocalDate.parse( "2023-02-26" ) , 
    LocalTime.parse( "22:03:05.034981" ) 
)

Details

> LocalDateTime format so that I will get something like this format, 2023-02-26 22:03:05.034981

You are confusing date-time objects with formatted text. A date-time object is not text, and has no format.

A date-time class can be used to parse a piece of text, extract meaning, and instantiate a date-time object. But the original text is discarded. Date-time objects and String objects are entirely separate and distinct.

>I am getting a date input from the user which is of type yyyy-MM-dd (e.g: 2023-02-26)

So parse that input text as a LocalDate, a date-only value without a time-of-day and without the context of a time zone or offset-from-UTC.

LocalDate ld = LocalDate.parse( "2023-02-26" ) ;

Apparently you want to attach to that date a particular time-of-day (for reasons you do not describe clearly). First parse your text input for that time-of-day.

LocalTime lt = LocalTime.parse( "22:03:05.034981" ) ;

👉 Combine the two into a LocalDateTime.

LocalDateTime ldt = LocalDateTime.of( ld , lt ) ;

>Is there any specific way how I can use the 2023-02-26 to find something like 2023-02-26 22:03:05.034981 in the database?

Do you mean search the database for that exact date and time?

First, be sure the database table column is of a data type akin to the SQL data type TIMESTAMP WITHOUT TIME ZONE. Verify the WITHOUT part, rather than WITH.

Then write your SQL query with the ? syntax for passing a value via a prepared statement.

String sql =
    """
    SELECT * 
    FROM some_table_
    WHERE date_time_ = ?
    ;
    """
;

Query.

myPreparedStatement.setObject( 1 , ldt ) ;

Retrieve found rows.

LocalDateTime ldt = myResultSet.getObject( … , LocalDateTime.class ) ;

By the way… Be aware that a LocalDateTime object is ambiguous and indefinite. Without the context of a zone or offset, a LocalDateTime object cannot represent a moment, is not a point on the timeline.

huangapple
  • 本文由 发表于 2023年2月26日 23:12:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572915.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定