shz.core.time.ToTimestamp Maven / Gradle / Ivy
package shz.core.time;
import shz.core.NullHelp;
import java.sql.Timestamp;
import java.time.*;
import java.util.Date;
public final class ToTimestamp {
private ToTimestamp() {
throw new IllegalStateException();
}
public static Timestamp from(ZonedDateTime zdt) {
return zdt == null ? null : Timestamp.from(zdt.toInstant());
}
public static Timestamp from(LocalDateTime ldt) {
return ldt == null ? null : Timestamp.valueOf(ldt);
}
public static Timestamp from(Instant instant) {
return instant == null ? null : Timestamp.from(instant);
}
public static Timestamp from(Long timestamp) {
return timestamp == null ? null : Timestamp.from(Instant.ofEpochMilli(timestamp));
}
public static Timestamp from(LocalDate ld) {
return ld == null ? null : Timestamp.valueOf(LocalDateTime.of(ld, LocalTime.MIN));
}
public static Timestamp from(Date date) {
return date == null ? null : Timestamp.from(Instant.ofEpochMilli(date.getTime()));
}
static Timestamp from(String s) {
try {
return Timestamp.valueOf(s);
} catch (IllegalArgumentException ignored) {
}
return null;
}
public static Timestamp from(CharSequence cs) {
if (NullHelp.isBlank(cs)) return null;
String s = cs.toString();
Timestamp result;
if ((result = from(s)) != null) return result;
if ((result = from(ToZonedDateTime.from(s))) != null) return result;
if ((result = from(ToLocalDateTime.from(s))) != null) return result;
if ((result = from(ToInstant.from(s))) != null) return result;
if ((result = from(ToLong.from(s))) != null) return result;
if ((result = from(ToLocalDate.from(s))) != null) return result;
return null;
}
public static Timestamp from(Object obj) {
if (obj == null) return null;
if (obj instanceof Timestamp) return (Timestamp) obj;
if (obj instanceof ZonedDateTime) return from((ZonedDateTime) obj);
if (obj instanceof LocalDateTime) return from((LocalDateTime) obj);
if (obj instanceof Instant) return from((Instant) obj);
if (obj instanceof Long) return from((Long) obj);
if (obj instanceof LocalDate) return from((LocalDate) obj);
if (obj instanceof Date) return from((Date) obj);
if (obj instanceof CharSequence) return from((CharSequence) obj);
return null;
}
}