import java.time.Duration;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DurationFormatUtils;
@Slf4j
public class TimeHelper {
public static final DateTimeFormatter FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS[XXX][X]");
public static Instant parseTimeStamp(String timeStamp) {
return OffsetDateTime.parse(timeStamp, FORMATTER).toInstant();
}
public static String getDuration(String startTimeStamp, String endTimeStamp) {
String duration = "";
try {
duration =
DurationFormatUtils.formatDurationWords(
Duration.between(parseTimeStamp(startTimeStamp), parseTimeStamp(endTimeStamp))
.toMillis(),
true,
true);
} catch (NullPointerException | DateTimeParseException e) {
log.error(e.getMessage(), e);
}
return duration;
}
}
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Optional;
import java.util.Set;
private static final DateTimeFormatter VALUATION_TIMESTAMP_PARSER =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss[.SSS][xxx][xx][X]");
private Instant getTimeStampAsInstant(String timeStamp) {
return Optional.ofNullable(timeStamp)
.map(VALUATION_TIMESTAMP_PARSER::parse)
.map(Instant::from)
.orElse(null);
}
import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.Instant;
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS[xxx][xx][X]")
private Instant valuationTimeStamp;
private static final DateTimeFormatter VALUATION_TIMESTAMP_PARSER =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss[.SSS][xxx][xx][X]");
import java.time.Duration;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DurationFormatUtils;
@Slf4j
public class TimeHelper {
public static final DateTimeFormatter FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS[XXX][X]");
public static Instant parseTimeStamp(String timeStamp) {
return OffsetDateTime.parse(timeStamp, FORMATTER).toInstant();
}
public static String getDuration(String startTimeStamp, String endTimeStamp) {
String duration = "";
try {
duration =
DurationFormatUtils.formatDuration(
(Duration.between(parseTimeStamp(startTimeStamp), parseTimeStamp(endTimeStamp))
.toMillis()),
"d'd' HH'h' mm'm' ss's'");
} catch (NullPointerException | DateTimeParseException e) {
log.error(e.getMessage(), e);
}
return duration;
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章