Java_문법뽀개기 05

5w31892p·2022년 11월 15일
0

Java

목록 보기
6/17

📜 Java

:: ✍ 날짜와 시간 다루기

:: Java에서의 날짜와 시간

  • java.time package
    • 날짜와 시간을 사용할 때 자주 쓰임
패키지(package)란?
클래스의 묶음

LocalDate & LocalTime

  • java.time 패키지의 가장 기본이 되는 클래스

now( )

  • 객체 생성할 때 사용
  • 현재 날짜 시간이 필드에 담김
  • 즉, 현재시간이나 날짜를 찍고 싶으면 .now( )

of( )

  • 객체 생성할 때 사용
  • 지정하는 값이 필드에 담김
  • 즉, 특정 시간이나 날짜 지정하고 싶으면 .of( )
  • 현재시간이나 날짜를 찍고 싶으면 .now( )
  • 특정 시간이나 날짜 지정하고 싶으면 .of( )
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class Main {
    public static void main(String[] args) {
        System.out.println("now()를 활용하여 생성");
        LocalDate date = LocalDate.now();
        LocalTime time = LocalTime.now();
        LocalDateTime dateTime = LocalDateTime.now();

        System.out.println(date);
        System.out.println(time);
        System.out.println(dateTime);

        System.out.println("of()를 활용하여 생성");
        LocalDate newDate = LocalDate.of(2022, 10, 31);
        LocalTime newTime = LocalTime.of(9, 00, 20); // hour는 09시 08시 이런식으로 쓰면 안됨

        System.out.println(newDate);
        System.out.println(newTime);
    }
}
now()를 활용하여 생성
2022-11-15
19:57:13.601
2022-11-15T19:57:13.601
of()를 활용하여 생성
2022-10-31
09:00:20

:: 날짜와 시간의 형식 수정

  • formatter
    • 원하는 형식으로 출력
    • LocalDate.now( ) 를 했을 때와는 다르게 오전/오후가 추가되었으며 보다 직관적인 형태
  1. formatter 먼저 설정
  2. fommater.format에다 넣고 싶은 시간, 변수 등 넣기
  • .ofLocalizedTime(FormatStyle. ); 사용
FormatStyle 

FULL
Tuesday, April 12, 1952 AD or 3:30:42pm PST

LONG
January 12, 1952

MEDIUM
Jan 12, 1952

SHORT
12.13.52 or 3:30pm
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
        String shortFormat = formatter.format(LocalTime.now());
        System.out.println(shortFormat);
    }
}
오후 7:58

FormatStyle 쓰임새

  • 내가 사용하고자 하는 형식이 없거나 따로 생각한 형식이 있을 경우
  • .ofPattern("원하는 형태"); 사용
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter myFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd"); 

        // 소운자 yyyy 년
        // 대문자 MM 월 (소문자 mm X)
        // 소문자 dd 일

        String myDate = myFormatter.format(LocalDate.now());
        System.out.println(myDate);
    }
}
2022/11/15

ofPattern("yyyy년 MM월 dd일");
2022년 11월 15일

:: 날짜와 시간의 차이 계산

  • between( )
    • 날짜 차이를 계산
  • until( )로도 구할 수 있음
  • 미리 정의되어 있는 period 이용
  • LocalDate 나 LocalTime, LocalDateTime 사용하면 쉽게 기간 구할 수 있음
import java.time.LocalDate;
import java.time.Period;

public class Main {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        LocalDate birthday = LocalDate.of(2022, 2, 9);
        Period period = Period.between(today, birthday);
        System.out.println(period.getMonths());
        System.out.println(period.getDays());
    }
}
-9 : 현재 월 - 지정한 월의 값
-6 : 현재 일 - 지정한 일의 값

:: 퀴즈

오늘의 날짜와 시간을 [연도/월/일 시간/일자]의 형식으로 값으로 출력

  • 원하는 형식 적기
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd h/mm");
        String now = dateTimeFormatter.format(LocalDateTime.now());
        System.out.println("현재시간: " + now);
    }
}
현재시간: 2022/11/15 8/29

0개의 댓글