728x90
currentTimeMillis() 함수
currentTimeMillis() 함수는 1970년 1월 1일 0시를 기준으로 현재까지 경과한 시간을 1000분의 1초(밀리초)로 반환한다.
예를 들어, long start = System.currentTimeMillis(); 라고 가정하자.
1000분의 1초를 반환하기 때문에, 1970년 1월 1일 0시로부터 1000초가 지나면 long start = System.currentTimeMillis(); 의 출력결과는 1이다.
즉, long start = System.currentTimeMillis(); 값을 1000.0으로 나누면 1970/1/1 로부터 현재까지 경과한 시간을 1초 단위로 받는다.
경과한 시간을 분 단위로 받으려면, 1분이 60초니까(1000*60 = 60000)로 계산해서 long start를 /60000 하면 된다.
시 단위로 받으려면, 1시간은 60분→ 1분은 60초 * 60 = 60*60*1000 = 3600000 → long start를 /3600000 하면 된다.
<기본 사용법>
public class currentTimeMillis { public static void main(String [] args) { long now = System.currentTimeMillis(); System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(now)); System.out.println("currentTimeMillis 원형 : " + now); System.out.println("초 : " + now / 1000.0); System.out.println("분 : " + now / 60000); System.out.println("시 : " + now / 3600000); } } /* <출력결과> 2022-04-10 00:54:14.379 currentTimeMillis 원형 : 1649519654379 초 : 1649519654 분 : 27491994 시 : 458199 */
<처리 시간, 소요 시간 계산>
public class Iam_im2 { public static void main(String[] args) throws InterruptedException { // 1. 처리전 시간 획득 long start = System.currentTimeMillis(); System.out.print("Now : "); System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(start)); // 2. 처리할 작업 Thread.sleep(3000); //3초만큼 지연 System.out.println(""); // 3. 처리후 시간 획득 long end = System.currentTimeMillis(); System.out.print("Now : "); System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(end)); // 4. 시간차이 출력 System.out.println("Time taken to enter a number : " + (end - start) / 1000.0 + " seconds\n"); } } /* Now : 2022-04-10 01:35:28.684 Now : 2022-04-10 01:35:31.792 Time taken to enter a number : 3.108 seconds */
728x90
'언어 > JAVA' 카테고리의 다른 글
Eclipse 이클립스 폰트 변경 (0) | 2022.04.14 |
---|---|
가위바위보(Rock-Paper-Scissors Game) (0) | 2022.04.07 |
숫자를 입력 받는 Timer (0) | 2022.04.07 |
Arrays.sort 로 배열 정렬하기 (0) | 2022.04.06 |
ArrayList remove 특정 값 삭제 (0) | 2022.04.06 |