Timestamp2LocalDateTimeConverter.java 1.05 KB
package cn.fw.hestia.server.converter;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.core.convert.converter.Converter;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * @author : kurisu
 * @className : Timestamp2LocalDateTimeConverter
 * @description : {@link String} to {@link LocalDateTime}
 * @date: 2021-01-19 10:04
 */
@Component
@ConfigurationPropertiesBinding
public class Timestamp2LocalDateTimeConverter implements Converter<String, LocalDateTime> {
    @Override
    @Nullable
    public LocalDateTime convert(@Nullable final String value) {
        if (StringUtils.isNotBlank(value) && NumberUtils.isDigits(value)) {
            return Instant.ofEpochMilli(NumberUtils.toLong(value)).atZone(ZoneId.systemDefault()).toLocalDateTime();
        }
        return null;
    }
}