标签: java-time

Jackson:用时区 ID 解析 ZonedDateTime

使用Spring MVC(Spring boot)我可以ZonedDateTime反序列化

2016-07-26T05:30:47+01:00
Run Code Online (Sandbox Code Playgroud)

但不是来自

2016-07-26T05:30:47+01:00 Europe/Paris
Run Code Online (Sandbox Code Playgroud)

如何显式添加时区并且仍然能够反序列化它?

java spring-mvc jackson spring-boot java-time

3
推荐指数
1
解决办法
6529
查看次数

将 org.joda.time.DateTime 转换为 java.time.OffsetDateTime 的最佳方法是什么?

long以下方法有效,但将时间转换为 a ,然后Instant将时区转换为 aTimeZone然后 a似乎有点笨拙ZoneId。有没有更干净的方法来做到这一点?

java.time.Instant instant = java.time.Instant.ofEpochMilli(jodaDateTime.getMillis());
OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(instant,
        jodaDateTime.getZone().toTimeZone().toZoneId());
Run Code Online (Sandbox Code Playgroud)

java jodatime datetime-conversion java-time

3
推荐指数
1
解决办法
7215
查看次数

日期字符串到 OffsetDateTime

我正在将日期/日期时间字符串转换为OffsetDateTime并且我有日期时间格式,它可能具有这些值之一

yyyy-MM-dd, yyyy/MM/dd
Run Code Online (Sandbox Code Playgroud)

有时有或没有时间,我需要将其转换为OffsetDateTime.

我试过下面的代码

// for format yyyy-MM-dd
DateTimeFormatter DATE_FORMAT = new DateTimeFormatterBuilder().appendPattern("yyyy-MM-dd")
                        .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
                        .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
                        .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
                        .parseDefaulting(ChronoField.MILLI_OF_SECOND, 0)
                        .toFormatter();
Run Code Online (Sandbox Code Playgroud)

由于没有时间,我将其设置为默认值,但是当我尝试解析时

OffsetDateTime.parse("2016-06-06", DATE_FORMAT)
Run Code Online (Sandbox Code Playgroud)

它抛出错误就像

线程“main”中的异常 java.time.format.DateTimeParseException:无法解析文本“2016-06-06”:无法从 TemporalAccessor 获取 OffsetDateTime:{},ISO 解析为 java 类型的 2016-06-06T00:00 .time.format.Parsed 已解析

谁能帮我解决这个问题吗?

datetimeoffset datetime-parsing java-8 java-time

3
推荐指数
1
解决办法
2万
查看次数

java.time.Clock.systemDefaultZone().getZone() 与 java.util.TimeZone.getDefault().toZoneId() 之间有什么区别?

鉴于两者java.time.Clock.systemDefaultZone().getZone()都java.util.TimeZone.getDefault().toZoneId()返回相同的输出,两者之间有什么区别吗?

例如这段代码

import java.time.Clock;
import java.util.TimeZone;

public class Main {

  public static void main(String[] args) {
    System.out.println("Clock.systemDefaultZone().getZone() : " 
        + Clock.systemDefaultZone().getZone());
    System.out.println("TimeZone.getDefault().toZoneId() : " 
        + TimeZone.getDefault().toZoneId());
  }

}
Run Code Online (Sandbox Code Playgroud)

返回此输出

Clock.systemDefaultZone().getZone() : Europe/Paris
TimeZone.getDefault().toZoneId() : Europe/Paris
Run Code Online (Sandbox Code Playgroud)

java timezone java-8 java-time

3
推荐指数
1
解决办法
2212
查看次数

使用 LONG 或 FULL 的 FormatStyle 进行本地化格式化程序时,OffsetDateTime 失败

太长了;博士

\n\n

这失败了。

\n\n
OffsetDateTime.now()\n              .format( \n                  DateTimeFormatter.ofLocalizedDateTime( FormatStyle.LONG )\n              )  // throws DateTimeException.\n
Run Code Online (Sandbox Code Playgroud)\n\n

但作品中的同一时刻具有相同的偏移量ZonedDateTime。

\n\n

为什么?

\n\n

细节

\n\n

