我很难将ISO 8601格式化的字符串转换为UTC 格式的java.time.LocalDateTime.
更具体的,我试图写一个XMLAdapter的,你可以输入各种ISO 8601个dataformats作为字符串(即2002-09-24,2011-03-22T13:30,2015-05-24T12:25:15Z,2015-07-28T11:11:15.321+05:30),并且在输出UTC,反之亦然一个LocalDateTime.
系统以UTC时间内部存储所有日期和时间信息.当用户请求日期或时间时,它将根据自己的ZoneId表示给用户.
我想将一些文本解析为日期.但是,无法保证文本具有所需的格式.这可能是2012-12-12或者2012甚至.
Currently, I am down the path to nested try-catch blocks, but that's not a good direction (I suppose).
LocalDate parse;
try {
parse = LocalDate.parse(record, DateTimeFormatter.ofPattern("uuuu/MM/dd"));
} catch (DateTimeParseException e) {
try {
Year year = Year.parse(record);
parse = LocalDate.from(year.atDay(1));
} catch (DateTimeParseException e2) {
try {
// and so on
} catch (DateTimeParseException e3) {}
}
}
Run Code Online (Sandbox Code Playgroud)
What's an elegant solution to this problem? Is it possible to use Optional在评估期间发生异常时不存在的?如果有,怎么样?
我们的软件架构师决定从项目依赖项中删除Joda时间库,并使用Java 8时间的功能.我在做我们的项目的研究,我想找到,我们正在使用乔达时间lib中的所有地方,并确定我们使用的究竟是什么(乔达LocalDate,DateTime等等).
我不是Joda时间库的专家,我不是Java 8时间的专家,所以我有一些问题:
在代码中我看到很多这样的地方:
DateTime jodaDateTime = new DateTime();
doSomething(jodaDateTime.toDate());
Run Code Online (Sandbox Code Playgroud)
我有点困惑.
(Q1)DateTime在这种特殊情况下使用joda是否有意义
?我相信,我们可以使用经典java.util.Date,我们不需要Joda:
java.util.Date date = new Date();
doSomething(date);
Run Code Online (Sandbox Code Playgroud)
正确?
另外,我看到了这段代码:
org.joda.time.DateTime jodaDateTime = new DateTime();
org.joda.time.DateTime jodaDateTimeInPast = jodaDateTime.minusSeconds(60);
doSomething(jodaDateTimeInPast.toDate());
Run Code Online (Sandbox Code Playgroud)
我认为,java 8时间API为计算过去的任何日期提供了很好的支持,所以我知道如何将上面的代码替换为Java 8:
LocalDatetime java8DateTime = LocalDateTime.now();
LocaldateTime java8DateTimeInPast = java8DateTime.minusSeconds(60);
Date java8Date = Date.from(java8DateTimeInPast.atZone(ZoneId.systemDefault()).toInstant());
doSomething(java8Date);
Run Code Online (Sandbox Code Playgroud)
(Q2)我是否以正确的方式完成了它?我不确定100%.我刚刚发现这个链接有一个表:" 从Joda-Time转换为java.time "有我发现的信息,我需要使用java.time.ZonedDateTime或java.time.OffsetDateTime类,但不是java.time.LocalDateTime,因为LocalDateTime-
相同的概念 - 没有时区的日期和时间
请确认我的重构是正确的(Q1和Q2).
DateTimeFormatterjava.time中的类提供了三种ofLocalized…生成字符串的方法,以表示包含一年的值.例如,ofLocalizedDate.
Locale l = Locale.US ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT ).withLocale( l );
LocalDate today = LocalDate.now( ZoneId.of( "America/Chicago" ) );
String output = today.format( f );
Run Code Online (Sandbox Code Playgroud)
对于我看过的语言环境,年份只是较短FormatStyle样式的两位数.
如何让java.time本地化却迫使年份变为四位数而不是两位数?
我怀疑答案在DateTimeFormatterBuilder课堂上.但我找不到任何改变一年的长度的功能.我也仔细阅读了Java 9源代码,但是不能很好地解释代码以找到答案.
这个问题类似于:
...但是这些问题针对的是现在由java.time类取代的旧日期时间框架.
我现在正在巴黎运行这段代码,其中12:40,其中ECT = ECT - 欧洲/巴黎
LocalDateTime creationDateTime = LocalDateTime.now(Clock.systemUTC());
ZoneId zone = ZoneId.of(ZoneId.SHORT_IDS.get("ECT"));
System.out.println ("creationDateTime --------------------------> " + creationDateTime);
System.out.println ("creationDateTime.atZone(zone).getHour() ---> " + creationDateTime.atZone(zone).getHour());
System.out.println ("creationDateTime.atZone(zone).getMinute() -> " + creationDateTime.atZone(zone).getMinute());
Run Code Online (Sandbox Code Playgroud)
但我在控制台得到这个
creationDateTime --------------------------> 2017-05-16T10:40:07.882
creationDateTime.atZone(zone).getHour() ---> 10
creationDateTime.atZone(zone).getMinute() -> 40
Run Code Online (Sandbox Code Playgroud)
我不应该得到12:40 ???????
使用'old',Java 8之前SimpleDateFormat我可以做到:
new java.text.SimpleDateFormat("MMM yyyy", new java.util.Locale("es", "ES")).parse("Mayo 2017")
Run Code Online (Sandbox Code Playgroud)
获取Date具有西班牙月份名称的日期对象.
如何使用Java 8实现相同的功能DateTimeFormatter?
我试过了:
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withLocale(new Locale("es", "ES")).ofPattern("MMM yyyy").parse("Mayo 2017")
Run Code Online (Sandbox Code Playgroud)
但只得到一个java.time.format.DateTimeParseException.
我在格里高利历中有一个约会,想要在朱利安历法中查找同一天.
使用Java 8的Date/Time API应该很容易,但我找不到办法.代码看起来应该是这样的:
/*
* LocalDate uses the Gregorian calendar
*/
LocalDate date = LocalDate.parse("2017-10-29");
/*
* switch from Gregorian calendar to Julian calendar here
*/
;
/*
* output should be 2017-10-16
*/
System.out.println(date);
Run Code Online (Sandbox Code Playgroud)
编辑
这当然不是从格里高利转换为朱利安日历的重复 .我的问题专门询问了Java Data/Time API.
我正在努力使用Java 8 DateTimeFormatter.
我想将给定的转换String为dateFormat并解析为LocalDateTime
这是我的代码
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")
String text = "2020-01-01T01:01:11.123Z"
LocalDateTime date = LocalDateTime.parse(text, f)
Run Code Online (Sandbox Code Playgroud)
但Java抛出
无法解析文本,在索引19处找到未解析的文本
如果我ofPattern改为yyyy-MM-dd'T'HH:mm:ss.SSSX,我的代码执行没有任何错误.但我不想使用毫秒和时区.
我们正在从Joda迁移到Java Time.目前我们DateTime在我们的实体中使用Joda.AFAIK DateTime相当于Java中的两种类型:OffsetDateTime和ZonedDateTime.既然我们要将它们保存在数据库中,我们将使用OffsetDateTime(对此有何评论?).
现在的问题是如何ObjectMapper正确配置杰克逊.我在网上找到的所有例子都是关于Jackson已经提供de/serializer实现的本地类型(例如LocalDateTime,LocalDateTimeSerializer和LocalDateTimeDeserializer).
我终于设法做了这样的事情:
public class OffsetDateTimeSerializer extends StdSerializer<OffsetDateTime> {
private final DateTimeFormatter formatter; // We need custom format!
public OffsetDateTimeSerializer(DateTimeFormatter formatter) {
super(OffsetDateTime.class);
this.formatter = formatter;
}
@Override
public void serialize(OffsetDateTime value, JsonGenerator generator, SerializerProvider provider) throws IOException {
generator.writeString(value.format(formatter));
}
}
Run Code Online (Sandbox Code Playgroud)
和
public class OffsetDateTimeDeserializer extends StdDeserializer<OffsetDateTime> {
private final DateTimeFormatter formatter; // We need custom format!
public OffsetDateTimeDeserializer(DateTimeFormatter formatter) …Run Code Online (Sandbox Code Playgroud) 当从LocalTime对象中减去给定量时,我意识到可能发生上溢/下溢.
例:
00:30 - 35 minutes = 23:55
23:30 + 35 minutes = 00:05
Run Code Online (Sandbox Code Playgroud)
对于我的应用程序,我想锁定时间,以便上面会00:00在下溢和23:59溢出时生成.
这可能吗?
java-time ×10
java ×8
java-8 ×6
datetime ×3
2-digit-year ×1
date ×1
date-parsing ×1
jackson ×1
java-date ×1
jodatime ×1
localization ×1
localtime ×1
refactoring ×1