반응형
- 현재 날짜 및 시간 구하기
일단 클래스 임포트 하기
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
- java.text.SimpleDateFormat
자주 사용하는 패턴
ex ) 현재 시간이 2022년 12월 31일 오후 10시 30분 30초 가정
yyyy (년도) | 2022 |
MM (월) | 12 |
dd (일) | 31 (그달의 일자) |
DD (일) | 365 (해당연도의 일자) |
HH (시간) | 22 (0~23 시간 표현) |
hh (시간) | 10 (1~12 시간 표현 ) |
mm (분) | 30 |
ss ( 초) | 30 |
a (오전/오후) | pm (기본 eng) |
F | 5 (그 달의 몇번째 날 표시) |
E | SAT (요일이름) |
* 기타 패턴 확인 가능 url
참고 출처 : 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.util.Locale
국가 언어에 대한 클래스로 여기서는 요일 변경이 가능해서 사용함.
- java.util.Date
시간을 가지고 올 수 있는 클래스임
객체 생성 시 가장가까운 밀리초로 할당된 시간을 표현 (= 현재 시간)
public class Example {
public static void main(String[] args) {
// 오늘 날짜, 시간 확인 가능한 객체 생성
Date today = new Date();
// 포맷변경 객체 생성
SimpleDateFormat test1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat test2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a", Locale.KOREA);
SimpleDateFormat test3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a", Locale.JAPAN);
SimpleDateFormat test4 = new SimpleDateFormat("오늘은 M월의 F번째 E요일", Locale.JAPAN);
/*
SimpleDateFormat의 format함수 - String으로 리턴
date 객체를 넣어서 날짜 포맷으로 변경해서 출력함.
*/
System.out.println(test1.format(today));
System.out.println(test2.format(today));
System.out.println(test3.format(today));
System.out.println(test4.format(today));
}
}
출력 결과
중간에 한문은 Locale.JAPAN 으로 사용하였기 때문에 일본어로 표시된다.
728x90
반응형
'Java' 카테고리의 다른 글
이클립스 및 STS - 자주 쓰는 단축키 모음 (0) | 2023.01.19 |
---|---|
No converter found for return value of type: class java.util.HashMap (Json converter 에러) (0) | 2023.01.11 |
[JSP] JSP 태그 (0) | 2022.12.28 |
Excel POI 사용법 (0) | 2022.10.20 |
[JSTL] 특정 문자열 포함 찾기: fn:contains / fn:containsIgnoreCase / fn:indexOf (0) | 2022.10.07 |