当让自动本地化viajava.time的字符串表示形式时,如果格式化程序带有of或 ,则调用可以工作。但是当格式化程序携带or时,会抛出a 。然而,使用相同的时刻和相同的偏移量成功了。为什么?OffsetDateTimeDateTimeFormatter.ofLocalizedDateTimeformatFormatStyleSHORTMEDIUMLONGFULLDateTimeExceptionZonedDateTime

\n\n
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.LONG ) ;\n\nOffsetDateTime odt = OffsetDateTime.now( ZoneId.systemDefault() ) ;\nZonedDateTime zdt = odt.atZoneSameInstant( odt.getOffset() ) ;  // Generate a `ZonedDateTime` with same moment and same offset as the `OffsetDateTime`.\n\n// Succeeds.\nString outputZdt = zdt.format( f ) ;\nSystem.out.println( …
Run Code Online (Sandbox Code Playgroud)

java date-formatting timezone-offset java-time

3
推荐指数
1
解决办法
2572
查看次数

如何在 Java 中获取 Temporal 的年份

我的问题是从 Temporal 接口中找到一个有用的方法。具体来说,我想从一个实现 Temporal 接口的对象中获取年份。接口的所有方法都只是从对象中添加或删除,但是,我只想获取时间本身的年份。

java java-8 java-time

3
推荐指数
1
解决办法
1572
查看次数

Java8时间库不能正确解释BST时区

我正在迁移我的代码以使用该java.time包,但我发现它DateTimeFormatter不能正确解释时区“BST”(英国夏令时间)。

它没有将其UTC+0100转换为太平洋/布干维尔时区,而是将其转换为太平洋/布干维尔时区。

有谁知道我如何解决这个问题而不需要回到旧的 SimpleDateFormat 或使用明确的时区?我的代码需要在世界多个地区运行。

这个时间戳格式是通过查询另一个系统获得的,所以我无法更改它。看来SimpleDateFormat可以正确识别时区。我的测试代码如下:

String sTime = "Fri Jun 07 14:07:07 BST 2019";
DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("EEE MMM dd kk:mm:ss z yyyy");
ZonedDateTime zd = ZonedDateTime.parse(sTime, FORMATTER);
System.out.println("The time zone: " + zd.getZone());
FileTime ftReturn = FileTime.from(zd.toEpochSecond(), TimeUnit.SECONDS);
System.out.println("File time is: " + ftReturn);

DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH);
Date dtDD = df.parse(sTime);
Calendar calendar = Calendar.getInstance();
calendar.setTime(dtDD);
FileTime ftReturn1 = FileTime.fromMillis(calendar.getTimeInMillis());
System.out.println("File time1 …
Run Code Online (Sandbox Code Playgroud)

java datetime-format java-8 java-time zoneddatetime

3
推荐指数
1
解决办法
1065
查看次数

DateTimeFormatter 不适用于低于 8 的 Android 版本

我想从另一个时间字符串中减去 30 分钟,但在较低版本(例如(Lollipop、Marshmallow、Naught))上遇到此错误 0.5,0.6,0.7。
错误如下:

java.lang.NoClassDefFoundError:解析失败:Ljava/time/format/DateTimeFormatter;

java time android date java-time

3
推荐指数
1
解决办法
2241
查看次数

基于使用 java.time API 的区域设置正确排列工作日

德国等一些国家从周一开始一周,美国等一些国家从周日开始一周。我认为 dayOfWeek.value 有一个基于 Locale 的版本,但我找不到它。有没有办法使用 java.time API 根据区域设置以正确的顺序获取工作日

java java-time

3
推荐指数
1
解决办法
227
查看次数

DateTimeParseException - 无法在索引 0 处进行解析

我正在尝试解析以下日期"Wed, 26 Feb 2020 03:42:25 -0800"

String datePattern = "EEE, dd MMM yyyy HH:mm:ss Z";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(datePattern);
ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateStr, formatter);
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

java.time.format.DateTimeParseException: Text 'Wed, 26 Feb 2020 03:42:25 -0800'  
could not be parsed at index 0
Run Code Online (Sandbox Code Playgroud)

谢谢

java date-parsing java-time

3
推荐指数
1
解决办法
2433
查看次数