有没有什么简短的方法可以将java.time.ZonedDateTime转换为XMLGregorianCalendar?
也许我需要一些中间步骤,比如将ZonedDateTime转换为java.util.Date,但这会使代码过于混乱.
问题出现在JAX-WS Web服务中,datetime作为XMLGregorianCalendar传递.
我使用java.timeJava 8 的新实现,对UTC到CET时间转换结果的输出感到惊讶.
ZonedDateTime utcTime = ZonedDateTime.of(2014, 7, 1, 8, 0, 0, 0, ZoneId.of("UTC"));
ZonedDateTime cetTime = ZonedDateTime.ofInstant(utcTime.toInstant(), ZoneId.of("CET"));
System.out.println("Summer-UTC-Time: " + utcTime);
System.out.println("Summer-CET-Time: " + cetTime);
System.out.println();
utcTime = ZonedDateTime.of(2014, 1, 1, 8, 0, 0, 0, ZoneId.of("UTC"));
cetTime = ZonedDateTime.ofInstant(utcTime.toInstant(), ZoneId.of("CET"));
System.out.println("Winter-UTC-Time: " + utcTime);
System.out.println("Winter-CET-Time: " + cetTime);
Run Code Online (Sandbox Code Playgroud)
我预计CET时间总是+1时间,但我得到了:
Summer-UTC-Time: 2014-07-01T08:00Z[UTC]
Summer-CET-Time: 2014-07-01T10:00+02:00[CET] -> +2 **Unexpected**
Winter-UTC-Time: 2014-01-01T08:00Z[UTC]
Winter-CET-Time: 2014-01-01T09:00+01:00[CET] -> +1 Expected
Run Code Online (Sandbox Code Playgroud)
显然我必须处理夏令时,这是我在使用CET时没想到的.是java.timeCET真理CEST?如果是的话,如果我需要CET,我应该使用哪个区域?
如何解析ZoneDateTime不包含的字符串zone和其他字段?
这是Spock中的测试重现:
import spock.lang.Specification
import spock.lang.Unroll
import java.time.ZoneId
import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
@Unroll
class ZonedDateTimeParsingSpec extends Specification {
def "DateTimeFormatter.ISO_ZONED_DATE_TIME parsing incomplete date: #value #expected"() {
expect:
ZonedDateTime.parse(value, DateTimeFormatter.ISO_ZONED_DATE_TIME) == expected
where:
value | expected
'2014-04-23T04:30:45.123Z' | ZonedDateTime.of(2014, 4, 23, 4, 30, 45, 123_000_000, ZoneOffset.UTC)
'2014-04-23T04:30:45.123+01:00' | ZonedDateTime.of(2014, 4, 23, 4, 30, 45, 123_000_000, ZoneOffset.ofHours(1))
'2014-04-23T04:30:45.123' | ZonedDateTime.of(2014, 4, 23, 4, 30, 45, 123_000_000, ZoneId.systemDefault())
'2014-04-23T04:30' | ZonedDateTime.of(2014, 4, 23, 4, 30, 0, 0, …Run Code Online (Sandbox Code Playgroud) 我现在创建日期:
ZoneId gmt = ZoneId.of("GMT");
LocalDateTime localDateTime = LocalDateTime.now();
LocalDate localDateNow = localDateTime.toLocalDate();
Run Code Online (Sandbox Code Playgroud)
然后我希望以毫秒为单位返回此日期:
localDateNow.atStartOfDay(gmt) - 22.08.2017
localDateNow.atStartOfDay(gmt).toEpochSecond(); - 1503360000 (18.01.70)
Run Code Online (Sandbox Code Playgroud)
我怎么能LocalDate.now()在几毫秒内返回?
我的程序正在将输入字符串解析为LocalDate对象.在大多数情况下,字符串看起来像30.03.2014,但偶尔看起来像3/30/2014.根据具体情况,我需要使用不同的模式来调用DateTimeFormatter.ofPattern(String pattern).基本上,我需要检查字符串是否与模式匹配,dd.MM.yyyy或者M/dd/yyyy在进行解析之前.
正则表达式的方法类似于:
LocalDate date;
if (dateString.matches("^\\d?\\d/\\d{2}/\\d{4}$")) {
date = LocalDate.parse(dateString, DateTimeFormatter.ofPattern("M/dd/yyyy"));
} else {
date = LocalDate.parse(dateString, DateTimeFormatter.ofPattern("dd.MM.yyyy"));
}
Run Code Online (Sandbox Code Playgroud)
这有效,但在匹配字符串时使用日期模式字符串会很好.
有没有任何标准方法可以使用新的Java 8时间API执行此操作,而无需使用正则表达式匹配?我查看过文档,DateTimeFormatter但找不到任何内容.
在Java 8时代/ JSR 310/threeten.org backport ...
问:如何在日期部分和时间部分之间用"T"或"空格"解析ISO-8601时间戳?
时间戳的ISO-8601格式指定文本"T"以将日期规范与时间规范分开:
2015-05-12T15:42:00.123
Run Code Online (Sandbox Code Playgroud)
许多应用程序以这种格式生成时间戳,空格为'char而不是'T'.参考文献表明ISO-8601通过相互协议允许这一点.
2015-05-12 15:42:00.123
Run Code Online (Sandbox Code Playgroud)
因为我从多个来源摄取数据,我想要允许'T'或空格''.
我观察到模式字符串允许指定可选组件,但我没有看到任何方式在模式字符串中指定"选择"...
问:有没有办法在JSR 310模式字符串中"选择一个完全一个"?
我能够通过构造一个带有两个可选模式的DateTimeFormatter来实现这个目的:
DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
.appendOptional(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.optionalStart().appendPattern("yyyy-MM-dd HH:mm:ss.SSS").optionalEnd()
.toFormatter();
Run Code Online (Sandbox Code Playgroud)
看到optionalStart()和optionalEnd()导致我:
DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
.appendPattern("[yyyy-MM-dd HH:mm:ss.SSS][yyyy-MM-dd'T'HH:mm:ss.SSS]")
.toFormatter();
Run Code Online (Sandbox Code Playgroud)
但是,我不相信这是处理这种情况的正确方法......
问:处理多个DateTime模式的最佳实践解决方案是什么?
提前致谢.
在Joda-Time库中,DateTime该类提供了withTimeAtStartOfDay获取当天第一时刻的方法.你可能会认为那个时刻是"午夜".第一个时刻通常是时间,00:00:00.000但并非总是如此.
在Java 8及更高版本中找到的java.time包是否具有等效功能?
java.time中的Period类只处理面向日期的药水:年,月,日.
时间部分怎么样:小时,分钟,秒?
我们如何解析和生成ISO 8601中定义的完整周期的字符串表示,PnYnMnDTnHnMnS?例如,一天半:P1DT12H.学年是九个月P9M.每年我都会有两周零三天的假期P17D.顾客占用了酒店房间2天17个半小时P2DT17H30M.
我正在尝试学习新的Date&Time API.我的代码工作除了最后一行:
LocalDate current=LocalDate.now();
System.out.println(current);
LocalDate personaldate=LocalDate.of(2011,Month.AUGUST, 15);
System.out.println(personaldate);
LocalDate afterten=current.plus(Period.ofDays(10));
System.out.println(afterten);
// error occurs here
System.out.println(afterten.plus(Duration.ofDays(3)));
Run Code Online (Sandbox Code Playgroud)
当我尝试以天为单位添加持续时间时,会生成错误.谁能帮我理解为什么?
错误:
Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Seconds
at java.time.LocalDate.plus(LocalDate.java:1241)
at java.time.LocalDate.plus(LocalDate.java:137)
at java.time.Duration.addTo(Duration.java:1070)
at java.time.LocalDate.plus(LocalDate.java:1143)
at TestClass.main(TestClass.java:15)
Run Code Online (Sandbox Code Playgroud) 假设我有一个 ZonedDateTime:
ZonedDateTime zonedDateTime =
ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("US/Pacific"));
Run Code Online (Sandbox Code Playgroud)
我想知道它是在柏林的哪个日期/时间。我有两种方法:
zonedDateTime.withZoneSameInstant(ZoneId.of("Europe/Berlin")); // probably this is the right one to get the corresponding date/time in Berlin
zonedDateTime.withZoneSameLocal(ZoneId.of("Europe/Berlin"));
Run Code Online (Sandbox Code Playgroud)
该withZoneSameLocal方法的文档说:“仅当本地日期时间对新区域无效时才会更改......”并且不清楚何时真的会发生(任何示例?=))。
它们各自代表哪个日期/时间,有什么区别?