jdk8의datetime 시간 함수 사용 예시
package datetime;
import static java.time.temporal.TemporalAdjusters.lastDayOfMonth;
import static java.time.temporal.TemporalAdjusters.previousOrSame;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.OffsetTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
public class DataTimes {
public static void main(String[] args) {
//
LocalDateTime timePoint = LocalDateTime.now(); //
System.out.println("-- ----");
System.out.println(timePoint);
System.out.println("");
System.out.println("-- ----");
System.out.println(LocalDate.of(2012, Month.DECEMBER, 12)); // from
// values
System.out.println(LocalDate.ofEpochDay(150)); // middle of 1970
System.out.println(LocalTime.of(17, 18)); // the train I took home today
System.out.println(LocalTime.parse("10:15:30")); // From a String
System.out.println("");
System.out.println("-- ----");
System.out.println(timePoint.toLocalDate());
System.out.println(timePoint.getMonth());
System.out.println(timePoint.getDayOfMonth());
System.out.println(timePoint.getSecond());
System.out.println("");
System.out.println("--- ---");
LocalDateTime thePast = timePoint.withDayOfMonth(10).withYear(2010);
System.out.println(thePast);
System.out.println("--- 3 ---");
LocalDateTime yetAnother = thePast.plusWeeks(3).plus(3,
ChronoUnit.WEEKS);
System.out.println(yetAnother);
System.out.println("");
System.out.println("--- ---");
System.out.println(timePoint);
System.out.println(timePoint.with(lastDayOfMonth()));
System.out.println(timePoint.with(previousOrSame(DayOfWeek.WEDNESDAY)));
System.out.println(timePoint.with(LocalTime.now()));
System.out.println("");
System.out.println("--- --");
System.out.println(timePoint);
LocalDateTime truncatedTimeToMinutes = timePoint
.truncatedTo(ChronoUnit.MINUTES);
System.out.println(truncatedTimeToMinutes);
LocalDateTime truncatedTimeToSeconds = timePoint
.truncatedTo(ChronoUnit.SECONDS);
System.out.println(truncatedTimeToSeconds);
System.out.println("");
System.out.println("--- ---");
ZonedDateTime zoned_now = ZonedDateTime.of(LocalDateTime.now(),
ZoneId.of("UTC+08:00"));
System.out
.println(zoned_now.withZoneSameInstant(ZoneId.of("UTC+00:00")));
System.out.println(zoned_now.getOffset());
System.out.println("");
System.out.println("--- ---");
OffsetTime time = OffsetTime.now();
ZoneOffset offset = ZoneOffset.of("+02:00");
System.out.println(time);
System.out.println(time.withOffsetSameInstant(offset));
System.out.println("");
System.out.println("--- ---");
timePoint = LocalDateTime.now();
System.out.println(timePoint);
// 3 years, 2 months, 1 day
Period period1 = Period.of(3, 2, 1);
System.out.println(timePoint.plus(period1));
Duration duration = Duration.ofSeconds(3, 5);
System.out.println(timePoint.plus(duration));
Duration sixHours = Duration.between(
ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC+08:00")),
ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC+02:00")));
System.out.println(timePoint.plus(sixHours));
System.out.println("");
}
}
실행 결과
-- ----
2014-03-25T17:03:40.553
-- ----
2012-12-12
1970-05-31
17:18
10:15:30
-- ----
2014-03-25
MARCH
25
40
--- ---
2010-03-10T17:03:40.553
--- 3 ---
2010-04-21T17:03:40.553
--- ---
2014-03-25T17:03:40.553
2014-03-31T17:03:40.553
2014-03-19T17:03:40.553
2014-03-25T17:03:40.583
--- --
2014-03-25T17:03:40.553
2014-03-25T17:03
2014-03-25T17:03:40
--- ---
2014-03-25T09:03:40.583Z[UTC]
+08:00
--- ---
17:03:40.585+08:00
11:03:40.585+02:00
--- ---
2014-03-25T17:03:40.585
2017-05-26T17:03:40.585
2014-03-25T17:03:43.585000005
2014-03-25T23:03:40.586
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
jdk8의datetime 시간 함수 사용 예시JDK8이 발표되었습니다.datetime 시간 함수 사용 방법의 작은 예시를 썼습니다 실행 결과...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.