자바에서 2 밀리 초 값을 비교하려고합니다. 달력에서 하나, System.currentTimeMillis()
에서 하나. 그러나 캘린더의 값은 항상 정확한 시간에 System.currentTimeMillis()
의 값보다 훨씬 클 것입니다.캘린더와 밀리 초 시간 차이가 너무 큼
캘린더를 작성하려면 dd/MM/yyyy HH:mm
형식의 날짜 문자열을 구문 분석하고 해당 부분을 일정에 넣으십시오 (간결성을 위해 생략 된 try-catch).
Calendar obCal = null;
//Exception here shows invalid format
DateFormat DF = new SimpleDateFormat("dd/MM/yyyy HH:mm");
DF.parse(Date);
//Example string: 29/11/2011 16:30
String[] parts = Date.split("/");
obCal = Calendar.getInstance();
int Y = Integer.parseInt(parts[2].split(" ")[0]);
int m = Integer.parseInt(parts[1]);
int d = Integer.parseInt(parts[0]);
int H = Integer.parseInt(parts[2].split(" ")[1].split(":")[0]);
int M = Integer.parseInt(parts[2].split(" ")[1].split(":")[1]);
obCal.set(Calendar.YEAR, Y);
obCal.set(Calendar.MONTH, m);
obCal.set(Calendar.DAY_OF_MONTH, d);
obCal.set(Calendar.HOUR_OF_DAY, H);
obCal.set(Calendar.MINUTE, M);
obCal.set(Calendar.SECOND, 0);
obCal.set(Calendar.MILLISECOND, 0);
정확한 날짜와 시간이 표시된 후 캘린더를 검사합니다. 그때 obCal.getTimeInMillis()
의 시대 이후 밀리 초 값을 얻을 내가 날짜 문자열로 표현 된 시간에 도달하고, 달력에서 타임 스탬프를 비교하면 long
long stamp = obCal.getTimeInMillis()/1000L;
System.currentTimeMillis()
타임 스탬프를하는 등 초 단위로 저장 , 나는 이전이 훨씬 더 큰 것을 안다. 현재 시간과 달력 시간 (curTime-calTime
)의 차이는 일반적으로 다음과 같습니다. -2,500,000
이 문제의 원인은 무엇일까요? 감사합니다.
왜하지 'Calendar # setTime (...)'을 사용하면됩니까? – Thomas
왜'obCal.setTime (DF.parse (date));'를 사용하지 않습니까? – BalusC