使用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)
如何显式添加时区并且仍然能够反序列化它?
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) 我正在将日期/日期时间字符串转换为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 已解析
谁能帮我解决这个问题吗?
鉴于两者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) 这失败了。
\n\nOffsetDateTime.now()\n .format( \n DateTimeFormatter.ofLocalizedDateTime( FormatStyle.LONG )\n ) // throws DateTimeException.\nRun Code Online (Sandbox Code Playgroud)\n\n但作品中的同一时刻具有相同的偏移量ZonedDateTime。
为什么?
\n\n当让自动本地化viajava.time的字符串表示形式时,如果格式化程序带有of或 ,则调用可以工作。但是当格式化程序携带or时,会抛出a 。然而,使用相同的时刻和相同的偏移量成功了。为什么?OffsetDateTimeDateTimeFormatter.ofLocalizedDateTimeformatFormatStyleSHORTMEDIUMLONGFULLDateTimeExceptionZonedDateTime
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) 我的问题是从 Temporal 接口中找到一个有用的方法。具体来说,我想从一个实现 Temporal 接口的对象中获取年份。接口的所有方法都只是从对象中添加或删除,但是,我只想获取时间本身的年份。
我正在迁移我的代码以使用该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) 我想从另一个时间字符串中减去 30 分钟,但在较低版本(例如(Lollipop、Marshmallow、Naught))上遇到此错误 0.5,0.6,0.7。
错误如下:
java.lang.NoClassDefFoundError:解析失败:Ljava/time/format/DateTimeFormatter;
德国等一些国家从周一开始一周,美国等一些国家从周日开始一周。我认为 dayOfWeek.value 有一个基于 Locale 的版本,但我找不到它。有没有办法使用 java.time API 根据区域设置以正确的顺序获取工作日
我正在尝试解析以下日期"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-time ×10
java ×9
java-8 ×4
android ×1
date ×1
date-parsing ×1
jackson ×1
jodatime ×1
spring-boot ×1
spring-mvc ×1
time ×1
timezone ×1