有没有什么简短的方法可以将java.time.ZonedDateTime转换为XMLGregorianCalendar?
也许我需要一些中间步骤,比如将ZonedDateTime转换为java.util.Date,但这会使代码过于混乱.
问题出现在JAX-WS Web服务中,datetime作为XMLGregorianCalendar传递.
我在例子中从字符串中获取毫秒数时遇到麻烦.到目前为止,我已经尝试了这三种不同的方式,示例显示了最新的尝试.似乎总是认为TemporalAccessor不支持ChronoField.如果我可以成功构建一个Instant实例,我可以使用toEpochMilli().
String dateStr = "2014-08-16T05:03:45-05:00"
TemporalAccessor creationAccessor = DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(dateStr);
Instant creationDate = Instant.from(creationAccessor);
Run Code Online (Sandbox Code Playgroud)
请提供简明的答案(不要从头开始构建格式化程序)并仅使用java 8标准发行版(我可以使用Joda,但希望避免依赖项).
编辑:上面代码中的Instant.from抛出:java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {OffsetSeconds=-18000},ISO resolved to 2014-08-16T05:03:45 of type java.time.format.Parsed
我使用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) 我有两个Instant类的实例,java.time例如:
Instant instant1 = Instant.now();
Instant instant2 = Instant.now().plus(5, ChronoUnit.HOURS);
Run Code Online (Sandbox Code Playgroud)
现在我想检查两个实例Instant是否实际上在同一天(日,月和年匹配).我想,让我们只使用闪亮的新的LocalDate和通用的from静态方法:
LocalDate localdate1 = LocalDate.from(instant1);
LocalDate localdate2 = LocalDate.from(instant2);
if (localdate1.equals(localdate2)) {
// All the awesome
}
Run Code Online (Sandbox Code Playgroud)
除了通用from方法不是那么普遍,Java在运行时抱怨异常:
java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: 2014-11-04T18:18:12Z of type java.time.Instant
Run Code Online (Sandbox Code Playgroud)
这让我回到了1号广场.
如果两个实例Instant具有相同的日期(具有相同的日,月和年),建议/最快的方法是什么?
我正在使用这个DateTimeFormatter:
DateTimeFormatter.ofPattern("ddMMYY")
Run Code Online (Sandbox Code Playgroud)
我想解析字符串150790,我得到了这个错误:
Unable to obtain LocalDate from TemporalAccessor: {DayOfMonth=15, MonthOfYear=7, WeekBasedYear[WeekFields[MONDAY,4]]=2090},ISO of type java.time.format.Parsed
Run Code Online (Sandbox Code Playgroud)
显然,我希望得到以下内容TemporalAccessor:
{DayOfMonth=15, MonthOfYear=7, WeekBasedYear=1990}
Run Code Online (Sandbox Code Playgroud)
你知道我为什么得到2090而不是1990年?
谢谢你的帮助
以下代码不打印"CE"或"当前时代":
System.out.println(IsoEra.CE.getDisplayName(TextStyle.SHORT, Locale.UK)); // output: AD
System.out.println(IsoEra.CE.getDisplayName(TextStyle.FULL, Locale.UK)); // output: Anno Domini
Run Code Online (Sandbox Code Playgroud)
当然,IsoEra.CE.name()如果需要像"普通时代"或"当代时代"这样的完整显示名称,则有所帮助.我认为这有点奇怪,因为javadoc IsoEra在其类描述中明确提到术语"当前时代".它甚至不适用于根区域设置.这里的用例是为非宗教背景的客户提供服务.
这也没有帮助:
LocalDate date = LocalDate.now();
String year = date.format(DateTimeFormatter.ofPattern("G yyyy", Locale.UK)); // AD 2015
System.out.println(year);
Run Code Online (Sandbox Code Playgroud)
我找到的唯一方法是:
TextStyle style = ...;
Map<Long,String> eras = new HashMap<>();
long bce = (long) IsoEra.BCE.getValue(); // 0L
long ce = (long) IsoEra.CE.getValue(); // 1L
if (style == TextStyle.FULL) {
eras.put(bce, "Before current era");
eras.put(ce, "Current era");
} else {
eras.put(bce, "BCE");
eras.put(ce, "CE");
}
DateTimeFormatter dtf =
new …Run Code Online (Sandbox Code Playgroud) 我有一个SpringBoot应用程序.使用Spring Initializer,嵌入式Tomcat,Thymeleaf模板引擎和包作为可执行的JAR文件.
我有一个具有2个属性的域对象(initDate,endDate).我想创建2个转换器来处理mySQL DB
@Convert(converter = LocalDateTimeAttributeConverter.class)
private LocalDateTime initDate;
@Convert(converter = ZonedDateTimeAttributeConverter.class)
private ZonedDateTime endDate;
Run Code Online (Sandbox Code Playgroud)
转换器1(可以)
@Converter
public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, Timestamp> {
@Override
public Timestamp convertToDatabaseColumn(LocalDateTime localDateTime) {
return (localDateTime == null ? null : Timestamp.valueOf(localDateTime));
}
@Override
public LocalDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
return (sqlTimestamp == null ? null : sqlTimestamp.toLocalDateTime());
}
}
Run Code Online (Sandbox Code Playgroud)
这是我想要创建的那个
@Converter
public class ZonedDateTimeAttributeConverter implements AttributeConverter<ZonedDateTime, Timestamp> {
@Override
public Timestamp convertToDatabaseColumn(ZonedDateTime zoneDateTime) {
return (zoneDateTime == null ? null : Timestamp.valueOf(zoneDateTime));
} …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()在几毫秒内返回?
我正在尝试将字符串转换为LocalDateTime对象.
@Test
public void testDateFormat() {
String date = "20171205014657111";
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
LocalDateTime dt = LocalDateTime.parse(date, formatter);
}
Run Code Online (Sandbox Code Playgroud)
我希望这个测试能够通过.
我收到以下错误:
java.time.format.DateTimeParseException:无法在索引0处解析文本'20171205014657111'