반응형

Date

format

Date date = new Date(2020 - 1900, 12 - 1, 31, 23, 59, 59);

assertEquals("2020년 12월 31일 목요일", DateFormat.getDateInstance(DateFormat.FULL).format(date));
assertEquals("2020년 12월 31일", DateFormat.getDateInstance(DateFormat.LONG).format(date));
assertEquals("2020. 12. 31.", DateFormat.getDateInstance(DateFormat.MEDIUM).format(date));
assertEquals("20. 12. 31.", DateFormat.getDateInstance(DateFormat.SHORT).format(date));
assertEquals("목 12월 31", new SimpleDateFormat("E MMM dd", Locale.KOREA).format(date));
assertEquals("Thu Dec 31", new SimpleDateFormat("E MMM dd", Locale.US).format(date));
assertEquals("2020-12-31 23:59:59:000", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(date));

compare

Date date = new Date(2020 - 1900, 12 - 1, 31, 23, 59, 59);
Date now = new Date();

assertTrue(now.after(date));
assertTrue(date.before(now));
assertFalse(date.before(date));

LocalDateTime

format

LocalDateTime date = LocalDateTime.of(2020, 12, 31, 23, 59, 59);

assertEquals("2020-12-31", date.format(DateTimeFormatter.ISO_LOCAL_DATE));
assertEquals("23:59:59", date.format(DateTimeFormatter.ISO_LOCAL_TIME));
assertEquals("2020-12-31T23:59:59", date.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
assertEquals("목 12월 31", date.format(DateTimeFormatter.ofPattern("E MMM dd", Locale.KOREA)));
assertEquals("Thu Dec 31", date.format(DateTimeFormatter.ofPattern("E MMM dd", Locale.US)));
assertEquals("2020-12-31 23:59:59:000", date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS")));

parse

LocalDateTime date1 = LocalDateTime.of(2020, 12, 31, 23, 59, 59);
LocalDateTime date2 = LocalDateTime.parse("2020-12-31T23:59:59");
LocalDateTime date3 = LocalDateTime.parse("2020-12-31 23:59:59", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

assertEquals("2020-12-31 23:59:59", date1.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
assertEquals("2020-12-31 23:59:59", date2.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
assertEquals("2020-12-31 23:59:59", date3.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

change timezone

LocalDateTime date1 = LocalDateTime.parse("2020-12-31 23:59:59", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
LocalDateTime date2 = date1.atZone(ZoneId.of("Asia/Seoul")).withZoneSameInstant(ZoneId.of("America/New_York")).toLocalDateTime();

assertEquals("2020-12-31 23:59:59", date1.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
assertEquals("2020-12-31 09:59:59", date2.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

change time

LocalDateTime date = LocalDateTime.parse("2020-12-31 23:59:59", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

assertEquals("2021-01-05 23:59:59", date.plusDays(5).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
assertEquals("2020-12-26 23:59:59", date.minusDays(5).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
assertEquals("2020-12-05 23:59:59", date.withDayOfMonth(5).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
assertEquals("2020-12-31 00:00:00", date.truncatedTo(ChronoUnit.DAYS).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

compare

LocalDateTime date1 = LocalDateTime.parse("2020-06-06 12:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
LocalDateTime date2 = LocalDateTime.parse("2020-12-31 23:59:59", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Period period = Period.between(date1.toLocalDate(), date2.toLocalDate());
Duration duration = Duration.between(date1.toLocalTime(), date2.toLocalTime());

assertTrue(date1.isBefore(date2));
assertTrue(date2.isAfter(date1));
assertEquals(0, period.getYears());
assertEquals(6, period.getMonths());
assertEquals(25, period.getDays());
assertEquals(43199, duration.getSeconds());
assertEquals(0, duration.getNano());
assertEquals(208, ChronoUnit.DAYS.between(date1.toLocalDate(), date2.toLocalDate()));

convert Date to LocalDateTime

Date date = new Date(2020 - 1900, 12 - 1, 31, 23, 59, 59);
LocalDateTime dateTime1 = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
LocalDateTime dateTime2 = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();

assertEquals("2020-12-31 23:59:59", dateTime1.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
assertEquals("2020-12-31 23:59:59", dateTime2.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

convert LocalDateTime To Date

LocalDateTime dateTime = LocalDateTime.parse("2020-12-31 23:59:59", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Date date = Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());

assertEquals("2020-12-31 23:59:59", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
반응형

'Development > Java' 카테고리의 다른 글

[Java] Stream  (0) 2021.09.09
[Java] Shell Command 실행하기  (0) 2021.08.28
[Java] CompletableFuture  (0) 2021.03.30
[Java] Annotation Processor  (0) 2021.03.04
[Java] Reflection  (0) 2021.03.03

+ Recent posts