mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add method
This commit is contained in:
parent
ccb7b61b8c
commit
9878264882
@ -25,6 +25,7 @@ import cn.hutool.core.convert.impl.TimeZoneConverter;
|
||||
import cn.hutool.core.convert.impl.URIConverter;
|
||||
import cn.hutool.core.convert.impl.URLConverter;
|
||||
import cn.hutool.core.convert.impl.UUIDConverter;
|
||||
import cn.hutool.core.convert.impl.XMLGregorianCalendarConverter;
|
||||
import cn.hutool.core.convert.impl.ZoneIdConverter;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.lang.Opt;
|
||||
@ -32,6 +33,7 @@ import cn.hutool.core.reflect.ClassUtil;
|
||||
import cn.hutool.core.reflect.TypeUtil;
|
||||
import cn.hutool.core.util.ServiceLoaderUtil;
|
||||
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
import java.io.Serializable;
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.lang.ref.WeakReference;
|
||||
@ -183,6 +185,7 @@ public class RegisterConverter implements Converter, Serializable {
|
||||
|
||||
// 日期时间
|
||||
defaultConverterMap.put(Calendar.class, new CalendarConverter());
|
||||
defaultConverterMap.put(XMLGregorianCalendar.class, new XMLGregorianCalendarConverter());
|
||||
defaultConverterMap.put(java.util.Date.class, DateConverter.INSTANCE);
|
||||
defaultConverterMap.put(DateTime.class, DateConverter.INSTANCE);
|
||||
defaultConverterMap.put(java.sql.Date.class, DateConverter.INSTANCE);
|
||||
|
@ -4,6 +4,7 @@ import cn.hutool.core.convert.AbstractConverter;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
@ -50,6 +51,10 @@ public class CalendarConverter extends AbstractConverter {
|
||||
return DateUtil.calendar((Long)value);
|
||||
}
|
||||
|
||||
if(value instanceof XMLGregorianCalendar){
|
||||
return DateUtil.calendar((XMLGregorianCalendar) value);
|
||||
}
|
||||
|
||||
final String valueStr = convertToStr(value);
|
||||
return DateUtil.calendar(StrUtil.isBlank(format) ? DateUtil.parse(valueStr) : DateUtil.parse(valueStr, format));
|
||||
}
|
||||
|
@ -0,0 +1,87 @@
|
||||
package cn.hutool.core.convert.impl;
|
||||
|
||||
import cn.hutool.core.convert.AbstractConverter;
|
||||
import cn.hutool.core.convert.ConvertException;
|
||||
import cn.hutool.core.date.DateException;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
|
||||
import javax.xml.datatype.DatatypeConfigurationException;
|
||||
import javax.xml.datatype.DatatypeFactory;
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
/**
|
||||
* 日期转换器
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class XMLGregorianCalendarConverter extends AbstractConverter {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 日期格式化 */
|
||||
private String format;
|
||||
private final DatatypeFactory datatypeFactory;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public XMLGregorianCalendarConverter(){
|
||||
try {
|
||||
datatypeFactory = DatatypeFactory.newInstance();
|
||||
} catch (final DatatypeConfigurationException e) {
|
||||
throw new DateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取日期格式
|
||||
*
|
||||
* @return 设置日期格式
|
||||
*/
|
||||
public String getFormat() {
|
||||
return format;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置日期格式
|
||||
*
|
||||
* @param format 日期格式
|
||||
*/
|
||||
public void setFormat(final String format) {
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected XMLGregorianCalendar convertInternal(final Class<?> targetClass, final Object value) {
|
||||
if(value instanceof GregorianCalendar){
|
||||
return datatypeFactory.newXMLGregorianCalendar((GregorianCalendar) value);
|
||||
}
|
||||
|
||||
final GregorianCalendar gregorianCalendar = new GregorianCalendar();
|
||||
// Handle Date
|
||||
if (value instanceof Date) {
|
||||
gregorianCalendar.setTime((Date) value);
|
||||
} else if(value instanceof Calendar){
|
||||
final Calendar calendar = (Calendar) value;
|
||||
gregorianCalendar.setTimeZone(calendar.getTimeZone());
|
||||
gregorianCalendar.setFirstDayOfWeek(calendar.getFirstDayOfWeek());
|
||||
gregorianCalendar.setLenient(calendar.isLenient());
|
||||
gregorianCalendar.setTimeInMillis(calendar.getTimeInMillis());
|
||||
}else if (value instanceof Long) {
|
||||
gregorianCalendar.setTimeInMillis((Long) value);
|
||||
} else{
|
||||
final String valueStr = convertToStr(value);
|
||||
final Date date = StrUtil.isBlank(format) ? DateUtil.parse(valueStr) : DateUtil.parse(valueStr, format);
|
||||
if(null == date){
|
||||
throw new ConvertException("Unsupported date value: " + value);
|
||||
}
|
||||
gregorianCalendar.setTime(date);
|
||||
}
|
||||
return datatypeFactory.newXMLGregorianCalendar(gregorianCalendar);
|
||||
}
|
||||
|
||||
}
|
@ -9,6 +9,7 @@ import cn.hutool.core.date.format.parser.PositionDateParser;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
import java.text.ParsePosition;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
@ -50,6 +51,16 @@ public class CalendarUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为Calendar对象
|
||||
*
|
||||
* @param calendar 日期对象
|
||||
* @return Calendar对象
|
||||
*/
|
||||
public static Calendar calendar(final XMLGregorianCalendar calendar) {
|
||||
return calendar.toGregorianCalendar();
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为Calendar对象,使用当前默认时区
|
||||
*
|
||||
|
@ -17,6 +17,7 @@ import cn.hutool.core.regex.PatternPool;
|
||||
import cn.hutool.core.regex.ReUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Instant;
|
||||
@ -78,7 +79,7 @@ public class DateUtil extends CalendarUtil {
|
||||
* {@link Date}类型时间转为{@link DateTime}<br>
|
||||
* 如果date本身为DateTime对象,则返回强转后的对象,否则新建一个DateTime对象
|
||||
*
|
||||
* @param date Long类型Date(Unix时间戳)
|
||||
* @param date {@link Date}
|
||||
* @return 时间对象
|
||||
* @since 3.0.7
|
||||
*/
|
||||
@ -89,6 +90,17 @@ public class DateUtil extends CalendarUtil {
|
||||
return dateNew(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link XMLGregorianCalendar}类型时间转为{@link DateTime}
|
||||
*
|
||||
* @param date {@link XMLGregorianCalendar}
|
||||
* @return 时间对象
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static DateTime date(final XMLGregorianCalendar date) {
|
||||
return date(date.toGregorianCalendar());
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据已有{@link Date} 产生新的{@link DateTime}对象
|
||||
*
|
||||
|
@ -0,0 +1,16 @@
|
||||
package cn.hutool.core.convert;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
|
||||
public class XMLGregorianCalendarConverterTest {
|
||||
|
||||
@Test
|
||||
public void convertTest(){
|
||||
final XMLGregorianCalendar calendar = Convert.convert(XMLGregorianCalendar.class, DateUtil.parse("2022-01-03 04:00:00"));
|
||||
Assert.assertNotNull(calendar);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user