// 서울 시간
const timeStamp = new Date(new Date().getTime() + 9 * 60 * 60 * 1000)
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")
.withZone(ZoneId.of("Asia/Seoul"));
LocalDateTime ldt = LocalDateTime.now(); // 2024-07-05T09:18:29.417174500
String now = formatter.format(ldt); // 2024-07-05 09:18:29.417
public void setTtdGateElapsedTime(Timestamp timestamp) {
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
this.ttdGateElapsedTime =
timestamp.toLocalDateTime().format(formatter);
}
TimeStamp -> Json 변환
com.fasterxml.jackson.databind.JsonMappingException 발생
// String > Timestamp
private static Timestamp convertStringToTimestamp(String dateString) {
SimpleDateFormat dateFormat =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
java.util.Date parsedDate = null;
try {
parsedDate = dateFormat.parse(dateString);
} catch (ParseException e) {
throw new RuntimeException(e);
}
return new Timestamp(parsedDate.getTime());
}
// ORM, Timestamp > LocalDateTime > String > Json
list.stream().forEach(item -> {
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")
withZone(ZoneId.of("Asia/Seoul"));
Timestamp timestamp =
convertStringToTimestamp(item.get("TTD_TIMESTAMP").toString());
LocalDateTime ldt = timestamp.toLocalDateTime();
item.put("TTD_TIMESTAMP", formatter.format(ldt));
});
Instant now = Instant.now();
// now = 2024-07-05T00:29:02.705537700Z
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")
.withZone(ZoneId.of("UTC"));
String timestampString = formatter.format(now);
// timestampString = 2024-07-05 00:29:02.705
ISO 8601 형식의 UTC 시간을 Timestamp 타입으로 변환
SELECT TO_TIMESTAMP('2024-07-02T08:37:29.352Z', 'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')
FROM DUAL;
-- 2024-07-02 08:37:29.352
public boolean isValidDate(String dateStr) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
try {
LocalDate date = LocalDate.parse(dateStr, formatter);
return true;
} catch (DateTimeParseException e) {
return false;
}
}
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateConverter {
public static void main(String[] args) {
String dateString = "20230317";
Timestamp timestamp = convertStringToTimestamp(dateString);
System.out.println("Original String: " + dateString);
System.out.println("Converted Timestamp: " + timestamp);
}
public static Timestamp convertStringToTimestamp(String dateString) {
// 문자열을 LocalDate로 변환
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate localDate = LocalDate.parse(dateString, dateFormatter);
// LocalDate를 LocalDateTime으로 변환
LocalDateTime localDateTime = localDate.atStartOfDay();
// LocalDateTime을 Timestamp로 변환
Timestamp timestamp = Timestamp.valueOf(localDateTime);
return timestamp;
}
}
public static String toDotDate(String src) {
DateTimeFormatter OUT = DateTimeFormatter.ofPattern("yyyy.MM.dd");
DateTimeFormatter IN_DATE = new DateTimeFormatterBuilder()
.parseStrict()
.appendPattern("uuuuMMdd")
.toFormatter()
.withResolverStyle(ResolverStyle.STRICT);
DateTimeFormatter IN_DATETIME = new DateTimeFormatterBuilder()
.parseStrict()
.appendPattern("uuuuMMddHHmmss")
.toFormatter()
.withResolverStyle(ResolverStyle.STRICT);
String s = src.trim();
switch (s.length()) {
case 8: { // yyyyMMdd
LocalDate d = LocalDate.parse(s, IN_DATE);
return OUT.format(d);
}
case 14: { // yyyyMMddHHmmss
LocalDateTime dt = LocalDateTime.parse(s, IN_DATETIME);
return OUT.format(dt.toLocalDate());
}
default:
return s;
}
}