Skip to content

Java.text.SimpleDateFormat

자바 System 클래스의 함수중 현재 시간을 리턴해주는 메소드는 아래와 같다.

public static native long currentTimeMillis();

리턴형은 long 형으로, 1970년 1월 1일부터 계산된 1/1000초 단위의 값을 계산하여 리턴해 준다.

Simple usage

public static final String getTimeStamp() {
    Calendar calender = Calendar.getInstance();
    calender.setTimeInMillis(System.currentTimeMillis());
    SimpleDateFormat format = new SimpleDateFormat("yyMMdd-HHmmssSSS", Locale.KOREA);
    return format.format(calender.getTime());
}

현재 날짜와 시간을 획득하는 방법

long now = System.currentTimeMillis();
SimpleDateFormat sdfNow = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strNow = sdfNow.format(new Date(now));

Timestamp를 yyyy-mm-dd hh:mm:ss 형식으로 바꾸려면?

SimpleDateFormat sdfCurrent = new SimpleDateFormat ("yyyy-mm-dd hh:mm:ss"); 
Timestamp currentTime = new Timestamp(Long.parseLong("1141952708997")); 
String today = sdfCurrent.format(currentTime); 
System.out.println("today==="+today);

See also

Favorite site