[英]Date object formatting
I'm trying to format an input date in Java which looks like follows in the debugger.我正在尝试在 Java 中格式化输入日期,它在调试器中如下所示。
result = {Date@13861} "2019-09-29"
fastTime = 1569729600000
cdate = {Gregorian$Date@13873} "2019-09-29T00:00:00.000-0400"
I need to convert it into: 2019-09-28 20:00:00.0
.我需要将其转换为: 2019-09-28 20:00:00.0
。
My code:我的代码:
DateTimeFormatter dateTimeformatter_Test = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
LocalDate tempDate = LocalDate.parse(date.toString(), DateTimeFormatter.ofPattern( "yyyy-MM-dd" , Locale.US ));
But the line below throws a runtime error saying: Unsupported field: HourOfDay但下面的行抛出一个运行时错误说:不支持的字段:HourOfDay
String result= tempDate.format(dateTimeformatter_Test);
What am I missing here please?请问我在这里缺少什么?
Thanks.谢谢。
Your question is lacking time zone information, but here is one way to convert 2019-09-29
into 2019-09-28 20:00:00.0
:您的问题缺少时区信息,但这是将2019-09-29
转换为2019-09-28 20:00:00.0
一种方法:
System.out.println(
LocalDate.parse("2019-09-29")
.atStartOfDay(ZoneOffset.UTC)
.withZoneSameInstant(ZoneId.of("America/New_York"))
.format(DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.S")));
Output输出
2019-09-28 20:00:00.0
Here is an entirely different way, showing the values you saw in the debugger:这是一种完全不同的方式,显示您在调试器中看到的值:
TimeZone.setDefault(TimeZone.getTimeZone("America/New_York"));
java.sql.Date result = java.sql.Date.valueOf("2019-09-29");
System.out.println("result = " + result);
long fastTime = result.getTime();
System.out.println("fastTime = " + fastTime);
System.out.println("cdate = " + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(result));
ZonedDateTime zdt = Instant.ofEpochMilli(fastTime).atZone(ZoneId.of("America/Anchorage"));
System.out.println("zdt = " + zdt.format(DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.S")));
Output输出
result = 2019-09-29
fastTime = 1569729600000
cdate = 2019-09-29T00:00:00.000-0400
zdt = 2019-09-28 20:00:00.0
If this is for a time stamp for your SQL database: don't give your database a time stamp as a string.如果这是用于 SQL 数据库的时间戳:不要将时间戳作为字符串提供给数据库。 Give it a proper date-time object.给它一个合适的日期时间对象。 Since JDBC 4.2 this means:从 JDBC 4.2 开始,这意味着:
timestamp with time zone
(recommended for the vast majority of purposes) provide an OffsetDateTime
;对于timestamp with time zone
的 SQL timestamp with time zone
(推荐用于绝大多数用途)提供OffsetDateTime
; many drivers accept an Instant
too.许多司机也接受Instant
。timestamp
without time zone provide a LocalDateTime
.对于没有时区的 SQL timestamp
,提供LocalDateTime
。So for example:例如:
java.sql.Date inputDate = getFromSomewhere();
OffsetDateTime dateTimeForDatabase = inputDate.toLocalDate()
.atStartOfDay(ZoneOffset.UTC)
.toOffsetDateTime();
System.out.println(dateTimeForDatabase);
PreparedStatement stmt = yourDatabaseConnection.prepareStatement(
"insert into your_table(your_timestamp_col) values (?)");
stmt.setObject(1, dateTimeForDatabase);
int rowsInserted = stmt.executeUpdate();
Example output from the print statement in the middle:中间打印语句的示例输出:
2019-09-29T00:00Z 2019-09-29T00:00Z
The Z
means UTC, so this is the same point in time as the 2019-09-28 20:00:00.0
you asked for, assuming that your JVM's time zone is some variant of North American Eastern Time (America/Toronto or America/New_York). Z
表示 UTC,因此这与您要求的2019-09-28 20:00:00.0
时间点相同,假设您的 JVM 时区是北美东部时间(美国/多伦多或美国/纽约)。
The object that you see in your debugger looks very much like a java.sql.Date
object, so I have taken this type as my starting point.您在调试器中看到的对象看起来非常像java.sql.Date
对象,因此我将这种类型作为我的起点。 The java.sql.Date
class is poorly designed, though, in fact a true hack on top of the already poorly designed java.util.Date
class. java.sql.Date
类的设计很糟糕,但实际上,它是在已经设计得很糟糕的java.util.Date
类之上的一个真正的黑客。 It is also long outdated.它也早已过时。 So if you could get a modern type instead, for example a LocalDate
, it would be advantageous.因此,如果您可以获得现代类型,例如LocalDate
,那将是有利的。
A LocalDate
is a date without time of day, for example 2019-09-29. LocalDate
是没有时间的日期,例如 2019-09-29。 You were trying to format one using the format pattern yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
.您试图使用格式模式yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
格式化一个。 So you are asking to include the time of day in the result, but as I said, the LocalDate
hasn't got a time of day, so this does not make sense.所以你要求在结果中包含一天中的时间,但正如我所说, LocalDate
没有一天中的时间,所以这没有意义。 The error message that you got was actually pretty precise:您收到的错误消息实际上非常准确:
Unsupported field: HourOfDay不支持的字段:HourOfDay
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.