반응형
input: 20191024194947087 : length 17
substring: 20191024194947: length 14
output: 2019/04/24 19:49:47
@Test
public void dateTimeFormat() {
String input = "20191024194947087";
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
LocalDateTime ldt = LocalDateTime.parse(input.substring(0, 14), f);
String format = ldt.format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"));
Assert.assertEquals("2019/10/24 19:49:47", format);
}
// result
// 2019/10/24 19:49:47
input: 20191024194947087 : length 17
substring: 20191024194947 : length 14
output: 2019/10/24
@Test
public void dateFormat() {
String input = "20191024194947087";
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
LocalDateTime ldt = LocalDateTime.parse(input.substring(0, 14), f);
String format = ldt.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
Assert.assertEquals("2019/10/24", format);
}
// result
// 2019/10/24
2020.04.17 -> format (yyyy-MM-dd) => 2020-04-17
2020.04.17 -> format (yyyy/MM/dd) => 2020/04/17
private String convertDateFormat(String date, String format) {
return LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy.MM.dd")).format(DateTimeFormatter.ofPattern(format));
}
date: 2020-04-17
before: yyyy-MM-dd
after: yyyyMMdd000000
result: 20200417000000
private String convertDateFormat(String date, String before, String after) {
return LocalDate.parse(date, DateTimeFormatter.ofPattern(before)).format(DateTimeFormatter.ofPattern(after));
}
반응형
'Java' 카테고리의 다른 글
실습으로 확인하는 tomcat server vs netty server (0) | 2022.07.12 |
---|---|
error rsocket run on mac os (1) | 2022.06.11 |
gradle docker image 생성하기: spring boot (0) | 2021.01.25 |
java isDigits, number parse exception 처리 (0) | 2020.05.11 |
Java LocalDateTime으로 시간 체크 (0) | 2020.02.10 |