2012-10-08 4 views
0

내 응용 프로그램에 2 개의 이벤트가 있습니다. 그래서 나는이 코드 줄 사용 된 SharedPreferences의 시간을 저장하는 두 사람 사이의 시간 차이가 필요합니다두 시간의 차이는 항상 2 이상을 반환합니다.

public static void PutStatus(Context ctx,boolean stat) 
{ 
     if (ctx != null) 
     { 
     SharedPreferences shr = ctx.getSharedPreferences("test", Context.MODE_WORLD_READABLE); 
     shr.edit().putBoolean("SHIFT", stat).commit(); 
     if (stat) shr.edit().putLong("START_TIME", System.currentTimeMillis()).commit(); 
     } 



} 

을 그리고 나는이 사용하여 두 날짜와 시간 차이 계산 :

SharedPreferences shr = getApplicationContext().getSharedPreferences("test", Context.MODE_WORLD_READABLE); 
      long time_stamp = System.currentTimeMillis() - shr.getLong("START_TIME", 0); 
      Date data = new Date(time_stamp); 

분은 훌륭하지만 시간은 2 시간 앞입니다.

왜 시간대와 연결되어 있습니까?

답변

3

적절한 방법처럼 두 날짜 객체, 뭔가를 사용하는 제안은 조금 복잡하다. 문서에 대해서는

import javax.xml.datatype.Duration; 
import javax.xml.datatype.DatatypeFactory 
... 
Duration d = DatatypeFactory.newInstance().newDuration(time_stamp); 
System.out.println(d.getHours() + "h:" + d.getMinutes() + "m: " + d.getSeconds() + "s"); 

DurationDatatypeFactory 참조 : 당신은 그것을 다음과 같은 방법을 만들 것입니다.

+0

다음 예외가 발생합니다. 10-08 10 : 19 : 11.398 : W/System.err (1381) : javax.xml.datatype.DatatypeConfigurationException : Provider org.apache.xerces.jaxp.datatype.DatatypeFactoryImpl을 찾을 수 없음 어떤 문제가 있습니까? 이 솔루션은 나에게 괜찮은 것 같습니다 ... – opc0de

+0

@ opc0de 이것은 android에서 버그 인 것 같습니다. bugreport에 대해서는 http://code.google.com/p/android/issues/detail?id=14379를 참조하고 응용 프로그램에 Xerces의 DatatypeFactory를 포함하는 해결 방법에 대한 힌트를 참조하십시오. – halex

0

시간 간격이 필요한 경우 일부 모듈로 계산하는 것은 어떻습니까? 시간은 3600 * 1000 밀리 초이고 하루는 86400 * 1000입니다.이 방법으로 날짜를 만들면 1.1.1970 UTC에 상대적인 날짜 개체가 생성됩니다. 이는 휴대 전화가있는 시간대에서 2 시간 정도 떨어져있을 수도 있습니다.

0

내가 자바 기간을 얻기 위해

int diffInDays = (newerDate.getTime() - olderDate.getTime())/(1000 * 60 * 60 * 24)