Posted on 2007. 8. 27. 13:19
Filed Under Java

Timestamp 형태 <-----> 날자형태  로 변환하기

※ Timestamp는 1970년 1월 1일 0시 0분 0초 GMT를 기준으로...    증가한일수*24시간*60분*60초*1000밀리세컨드   값이다.
자바의 시간은 기본적으로 vm이 돌아가는 환경에 따라서 자동으로 각 국가별로 환산되기때문에 우리나라의 GMT차에 따른 +9시간(9*60*60*1000) 만큼 계산된 값이 나온다.
그러므로, DB에서 Timestamp값을 받아 java/jsp페이지에 적용시키려면 미리 9시간 만큼을 -9시간 하고 가져와야한다.


-- 날자형태를 Timestamp값으로 가져오기
SELECT (to_date('20070824', 'YYYYMMDD') -  to_date('19700101','yyyymmdd')) * (1*24*60*60*1000) + (9*60*60*1000)
FROM DUAL;

-- Timestamp형태를 날자값으로 가져오기
SELECT to_date('19700101','yyyymmdd') + (1187913600000 - (9*60*60*1000)) / (1*24*60*60*1000)
FROM DUAL;



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);

About

by 청지인

Notice

Counter

· Total
:
· Today
:
· Yesterday
: