let dt = new Date().getTime();
결과 : 1725985982197
자바
자바에서는 시스템 클래스 자체에서 제공되는 static currentTimeMillis()이라는 메서드를 사용해서 UNIX TIME을 가져올 수 있다.
long time = System.currentTimeMillis();
결과 : 1725984896039
사용 예시
데이터베이스에 아이템 구매 당시의 시간을 기록하고, 아이템 구매후 지난 시간을 측정하고자 할 때
long a;
a = System.currentTimeMillis();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long time = System.currentTimeMillis();
long deltaTime = time - a;
double seconds = deltaTime/(double)1000;
System.out.println("전체 남은 시간 (초) : " + seconds);
double minute = seconds/(double)60;
System.out.println("전체 남은 시간 (분): " + minute);
지점으로부터 5초가 지난지 확인하는 방법
현재 시간에서 특정 지점을 뺀 뒤 걸린 시간을 비교하면 된다.
long a;
a = System.currentTimeMillis();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long time = System.currentTimeMillis();
long deltaTime = time - a;
if(deltaTime>=5000) {
System.out.println("구매후 시간이 5초가 지났습니다.");
}