자바 개발하다 보면 가볍게 오늘 날짜를 반환하고 싶을 때 자꾸 까먹곤 하는 사용법
생각해보면 클래스명처럼 간단한 데이트 포멧
Date date = new Date(); // 오늘 날짜를 Date형으로 반환
String format = "yyyy-MM-dd"; // 년(4)-월(2)-일(2) 형태
SimpleDateFormat df = new SimpleDateFormat(format); // 반환할 형식을 설정한 간단한 Formatter
String str = df.format(date); // Formatter를 이용하여 Date형의 값을 String으로 변환
System.out.println(str);
오늘이 2020년 7월 22일 경우
2020-07-22
로 출력됩니다.
자주 쓰는 Format
"yyyy-MM-dd"; // 2020-07-22
"yyyy/MM/dd"; // 2020/07/22
"yyyy.MM.dd"; // 2020.07.22
"HH:mm:ss"; // 17:33:21
Format 형태
문자 | Date 요소 | 표현 | 예시 |
G | 연대 | Text | AD |
y | 년도 | Year | 2020 |
Y | Week year (한개쓰는 경우 2020, 두개 쓰면 20) | Year | 2020 |
M | 월 (1~12) (한개쓰면 1~12, 두개 쓰면 01~12) | Month | 07 |
w | 해당 년도에서의 주차 (1~53) (한개쓰면 1~53, 두개 쓰면 01~53) | Number | 22 |
W | 해당 월에서의 주차 (1~5, 일요일부터 1주차) | Number | 2 |
D | 1월 1일 기준 오늘 일자 (1~366) | Number | 189 |
d | 해당 월 1일 기준 오늘 일자 (1~31) (한개쓰면 1~31, 두개쓰면 01~31) | Number | 10 |
F | 해당 월의 몇번째 요일 (1~6) | Number | 2 |
E | 요일 (문자) | Text | 일 |
u | 요일 (1 = 월요일 ~ 7 = 일요일) | Number | 1 |
a | AM/PM (오전/오후) | Text | PM |
H | 시간(h). 24시간 기준. 24시를 0시부터 본 경우 (0-23) | Number | 0 |
k | 시간(h). 24시간 기준. 24시를 24시로 본 경우 (1-24) | Number | 24 |
K | 시간(h). AM/PM 기준. 12시를 0시로 본 경우 (0-11) | Number | 0 |
h | 시간(h). AM/PM 기준. 12시를 12시로 본 경우 (1-12) | Number | 12 |
m | 분(m) | Number | 30 |
s | 초(s) | Number | 55 |
S | 밀리초(ms) | Number | 978 |
z | 타임존 | General time zone | KST |
Z | 타임존 | RFC 822 time zone | +0900 |
X | 타임존 | ISO 8601 time zone | +09 |
참고사이트
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
SimpleDateFormat (Java Platform SE 7 )
Parses text from a string to produce a Date. The method attempts to parse text starting at the index given by pos. If parsing succeeds, then the index of pos is updated to the index after the last character used (parsing does not necessarily use all charac
docs.oracle.com
[JAVA] How to use SimpleDateFormat Class
🇺🇸 English
2024.02.10 - [개발 창고/Java] - [JAVA] How to use SimpleDateFormat Class
[JAVA] How to use SimpleDateFormat Class
When I develop Java, I forget how to use it when I want to return today's date lightly If you think about it, it's a simple date format like a class name Date date = new Date(); // Return today's date to Date type String format = "yyyy-MM-dd"; // Year(4)-M
royzero.tistory.com
'개발 창고 > Java' 카테고리의 다른 글
[JAVA] 이메일 유효성 검사 (0) | 2023.03.16 |
---|---|
[POI] 엑셀 폰트 변경 (0) | 2023.02.10 |
[Nexacro] DataSet to Class (0) | 2023.01.27 |
[Java] Executable Jar 파일 실행 시 메모리 설정 (0) | 2022.03.11 |
[JAVA] Calendar Class 사용하기 (0) | 2020.07.23 |