2016-07-01 96 views
0

我一直对计算时间差的应用程序,并有这真的奇怪的问题,模拟器设备得到不同的结果。仿真器和设备得出不同的结果

如果时间设定22:0022时45分上emaulator差为。 当我做同样的事情,当我设备上运行,我得到

感谢。

UPDATE

这里是码余如何计算小时/分钟

public int theTimeMachineHours(EditText a, EditText b) throws Exception{ 
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm"); 
    Date startDate = simpleDateFormat.parse(a.getText().toString()); 
    Date endDate = simpleDateFormat.parse(b.getText().toString()); 

    long difference = endDate.getTime() - startDate.getTime(); 
    if(difference<0) 
    { 
     Date dateMax = simpleDateFormat.parse("24:00"); 
     Date dateMin = simpleDateFormat.parse("00:00"); 
     difference=(dateMax.getTime() -startDate.getTime())+(endDate.getTime()-dateMin.getTime()); 
    } 
    int days = (int) (difference/(1000*60*60*24)); 
    int hours = (int) ((difference - (1000*60*60*24*days))/(1000*60*60)); 
    int min = (int) (difference - (1000*60*60*24*days) - (1000*60*60*hours))/(1000*60); 

    return hours; 
} 

UPDATE 2

下面是从一个代码部分代码,其中所述TIMEMACHINE方法是称为:

if(restFrom2Day1.length()!=0){ 
     sendBreakHour2Day1 = ""+theTimeMachineHours(restFrom2Day1,restTo2Day1); 
     sendBreakMin2Day1 = ""+theTimeMachineMin(restFrom2Day1,restTo2Day1); 
     hour2Day1 = Integer.parseInt(sendBreakHour2Day1); 
     minute2Day1 = Integer.parseInt(sendBreakMin2Day1); 
    } 
+0

您能提供一些代码来确定时差吗? – babadaba

+0

@babadaba我已经添加了代码。 – mogren3000

回答

0

我相信你在DST区(夏令时)下,对不对?

这样,您的设备可能会标记23:00(而不是22:00)。

也许,您正在将日历设置为22:00。但是,由于DST处于活动状态,因此您的时间实际上是23:00(22:00 + DST启用标志)。所以,也许,这就是你得到15分而不是45分钟的原因。

+0

好的。我如何禁用此功能。 – mogren3000

+0

在你的问题分享更多的代码..只是看你如何设置时间/日历等... – W0rmH0le

+0

检查更新与代码 – mogren3000

0

这是否会为您产生其他结果?

public long theTimeMachineHours(EditText a, EditText b) throws Exception{ 
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm", Locale.ENGLISH); 
    Date startDate = simpleDateFormat.parse(a.getText().toString()); 
    Date endDate = simpleDateFormat.parse(b.getText().toString()); 


    long different = endDate.getTime() - startDate.getTime(); 

    long minutesInMilli = 1000 * 60; 
    long hoursInMilli = minutesInMilli * 60; 

    long hours = different % (hoursInMilli * 24)/hoursInMilli; 
    long minutes = different % hoursInMilli/minutesInMilli; 

    return hours; 
} 
+0

如果需要您可以将小时数转换为int – babadaba

+0

这并没有解决问题。 – mogren3000

+0

你在EditTexts中输入什么内容? – babadaba

相关问题