날짜 시간을 블로그에 정리해두면 나중에 꺼내먹기 좋을것 같다고 생각했는데..

요 항목은 왠지 너무 암기과목 같은 기분이라 손이 잘 안가네..

 

미래의 나를 위해 정리해두긴 해야겠다.

암기과목이라 딱히 주석이 필요는 없을 것 같다.

 

혹시나  임포트 끌고올때,  STS4,이클립스는 [ command + shift + o ] 이다.

인텔리제이는 [ option + enter ]

package thisjavaexam.datetime;

import java.time.LocalDate;

public class LocalDateMain {

	public static void main(String[] args) {
		LocalDate nowDate = LocalDate.now();
		System.out.println("오늘 날짜 : " + nowDate);
		
		LocalDate ofDate = LocalDate.of(2024, 1, 19);
		System.out.println("내가 정한 날짜 : " + ofDate);
		
		LocalDate nowDatePlus = nowDate.plusDays(15);
		System.out.println("오늘 뒤로 15일 뒤 : " + nowDatePlus);
		
		LocalDate ofDatePlus = ofDate.plusDays(15);
		System.out.println("내가 정 날짜 + 15일 : " + ofDatePlus);	
	}
}

 

 

 

 

 

 

이제 LocalTime을 보자 

package thisjavaexam.datetime;

import java.time.LocalTime;

public class LocalTimeMain {

	public static void main(String[] args) {
		LocalTime nowTime = LocalTime.now();
		System.out.println("지금 시간 : " + nowTime);
		
		LocalTime ofTime = LocalTime.of(12, 30);
		System.out.println("지정 시간 : " + ofTime);
		
		LocalTime nowTimePlus = nowTime.plusHours(3).plusMinutes(20);
		System.out.println("지금부터 3시간 20분뒤 : " + nowTimePlus);	
	}
}

 

초가 뭔가 디테일하다...

아쉬운 부분이라고 할수있겠다...

 

 

두개의 합체본은 아래와 같다.

package thisjavaexam.datetime;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class LocalDateTimeMain {

	public static void main(String[] args) {
		LocalDateTime nowDt = LocalDateTime.now();
		System.out.println("현재 시간 날짜 : " + nowDt);
		
		//시간, 날짜만 담는 타입을 LocalDate,LocalTime으로 바꿔야한다. 
		LocalDate onlyDate = nowDt.toLocalDate();
		LocalTime onlyTime = nowDt.toLocalTime();
		System.out.println("지금 시간은 : " + onlyTime + ", 오늘 날짜 : " + onlyDate);
		
		//time, date 순서를 바꾸면 오류발생 
		//LocalDateTime sumTimeDate = LocalDateTime.of(onlyTime, onlyDate);
		LocalDateTime sumDateTime = LocalDateTime.of(onlyDate, onlyTime);
		System.out.println("날짜 시간을 합체 : " + sumDateTime);
		
		System.out.println("===========");
		
		
		LocalDateTime ofDt = LocalDateTime.of(2024,12,24,00,00,01);
		System.out.println("지정 시간 날짜 : " + ofDt);
		
		LocalDateTime ofDtPlus = ofDt.plusYears(1).plusMonths(1).plusDays(28).plusHours(2);
		System.out.println("지정 날짜 + 1년 1개월 28일 2시간뒤 : " + ofDtPlus);
		
		System.out.println("현재 날짜시간이 지정 날짜시간보다 이전인가? " + nowDt.isBefore(ofDt));
		System.out.println("현재 날짜시간이 지정 날짜시간보다 이후인가? " + nowDt.isAfter(ofDt));
		System.out.println("현재 날짜시간과 지정 날짜시간이 같은가? " + nowDt.isEqual(ofDt));	
	}
}

 

 

 

 

기본적으로 이정도만 사용해도 되지않을까 싶다.

꽤나 쉬운편..이지만 까먹을수있으니 남겨둔다.

+ Recent posts