LocalDate nowDate = LocalDate.now(); // 현재 시간을 기준으로 생성
LocalDate ofDate = LocalDate.of(2013, 11, 21); // 특정 날짜를 기준으로 생성한다. 년, 월, 일 입력 가능
ofDate = ofDate.plusDays(10); // 특정일을 더한다. 다양한 plusXxx() 메서드가 존재한다.
LocalTime nowTime = LocalTime.now(); // 현재 시간을 기준으로 생성
LocalTime ofTime = LocalTime.of(9, 10, 30); // 특정 시간을 기준으로 생성한다. 시, 분, 초, 나노초 입력 가능
ofTime = ofTime.plusSeconds(30); // 특정초를 더한다. 다양한 plusXxx() 메서드가 존재한다.
LocalDateTime nowDt = LocalDateTime.now();
LocalDateTime ofDt = LocalDateTime.of(2016, 8, 16, 8, 10, 1);
// 날짜와 시간 분리
LocalDate localDate = ofDt.toLocalDate();
LocalTime localTime = ofDt.toLocalTime();
// 날짜와 시간 합체
LocalDateTime localDateTime = LocalDateTime(localDate, localTime);
// 계산(불변)
LocalDateTime ofDtPlus = ofDt.plusDays(1000); // 1000일 더하기
LocalDateTime ofDtPlus1Year = ofDt.plusYears(1); // 1년 더하기
// 비교
boolean flag = nowDt.isBefore(ofDt)); // 현재 날짜시간이 지정 날짜 시간보다 이전인가? 리턴 true / false
boolean flag = nowDt.isAfter(ofD)); // 현재 날짜시간이 지정 날짜 시간보다 이후인가? 리턴 true / false
boolean flag = nowDt.isEquals(ofDt)); // 현재 날짜시간과 지정 날짜 시간이 같은가? 리턴 true / false
ZonedDateTime nowZdt = ZonedDateTime.now();
LocalDateTime ldt = LocalDateTime.of(2030, 1, 1, 13, 30, 50);
ZonedDateTime zdt1 = ZonedDateTime.of(ldt, ZoneId.of("Asia/Seoul"));
ZonedDateTime zdt2 = ZonedDateTime.of(2030, 1, 1, 13, 30, 50, 0, ZoneId.of("Asia/Seoul"));
// 같은시간의 다른지역 값 구하기
ZonedDateTime utcZdt = zdt2.withZoneSameInstant(ZoneId.of("UTC"));
특정 시점의 시간(시각)
이 프로젝트는 2024년 12월 30일 까지 완료해야해.
다음 회의는 14시 20분에 진행한다.
내 생일은 5월 13일이야.시간의 간격(기간)
앞으로 4년은 더 공부해야 해.
이 프로젝트는 2개월 남았어.
라면은 3분동안 끓여야 해.
package dateTime.period;
import java.time.LocalDate;
import java.time.Period;
public class periodMain {
public static void main(String[] args) {
// 생성
Period period = Period.ofDays(10);
System.out.println("period = " + period);
// 계산에 사용
LocalDate currentDate = LocalDate.of(2030, 1, 1);
LocalDate plusDate = currentDate.plus(period);
System.out.println("plusDate = " + plusDate);
System.out.println("plusDate = " + plusDate);
// 기간 차이
LocalDate startDate = LocalDate.of(2030, 1, 1);
LocalDate endDate = LocalDate.of(2030, 4, 2);
Period between = Period.between(startDate, endDate);
System.out.println("between = " + between);
System.out.println("기간 : " + between.getMonths() + "개월 " + between.getDays() + "일");
}
}
package dateTime.duration;
import java.time.Duration;
import java.time.LocalTime;
public class durationMain {
public static void main(String[] args) {
Duration duration = Duration.ofMinutes(30);
System.out.println("duration = " + duration);
LocalTime lt = LocalTime.of(1, 0);
System.out.println("lt = " + lt);
// 계산에 사용
LocalTime plusTime = lt.plus(duration);
System.out.println("더한 시간 = " + plusTime);
// 시간 차이
LocalTime start = LocalTime.of(9, 0);
LocalTime end = LocalTime.of(10, 0);
Duration between = Duration.between(start, end);
System.out.println("차이 : " + between.getSeconds() + "초");
// between.toMinutesPart() -> 시간계산을 하고 남은 분 (60분을 넘을 수 없음)
// between.toMinutes() -> 전체 시간을 분으로 표현
System.out.println("근무 시간 : " + between.toHours() + "시간 " + between.toMinutesPart() + "분");
System.out.println("근무 시간 : " + between.toMinutes() + "분");
}
}
package dateTime;
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
public class GetTimeMain {
public static void main(String[] args) {
LocalDateTime dt = LocalDateTime.of(2030, 1, 1, 13, 30, 59);
// ChronoField 사용
System.out.println("YEAR = " + dt.get(ChronoField.YEAR));
System.out.println("MONTH = " + dt.get(ChronoField.MONTH_OF_YEAR));
System.out.println("DAY_OF_MONTH = " + dt.get(ChronoField.DAY_OF_MONTH));
System.out.println("HOUR_OF_DAY = " + dt.get(ChronoField.HOUR_OF_DAY));
System.out.println("MINUTE_OF_HOUR = " + dt.get(ChronoField.MINUTE_OF_HOUR));
System.out.println("SECOND_OF_MINUTE = " + dt.get(ChronoField.SECOND_OF_MINUTE));
System.out.println("편의 메소드 제공");
System.out.println("YEAR = " + dt.getYear());
System.out.println("MONTH = " + dt.getMonth());
System.out.println("DAY_OF_MONTH = " + dt.getDayOfMonth());
System.out.println("HOUR_OF_DAY = " + dt.getHour());
System.out.println("MINUTE_OF_HOUR = " + dt.getMinute());
System.out.println("SECOND_OF_MINUTE = " + dt.getSecond());
System.out.println("편의 메소드에 없음");
System.out.println("MINUTE_OF_DAY = " + dt.get(ChronoField.MINUTE_OF_DAY));
System.out.println("SECOND_OF_DAY = " + dt.get(ChronoField.SECOND_OF_DAY));
System.out.println("DAY_OF_WEEK = " + dt.get(ChronoField.DAY_OF_WEEK));
}
}
package dateTime;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.temporal.ChronoUnit;
public class ChangeTimePlusMain {
public static void main(String[] args) {
LocalDateTime dt = LocalDateTime.of(2018, 1, 1, 13, 30, 59);
System.out.println("dt = " + dt);
// ChronoUnit 사용
LocalDateTime plusDt1 = dt.plus(10, ChronoUnit.YEARS);
System.out.println("plusDt1 = " + plusDt1);
LocalDateTime plusDt2 = dt.plusYears(10);
System.out.println("plusDt2 = " + plusDt2);
Period period = Period.ofYears(10);
LocalDateTime plusDt3 = dt.plus(period);
}
}
package dateTime;
import java.time.LocalDate;
import java.time.temporal.ChronoField;
public class IsSupportedMain {
public static void main(String[] args) {
LocalDate now = LocalDate.now();
boolean supported = now.isSupported(ChronoField.SECOND_OF_MINUTE);
System.out.println("supported = " + supported);
if (supported) {
int minute = now.get(ChronoField.SECOND_OF_MINUTE);
System.out.println("minute = " + minute);
}
}
}
package dateTime;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAdjusters;
public class ChangeTimeWithMain {
public static void main(String[] args) {
LocalDateTime dt = LocalDateTime.of(2018, 1, 1, 13, 30, 59);
System.out.println("dt = " + dt);
LocalDateTime changedDt1 = dt.with(ChronoField.YEAR, 2020);
System.out.println("changedDt1 = " + changedDt1);
LocalDateTime changedDt2 = dt.withYear(2020);
System.out.println("changedDt2 = " + changedDt2);
// TemporalAdjuster 사용
// 다음주 금요일
LocalDateTime with1 = dt.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
System.out.println("기준 날짜 : " + dt);
System.out.println("다음 금요일 : " + with1);
// 이번 달의 마지막 일요일
LocalDateTime with2 = dt.with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY));
System.out.println("같은 달의 마지막 일요일 : " + with2);
}
}
package dateTime;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class FormatterMain1 {
public static void main(String[] args) {
// 포맷팅 : 날짜를 문자로
LocalDateTime date = LocalDateTime.of(2024, 12, 31, 13, 30, 59);
System.out.println("date = " + date);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDate = date.format(formatter);
System.out.println("날짜와 시간 포맷팅 = " + formattedDate);
// 파싱 : 문자를 날짜로
String input = "2030-01-01 11:30:00";
LocalDateTime parsedDate = LocalDateTime.parse(input, formatter);
System.out.println("문자열 파싱 날짜와 시간 : " + parsedDate);
}
}