콘텐츠로 건너뛰기

java 에서 날짜 출력하기

자바에서 2010.01.08 과 같이 표현할 일이 생겼다.

처음에는 Calendar 객체를 통해서 구하려했다.

Calendar clendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;    // month는 0부터 시작함
int day = calendar.get(Calendar.DAY_OF_MONTH);

System.out.printf(“%4d.%02d.%02d”, year, month, day);

이렇게 C언어에서 쓰던 표현식을 이용한 출력을 통해 나타낼 수 있다.

근데,

더 간단한 방법을 찾아냈다.

SimpleDateFormat sdf = new SimpleDateFormat(“yyyy.MM.dd”, Locale.KOREA);
System.out.println(sdf.format(new Date());

이래줘야 자바지…^^

뒤에 Locale은 옵션인 듯 한데, 안 써주니까는 warning 마크가 발생한다.

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다