1. Date - 날짜 클래스
월
0 ~ 11 까지임
정의
Date date = new Date(); // 현재 날짜 가져오기
String strNow = now.toString(); // 현재 날짜를 문자열로 변환
2. SimpleDateFormat - 날짜 포맷
대소문자를 구분함
정의
Date now = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy년 MM월 dd일 hh시 mm분 ss초");
String 변수 = simpleDateFormat.format(Date 객체); // 기본 포맷 -> 사용자 정의 포맷으로 변경
기호 | 의미 | 출력 |
G | 연대(BC,AD) | AD |
y | 년도 | 2022 |
M | 월 (1~12월 또는 1월~12월) | 4또는 4월, April |
w | 년의 몇 번째 주(1~53) | 16 |
W | 월의 몇 번째 주(1~5) | 3 |
D | 년의 몇 번째 일(1~366) | 102 |
d | 월의 몇 번째 일(1~31) | 12 |
F | 월의 몇 번째 요일(1~5) | 2 |
E | 요일 | Tue or 화 |
a | 오전/오후(AM,PM) | AM |
H | 시간(0~23) | 10 |
k | 시간(1~24) | 10 |
K | 시간(0~11) | 10 |
h | 시간(1~12) | 10 |
m | 분(0~59) | 0 |
s | 초(0~59) | 0 |
S | 천분의 1초(0~999) | 468 |
z | Time zone(General time zone) | KST |
Z | Time zone(RFC 822 time zone) | +0900 |
' | escape문자(특수문자를 표현하는데 사용) |
💡new SimpleDateFormat("yyyy년 MM월 dd일 E요일", Locale.KOREAN);
날짜 포맷 형식 지정 시
지역설정을 한국어로 해야 mon요일, tue요일 등 방지할 수 있습니다.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class test {
public static void main(String[] args) throws ParseException {
/* 시작 , 끝 날짜 임의 세팅 */
Date today = new Date();
SimpleDateFormat format0,format1,format2,format3,format4;
SimpleDateFormat format5,format6,format7,format8,format9,format10;
format0 = new SimpleDateFormat("yyyyMMdd");
format1 = new SimpleDateFormat("yyyy-MM-dd");
format2 = new SimpleDateFormat("''yy년 MM월 dd일 E요일",Locale.KOREAN);
format3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
format4 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
format5 = new SimpleDateFormat("오늘은 올 해의 D번째 날");
format6 = new SimpleDateFormat("오늘은 이 달의 d번째 날");
format7 = new SimpleDateFormat("오늘은 올 해의 w번째 주");
format8 = new SimpleDateFormat("오늘은 이 달의 W번째 주");
format9 = new SimpleDateFormat("오늘은 이 달의 F번째 E요일",Locale.KOREAN);
format10 = new SimpleDateFormat("G,y,M,w,W,D,d,F,E,a,H,k,K,h,m,s,S,z,Z");
System.out.println(format0.format(today));
System.out.println(format1.format(today));
System.out.println(format2.format(today));
System.out.println(format3.format(today));
System.out.println(format4.format(today));
System.out.println();
System.out.println(format5.format(today));
System.out.println(format6.format(today));
System.out.println(format7.format(today));
System.out.println(format8.format(today));
System.out.println(format9.format(today));
System.out.println();
System.out.println(format10.format(today));
}
}
/* 출력 결과 */
20220412
2022-04-12
'22년 04월 12일 화요일
2022-04-12 10:00:00.468
2022-04-12 10:00:00 AM
오늘은 올 해의 102번째 날
오늘은 이 달의 12번째 날
오늘은 올 해의 16번째 주
오늘은 이 달의 3번째 주
오늘은 이 달의 2번째 화요일
AD,2022,4,16,3,102,12,2,Tue,AM,10,10,10,10,0,0,468,KST,+0900
날짜를 원하는 형식으로 표현하기 위해 패턴을 사용하는데 다음 표는 SimpleDateFormat의 패턴 장성에 사용되는 기호들이다.
문자 | 날짜 및 시간 요소 | 표시 | 예제 |
G | Era designator | Text | AD |
y | Year | Year | 1996; 96 |
Y | Week year | Year | 2009; 09 |
M | Month in year (context sensitive) | Month | July; Jul; 07 |
L | Month in year (standalone form) | Month | July; Jul; 07 |
w | Week in year | Number | 27 |
W | Week in month | Number | 2 |
D | Day in year | Number | 189 |
d | Day in month | Number | 10 |
F | Day of week in month | Number | 2 |
E | Day name in week | Text | Tuesday; Tue |
u | Day number of week (1 = Monday, ..., 7 = Sunday) | Number | 1 |
a | Am/pm marker | Text | PM |
H | Hour in day (0-23) | Number | 0 |
k | Hour in day (1-24) | Number | 24 |
K | Hour in am/pm (0-11) | Number | 0 |
h | Hour in am/pm (1-12) | Number | 12 |
m | Minute in hour | Number | 30 |
s | Second in minute | Number | 55 |
S | Millisecond | Number | 978 |
z | Time zone | General time zone | Pacific Standard Time; PST; GMT-08:00 |
Z | Time zone | RFC 822 time zone | -0800 |
X | Time zone | ISO 8601 time zone | -08; -0800; -08:00 |
예제 패턴
날짜 및 시간 패턴 | 결과 |
"yyyy.MM.dd G 'at' HH:mm:ss z" | 2001.07.04 AD at 12:08:56 PDT |
"EEE, MMM d, ''yy" | Wed, Jul 4, '01 |
"h:mm a" | 12:08 PM |
"hh 'o''clock' a, zzzz" | 12 o'clock PM, Pacific Daylight Time |
"K:mm a, z" | 0:08 PM, PDT |
"yyyyy.MMMMM.dd GGG hh:mm aaa" | 02001.July.04 AD 12:08 PM |
"EEE, d MMM yyyy HH:mm:ss Z" | Wed, 4 Jul 2001 12:08:56 -0700 |
"yyMMddHHmmssZ" | 010704120856-0700 |
"yyyy-MM-dd'T'HH:mm:ss.SSSZ" | 2001-07-04T12:08:56.235-0700 |
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX" | 2001-07-04T12:08:56.235-07:00 |
"YYYY-'W'ww-u" | 2001-W27-3 |
참고 사이트
SimpleDateFormat (Java Platform SE 8 )
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
출처: https://junghn.tistory.com/entry/JAVA-자바-날짜-포맷-변경-방법SimpleDateFormat-yyyyMMdd [코딩 시그널:티스토리]
'JAVA' 카테고리의 다른 글
Java - LocalDate , LocalTime , LocalDateTime (클래스) (0) | 2023.09.26 |
---|---|
Java - Calendar (클래스) (0) | 2023.09.26 |
Java - Math (클래스) (0) | 2023.09.26 |
Java - Wrapper ( Boxing , UnBoxing ) (0) | 2023.09.26 |
Java - String (클래스) (0) | 2023.09.26 |