mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add XXXConvert
This commit is contained in:
parent
cb7912717e
commit
10de9f955c
@ -13,11 +13,14 @@ import cn.hutool.core.convert.impl.ClassConverter;
|
||||
import cn.hutool.core.convert.impl.CollectionConverter;
|
||||
import cn.hutool.core.convert.impl.CurrencyConverter;
|
||||
import cn.hutool.core.convert.impl.DateConverter;
|
||||
import cn.hutool.core.convert.impl.DurationConverter;
|
||||
import cn.hutool.core.convert.impl.EnumConverter;
|
||||
import cn.hutool.core.convert.impl.LocaleConverter;
|
||||
import cn.hutool.core.convert.impl.MapConverter;
|
||||
import cn.hutool.core.convert.impl.NumberConverter;
|
||||
import cn.hutool.core.convert.impl.OptionalConverter;
|
||||
import cn.hutool.core.convert.impl.PathConverter;
|
||||
import cn.hutool.core.convert.impl.PeriodConverter;
|
||||
import cn.hutool.core.convert.impl.PrimitiveConverter;
|
||||
import cn.hutool.core.convert.impl.ReferenceConverter;
|
||||
import cn.hutool.core.convert.impl.StackTraceElementConverter;
|
||||
@ -43,18 +46,21 @@ import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Path;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.OffsetTime;
|
||||
import java.time.Period;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collection;
|
||||
import java.util.Currency;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.TimeZone;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@ -385,6 +391,8 @@ public class ConverterRegistry implements Serializable{
|
||||
defaultConverterMap.put(ZonedDateTime.class, new TemporalAccessorConverter(ZonedDateTime.class));
|
||||
defaultConverterMap.put(OffsetDateTime.class, new TemporalAccessorConverter(OffsetDateTime.class));
|
||||
defaultConverterMap.put(OffsetTime.class, new TemporalAccessorConverter(OffsetTime.class));
|
||||
defaultConverterMap.put(Period.class, new PeriodConverter());
|
||||
defaultConverterMap.put(Duration.class, new DurationConverter());
|
||||
|
||||
// Reference
|
||||
defaultConverterMap.put(WeakReference.class, new ReferenceConverter(WeakReference.class));// since 3.0.8
|
||||
@ -400,6 +408,7 @@ public class ConverterRegistry implements Serializable{
|
||||
defaultConverterMap.put(Currency.class, new CurrencyConverter());// since 3.0.8
|
||||
defaultConverterMap.put(UUID.class, new UUIDConverter());// since 4.0.10
|
||||
defaultConverterMap.put(StackTraceElement.class, new StackTraceElementConverter());// since 4.5.2
|
||||
defaultConverterMap.put(Optional.class, new OptionalConverter());// since 5.0.0
|
||||
|
||||
return this;
|
||||
}
|
||||
|
@ -0,0 +1,30 @@
|
||||
package cn.hutool.core.convert.impl;
|
||||
|
||||
import cn.hutool.core.convert.AbstractConverter;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Period;
|
||||
import java.time.temporal.TemporalAmount;
|
||||
|
||||
/**
|
||||
*
|
||||
* {@link Duration}对象转换器
|
||||
*
|
||||
* @author Looly
|
||||
* @since 5.0.0
|
||||
*/
|
||||
public class DurationConverter extends AbstractConverter<Duration> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
protected Duration convertInternal(Object value) {
|
||||
if(value instanceof TemporalAmount){
|
||||
return Duration.from((TemporalAmount) value);
|
||||
} else if(value instanceof Long){
|
||||
return Duration.ofMillis((Long) value);
|
||||
} else {
|
||||
return Duration.parse(convertToStr(value));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package cn.hutool.core.convert.impl;
|
||||
|
||||
import cn.hutool.core.convert.AbstractConverter;
|
||||
|
||||
import java.time.Period;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
*
|
||||
* {@link Optional}对象转换器
|
||||
*
|
||||
* @author Looly
|
||||
* @since 5.0.0
|
||||
*/
|
||||
public class OptionalConverter extends AbstractConverter<Optional<?>> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
protected Optional<?> convertInternal(Object value) {
|
||||
return Optional.ofNullable(value);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package cn.hutool.core.convert.impl;
|
||||
|
||||
import cn.hutool.core.convert.AbstractConverter;
|
||||
|
||||
import java.time.Period;
|
||||
import java.time.temporal.TemporalAmount;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
*
|
||||
* {@link Period}对象转换器
|
||||
*
|
||||
* @author Looly
|
||||
* @since 5.0.0
|
||||
*/
|
||||
public class PeriodConverter extends AbstractConverter<Period> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
protected Period convertInternal(Object value) {
|
||||
if(value instanceof TemporalAmount){
|
||||
return Period.from((TemporalAmount) value);
|
||||
}else if(value instanceof Integer){
|
||||
return Period.ofDays((Integer) value);
|
||||
} else {
|
||||
return Period.parse(convertToStr(value));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -10,7 +10,6 @@ import java.time.LocalTime;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.OffsetTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
|
@ -217,7 +217,6 @@ public class DateTime extends Date {
|
||||
*
|
||||
* @param dateStr Date字符串
|
||||
* @param formatter 格式化器,{@link DateTimeFormatter}
|
||||
* @return DateTime对象
|
||||
* @since 5.0.0
|
||||
*/
|
||||
public DateTime(CharSequence dateStr, DateTimeFormatter formatter) {
|
||||
|
@ -545,6 +545,21 @@ public class DateUtil {
|
||||
return format.format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据特定格式格式化日期
|
||||
*
|
||||
* @param date 被格式化的日期
|
||||
* @param format {@link SimpleDateFormat}
|
||||
* @return 格式化后的字符串
|
||||
* @since 5.0.0
|
||||
*/
|
||||
public static String format(Date date, DateTimeFormatter format) {
|
||||
if (null == format || null == date) {
|
||||
return null;
|
||||
}
|
||||
return format.format(date.toInstant());
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化日期时间<br>
|
||||
* 格式 yyyy-MM-dd HH:mm:ss
|
||||
|
@ -1,5 +1,6 @@
|
||||
package cn.hutool.core.convert;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@ -15,8 +16,12 @@ public class TemporalAccessorConverterTest {
|
||||
|
||||
@Test
|
||||
public void toInstantTest(){
|
||||
Instant instant = Convert.convert(Instant.class, "2019-02-18");
|
||||
Assert.assertNotNull(instant);
|
||||
String dateStr = "2019-02-18";
|
||||
|
||||
// 通过转换获取的Instant为UTC时间
|
||||
Instant instant = Convert.convert(Instant.class, dateStr);
|
||||
Instant instant1 = DateUtil.parse(dateStr).toInstant();
|
||||
Assert.assertEquals(instant1, instant);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -75,7 +75,7 @@ public class FileUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyTest() throws Exception {
|
||||
public void copyTest() {
|
||||
File srcFile = FileUtil.file("hutool.jpg");
|
||||
File destFile = FileUtil.file("hutool.copy.jpg");
|
||||
|
||||
@ -87,7 +87,7 @@ public class FileUtilTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void copyFilesFromDir() throws Exception {
|
||||
public void copyFilesFromDir() {
|
||||
File srcFile = FileUtil.file("D:\\驱动");
|
||||
File destFile = FileUtil.file("d:\\驱动备份");
|
||||
|
||||
@ -132,14 +132,18 @@ public class FileUtilTest {
|
||||
Assert.assertEquals("/bar", FileUtil.normalize("//server/../bar"));
|
||||
Assert.assertEquals("C:/bar", FileUtil.normalize("C:\\foo\\..\\bar"));
|
||||
Assert.assertEquals("C:/bar", FileUtil.normalize("C:\\..\\bar"));
|
||||
Assert.assertEquals("~/bar/", FileUtil.normalize("~/foo/../bar/"));
|
||||
Assert.assertEquals("bar", FileUtil.normalize("~/../bar"));
|
||||
Assert.assertEquals("bar", FileUtil.normalize("../../bar"));
|
||||
Assert.assertEquals("C:/bar", FileUtil.normalize("/C:/bar"));
|
||||
|
||||
Assert.assertEquals("\\/192.168.1.1/Share/", FileUtil.normalize("\\\\192.168.1.1\\Share\\"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void normalizeHomePathTest() {
|
||||
String home = FileUtil.getUserHomePath().replace('\\', '/');
|
||||
Assert.assertEquals(home + "/bar/", FileUtil.normalize("~/foo/../bar/"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void normalizeClassPathTest() {
|
||||
Assert.assertEquals("", FileUtil.normalize("classpath:"));
|
||||
|
Loading…
x
Reference in New Issue
Block a user