JDK8이 출시되었으며 DateTime 시간 기능을 사용하는 방법에 대한 작은 예를 작성했습니다.
코드 사본은 다음과 같습니다.
패키지 dateTime;
정적 java.time.temporal.temporaladjusters.lastdayofmonth 가져 오기;
정적 java.time.temporal.temporaladjusters.previousorsame 가져 오기;
java.time.dayofweek 가져 오기;
Java.Time.Duration 가져 오기;
java.time.localdate 가져 오기;
java.time.localdateTime 가져 오기;
Java.Time.localTime 가져 오기;
java.time.month import;
java.time.offsettime 가져 오기;
import java.time.period;
java.time.zoneid import;
java.time.zoneoffset import;
java.time.zoneddateTime 가져 오기;
java.time.temporal.chronounit import;
공개 수업 데이터 {
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);
// 값
System.out.println (LocalDate.ofepochday (150));
System.out.println (localtime.of (17, 18));
system.out.println (localtime.parse ( "10:15:30");
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 antanother = 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 (previalorsame (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 (Zoneed_now.withzonesameInstant (ZoneId.of ( "utc+00 : 00")));
System.out.println (Zoned_now.getOffset ());
System.out.println ( "");
System.out.println ( "--- 시간대 오프셋은 시간에 사용됩니다 ---");
오프셋 타임 시간 = offsettime.now ();
ZoneOffset Offset = ZoneOffset.of ( "+02:00");
System.out.println (시간);
System.out.println (time.withOffSetSameInstant (Offset));
System.out.println ( "");
System.out.println ( "--- 시간 추가 및 뺄셈 ---");
timePoint = localDateTime.now ();
System.out.println (TimePoint);
// 3 년, 2 개월, 1 일
기간 1 = 기간 (3, 2, 1);
System.out.println (timepoint.plus (period1));
지속 시간 기간 = 지속 시간 .ofseconds (3, 5);
System.out.println (timepoint.plus (duration));
기간 6 시간 = 기간.
ZonedDateMe.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
3월
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