항해99/Java 문법 뽀개기

자바문법뽀개기(15) - 날짜와 시간 다루기

숲별 2022. 9. 27. 16:25
728x90

이제 문법은 끝이고 활용들 배우기..

(객체지향, 예외 나중에 다시 한번 복습해야할 듯.)

 

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 usages");
        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() usage");
        LocalDate dateOf = LocalDate.of(2021,3,30);
        LocalTime timeOf = LocalTime.of(22,50,0);

        System.out.println(dateOf);
        System.out.println(timeOf);
    }
}

 

=>now usages
2022-09-27
16:38:57.664
2022-09-27T16:38:57.665   //(마지막은 ms단위까지 표시)

of() usage
2021-03-30
22:50

 

.now() 현재

.of() 특정 날짜나 시간 형식 맞춰서 입력 가능.

 

<날짜 시간 format 수정>

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

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

=>오후 4:50

.SHORT에도 MEDIUM, LONG, FULL이 있다.

근데...쌤이나 ctrl 눌러서 들어간 FormatStyle은 PM으로 나오는데 나는 왜..?

쌤이 다른 건가..?

 

https://formestory.tistory.com/6

 

[Swift] Date() 날짜구하기, DateFormatter() 다루기 오전, 오후 등등

안녕하세요. 오늘은 날짜와 관련된 코드에 대해 포스팅하도록 하겠습니다. 예를 들어 "2020-08-13 16:30" String을 Date 형태로 변환하여 날짜 계산을 한다던지, "2020-08-13 16:30" String을 "2020.08.13 오후 4..

formestory.tistory.com

나는 local 안 썼는데..? 아..ofLocalizedTime이 로컬인 건가..? 그럼 쌤이 언어가 영어인 거..?

 

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter myFomatter = DateTimeFormatter.ofPattern("yyyy년 MM월 dd일");
        String myDate = myFomatter.format(LocalDate.now());
        System.out.println(myDate);
    }
}

=>2022년 09월 27일

내가 지정한 형식으로 사용 가능. MM≠mm

 

<날짜, 시간 차이>

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(2020,1,1);
        Period period = Period.between(today,birthday);
        System.out.println(period.getMonths());
        System.out.println(period.getDays());
    }
}

=>-8
-26

Period와 LocalDate등을 이용하면 우리가 매번 더하고 빼고 안해도 기간을 쉽게 구할 수 있다.

 

<퀴즈>

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

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

=>현재시간: 2022/09/27 5/16