java : 현재시간 구하기 (calender클래스, 시스템 타이머)

2018. 1. 1. 15:21BASIC/java

* Calender 클래스 이용

 

// calender 객체를 얻고,

Calendar cal = Calendar.getInstance();

// 출력 형태를 지정하고,

SimpleDateFormat datetime_format = new SimpleDateFormat("yyyy-MM-dd hh:mm");

// 출력 형태에 맞는 calender.getTime();

logger.info(datetime_format.format(cal.getTime()));

int year = cal.get(Calendar.YEAR);

int month = cal.get(Calendar.MONTH);

int day = cal.get(Calendar.DAY_OF_MONTH);

int hour = cal.get(Calendar.HOUR);

int minute = cal.get(Calendar.MINUTE);

int second = cal.get(Calendar.SECOND);

int millisecond = cal.get(Calendar.MILLISECOND);

 

* 시스템 타이머 이용

// 현재 시스템의 시간 정보를 얻고,

long curr = System.currentTimeMillis();

// 출력 형태를 지정하고,

SimpleDateFormat datetime_format = new SimpleDateFormat("yyyy-MM-dd hh:mm");

// 출력 형태에 맞는 Date 문자열을 얻는다.

datetime_format.format(new Date(curr));



!! 이때 시간형태를 0~24시간 형태로 하고싶다면 포맷 형식을 ( hh > kk )로 변경


SimpleDateFormat datetime_format new SimpleDateFormat("yyyy-MM-dd kk:mm");