주제>

자바에서 현재 시간을 가져와서 표현한다.

 

로그에서 현재 시간을 출력해 볼 때 쓰면된다.

시간 형식은 원하는 표현 방법을 사용하면 된다.

 

 

사용예>

시간 포맷을 아래와 같이 표현한다.

 

년-월-일 시간(24):분:초

yyyy-MM-dd HH:mm:ss

 

예로 

2020년 09월 08일 오전 6시 0분 0초라면

표현 형식: 2020-09-08 06:00:00

 

 

아래와 같이 객체를 생성하면 된다.

 

new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis());

 

 

사용 예제>

System.out.println("현재 시간: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));

 

 

 

Class SimpleDateFormat

 

1) 날짜와 시간 형식

 

Date and Time Patterns

 

 

2) 사용방법

 

Example

 

 

 

Class System

 

public static long currentTimeMillis()

: 현재 시간을 밀리세컨드로 리턴한다.

  public static 이므로 System.currentTimeMillis() 로 바로 가져다 쓸 수 있다.

 

 

출처>

오라클 자바 문서

Class SimpleDateFormat

https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

 

Class System - currentTimeMillis

 

public static long currentTimeMillis()

Returns the current time in milliseconds.

Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

See the description of the class Date for a discussion of slight discrepancies that may arise between "computer time" and coordinated universal time (UTC).

Returns:the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.

 

https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#currentTimeMillis()

 

 

728x90

+ Recent posts