Java转换xml格式时间 (yyyy-MM-ddTHH:mm:ss.SSSZ)

本文介绍了一种将XML格式的日期字符串转换为Java Timestamp的方法。通过使用DatatypeFactory和XMLGregorianCalendar类,可以自动处理时区差异,如XML时间与北京时间之间的8小时时差。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public void xmlDate2Java(String date) {
  DatatypeFactory dtf = null;
  try {
      dtf = DatatypeFactory.newInstance();
  } catch (DatatypeConfigurationException e) {
  }
  Timestamp timestamp = null;
  try {
      XMLGregorianCalendar dateType = dtf.newXMLGregorianCalendar(date);
      timestamp = new Timestamp(dateType.toGregorianCalendar().getTimeInMillis());
  }catch(Exception e) { 
  }
}

 

xml时间是比北京时间早八小时的 这里也会自动处理 自动加8个小时。 

2025-03-06T15:23:49.000Z这是前端传来的rg.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument [0] in public com.xymzsfxy.backend.returncode.Result com.xymzsfxy.backend.controller.admin.ProductController.update(com.xymzsfxy.backend.entity.Product) with 2 errors: [Field error in object ‘product’ on field ‘createdTime’: rejected value [2025-03-06T15:23:49.000Z]; codes [typeMismatch.product.createdTime,typeMismatch.createdTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [product.createdTime,createdTime]; arguments []; default message [createdTime]]; default message [Failed to convert property value of type ‘java.lang.String’ to required type ‘java.util.Date’ for property ‘createdTime’; Failed to convert from type [java.lang.String] to type [@javax.persistence.Column @com.fasterxml.jackson.annotation.JsonFormat java.util.Date] for value [2025-03-06T15:23:49.000Z]]] [Field error in object ‘product’ on field ‘updatedTime’: rejected value [2025-03-06T15:23:49.000Z]; codes [typeMismatch.product.updatedTime,typeMismatch.updatedTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [product.updatedTime,updatedTime]; arguments []; default message [updatedTime]]; default message [Failed to convert property value of type ‘java.lang.String’ to required type ‘java.util.Date’ for property ‘updatedTime’; Failed to convert from type [java.lang.String] to type [@javax.persistence.Column @com.fasterxml.jackson.annotation.JsonFormat java.util.Date] for value [2025-03-06T15:23:49.000Z]]] at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:177) at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:122) at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:179) 怎么修改
最新发布
03-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值