2012-03-27 22 views
1

我想獲得字符串形式的時間之間的差異,但我得到的差異總是0,我正在對黑莓工作。如何獲取字符串形式的時間之間的差異

  long startTime = HttpDateParser.parse("01:00:00"); 
      System.out.println("start time is :" + startTime); 
      long endTime = HttpDateParser.parse("02:00:00"); 
      System.out.println("end time is :" + endTime); 
      long diff = endTime - startTime; 
      long diffHours = diff/(60 * 60 * 1000); 

回答

1

如果要計算同一天兩次被格式化爲之間唯一的一次‘HH:MM:SS’,那麼它是非常容易

使用簡單HttpDateParser和採取具有正確的格式

像任何日期「星期二,2012年3月27日九時十一分37秒GMT」

在這裏,我們僅更換時間,那麼你將g ^等時間米莉兩次

String common="Tue, 27 Mar 2012 "; //09:11:37 GMT 
      String time1="1:50:10"; 
      String time2="3:30:20"; 
      long startTime = HttpDateParser.parse(common+time1+" GMT"); 
      System.out.println("start time is :" + startTime); 
      long endTime = HttpDateParser.parse(common+time2+" GMT"); 
      System.out.println("end time is :" + endTime); 
      long diff = endTime - startTime; 
      long hours=diff/(60*60*1000); 
     long minutes=(diff%(60*60*1000))/(60*1000); 

     long sec=((diff%(60*60*1000))%(60*1000))/1000; 
     String formateddate=""; 
     if(hours<10) 
     { 
      formateddate="0"+hours; 
     }else{ 
      formateddate=""+hours; 
     } 
     if(minutes<10) 
     { 
      formateddate=formateddate+":0"+minutes; 
     }else{ 
      formateddate=formateddate+":"+minutes; 
     } 
     if(sec<10) 
     { 
      formateddate=formateddate+":0"+sec; 
     }else{ 
      formateddate=formateddate+":"+sec; 
     } 


     System.out.println("Difference in Houres:"+formateddate); 

我輸出

[0.0] start time is :1332813010000 
[0.0] end time is :1332819020000 
[0.0] Difference in Houres:01:40:10 

之間。注意:如果你想不同的兩天 在這裏,你需要傳遞的日期格式應該是正確的像例如之間計算: 「YYYY-MM-DD HH:MM:SS」

在這裏,我提供了一些示例實施例

import java.util.Calendar; 

import net.rim.device.api.i18n.SimpleDateFormat; 
import net.rim.device.api.io.http.HttpDateParser; 
import net.rim.device.api.ui.Field; 
import net.rim.device.api.ui.FieldChangeListener; 
import net.rim.device.api.ui.component.ButtonField; 
import net.rim.device.api.ui.component.LabelField; 
import net.rim.device.api.ui.container.MainScreen; 
import net.rim.device.api.ui.picker.DateTimePicker; 

public final class Screen1 extends MainScreen implements FieldChangeListener 
{ 
    /** 
    * Creates a new MyScreen object 
    */ 
    ButtonField date1; 
    ButtonField date2; 
    ButtonField button; 
    LabelField lbl; 
    DateTimePicker picker; 
    String str=""; 
    Calendar cal; 

    public Screen1() 
    { 

     date1=new ButtonField("date1"); 
     date1.setChangeListener(this); 
     add(date1); 

     date2=new ButtonField("date2"); 
     date2.setChangeListener(this); 
     add(date2); 

     button = new ButtonField("Screen 1 "); 
     button.setChangeListener(this); 
     add(button); 
     lbl=new LabelField(); 
     add(lbl); 
    } 
    public void fieldChanged(Field field, int context) { 
     if(field==button){//Tue, 27 Mar 2012 09:11:37 GMT 

      System.out.println(date1.getLabel().toString()+" "+date2.getLabel().toString()); 
      long startTime = HttpDateParser.parse(date1.getLabel().toString()); 
      System.out.println("start time is :" + startTime); 
      long endTime = HttpDateParser.parse(date2.getLabel().toString()); 
      System.out.println("end time is :" + endTime); 
      long diff = endTime - startTime; 

      long hours=diff/(60*60*1000); 
     long minutes=(diff%(60*60*1000))/(60*1000); 

     long sec=((diff%(60*60*1000))%(60*1000))/1000; 
     String formateddate=""; 
     if(hours<10) 
     { 
      formateddate="0"+hours; 
     }else{ 
      formateddate=""+hours; 
     } 
     if(minutes<10) 
     { 
      formateddate=formateddate+":0"+minutes; 
     }else{ 
      formateddate=formateddate+":"+minutes; 
     } 
     if(sec<10) 
     { 
      formateddate=formateddate+":0"+sec; 
     }else{ 
      formateddate=formateddate+":"+sec; 
     } 


     System.out.println("Difference in Houres:"+formateddate); 
     lbl.setText("Time Between above dates is :"+formateddate); 
     }else if(field==date1) 
     { 
      date1.setLabel(getDateTimeFromPicker().toString());         

     }else if(field==date2) 
     { 
      date2.setLabel(getDateTimeFromPicker().toString()); 
     } 

    } 
    private String getDateTimeFromPicker() 
    { 
     picker = DateTimePicker.createInstance(Calendar.getInstance(), "yyyy-MM-dd", "HH:mm:ss"); 
     picker.doModal(); 
     cal=picker.getDateTime(); 
     str=""; 
     if((cal.get(Calendar.MONTH)+1)<10) 
      str=str+cal.get(Calendar.YEAR)+"-"+"0"+(cal.get(Calendar.MONTH)+1); 
     else 
      str=str+cal.get(Calendar.YEAR)+"-"+(cal.get(Calendar.MONTH)+1); 

     if(cal.get(Calendar.DATE)<10) 
      str=str+"-"+"0"+cal.get(Calendar.DATE); 
     else 
      str=str+"-"+cal.get(Calendar.DATE); 

     if(cal.get(Calendar.HOUR_OF_DAY)<10) 
      str=str+" "+"0"+cal.get(Calendar.HOUR_OF_DAY); 
     else 
      str=str+" "+cal.get(Calendar.HOUR_OF_DAY); 

     if(cal.get(Calendar.MINUTE)<10) 
      str=str+":"+"0"+cal.get(Calendar.MINUTE); 
     else 
      str=str+":"+cal.get(Calendar.MINUTE); 

     if(cal.get(Calendar.SECOND)<10) 
      str=str+":"+"0"+cal.get(Calendar.SECOND); 
     else 
      str=str+":"+cal.get(Calendar.SECOND); 
     return str; 
    } 
} 

你可以看到在你的屏幕下面的輸出 enter image description here

+0

感謝它工作:) – user1195292 2012-03-27 11:39:50

0

我想你所傳遞的字符串不是預期的格式。請參閱RIM BlackBerry API的link瞭解更多信息。

的格式的字符串應該是是否HttpDateParser只接受時間的這些

"Wdy, DD Mon YY" 
"Wdy, DD Mon YYYY" 
"Wdy, DD Mon YY HH:MM:SS" 
"Wdy, DD Mon YY HH:MM:SS GMT" 
"Wdy, DD Mon YY HH:MM:SS TZD" 
"Wdy, DD Mon YY HHMMSS" 
"Wdy, DD Mon YY HHMMSS GMT" 
"Wdy, DD Mon YY HHMMSS TZD" 
"Wdy, DD Mon YYYY HH:MM:SS" 
"Wdy, DD Mon YYYY HH:MM:SS GMT" 
"Wdy, DD Mon YYYY HH:MM:SS TZD" 
"Wdy, DD Mon YYYY HHMMSS" 
"Wdy, DD Mon YYYY HHMMSS GMT" 
"Wdy, DD Mon YYYY HHMMSS TZD" 
"Wdy, Mon DD YY HH:MM:SS" 
"Wdy, Mon DD YY HH:MM:SS GMT" 
"Wdy, Mon DD YY HH:MM:SS TZD" 
"Wdy, Mon DD YY HHMMSS" 
"Wdy, Mon DD YY HHMMSS GMT" 
"Wdy, Mon DD YY HHMMSS TZD" 
"Wdy, Mon DD YYYY HH:MM:SS" 
"Wdy, Mon DD YYYY HH:MM:SS GMT" 
"Wdy, Mon DD YYYY HH:MM:SS TZD" 
"Wdy, Mon DD YYYY HHMMSS" 
"Wdy, Mon DD YYYY HHMMSS GMT" 
"Wdy, Mon DD YYYY HHMMSS TZD" 
"Wdy, DD Mon YY, HH:MM:SS" 
"Wdy, DD Mon YY, HH:MM:SS GMT" 
"Wdy, DD Mon YY, HH:MM:SS TZD" 
"Wdy, DD Mon YY, HHMMSS" 
"Wdy, DD Mon YY, HHMMSS GMT" 
"Wdy, DD Mon YY, HHMMSS TZD" 
"Wdy, DD Mon YYYY, HH:MM:SS" 
"Wdy, DD Mon YYYY, HH:MM:SS GMT" 
"Wdy, DD Mon YYYY, HH:MM:SS TZD" 
"Wdy, DD-Mon-YY HH:MM:SS" 
"Wdy, DD-Mon-YY HH:MM:SS GMT" 
"Wdy, DD-Mon-YY HH:MM:SS TZD" 
"Wdy, DD-Mon-YY HHMMSS" 
"Wdy, DD-Mon-YY HHMMSS GMT" 
"Wdy, DD-Mon-YY HHMMSS TZD" 
"Wdy, DD-Mon-YY, HH:MM:SS" 
"Wdy, DD-Mon-YY, HH:MM:SS GMT" 
"Wdy, DD-Mon-YY, HH:MM:SS TZD" 
"Wdy, DD-Mon-YY, HHMMSS" 
"Wdy, DD-Mon-YY, HHMMSS GMT" 
"Wdy, DD-Mon-YY, HHMMSS TZD" 
"Wdy, DD-Mon-YYYY HH:MM:SS" 
"Wdy, DD-Mon-YYYY HH:MM:SS GMT" 
"Wdy, DD-Mon-YYYY HH:MM:SS TZD" 
"Wdy, DD-Mon-YYYY HHMMSS" 
"Wdy, DD-Mon-YYYY HHMMSS GMT" 
"Wdy, DD-Mon-YYYY HHMMSS TZD" 
"Wdy, DD-Mon-YYYY, HH:MM:SS" 
"Wdy, DD-Mon-YYYY, HH:MM:SS GMT" 
"Wdy, DD-Mon-YYYY, HH:MM:SS TZD" 
"Wdy, DD-Mon-YYYY, HHMMSS" 
"Wdy, DD-Mon-YYYY, HHMMSS GMT" 
"Wdy, DD-Mon-YYYY, HHMMSS TZD" 
"Weekday, DD-Mon-YY HH:MM:SS" 
"Weekday, DD-Mon-YY HH:MM:SS GMT" 
"Weekday, DD-Mon-YY HH:MM:SS TZD" 
"Weekday, DD-Mon-YY HHMMSS" 
"Weekday, DD-Mon-YY HHMMSS GMT" 
"Weekday, DD-Mon-YY HHMMSS GMT" 
"Weekday, DD-Mon-YYYY HH:MM:SS" 
"Weekday, DD-Mon-YYYY HH:MM:SS GMT" 
"Weekday, DD-Mon-YYYY HH:MM:SS TZD" 
"Weekday, DD-Mon-YYYY HHMMSS" 
"Weekday, DD-Mon-YYYY HHMMSS GMT" 
"Weekday, DD-Mon-YYYY HHMMSS TZD" 
"Wdy Mon DD HH:MM:SS YYYY" 
"YYYY" (eg 1997) 
"YYYY-MM" (eg 1997-07) 
"YYYY-MM-DD" (eg 1997-07-16) 
"YYYY-MM-DDThh:mmTZD" (eg 1997-07-16T19:20+01:00) 
"YYYY-MM-DDThh:mm:ssTZD" (eg 1997-07-16T19:20:30+01:00) 
"YYYY-MM-DDThh:mm:ss.sTZD" (eg 1997-07-16T19:20:30.45+01:00) 
+0

對不起它HH:MM:SS只 – user1195292 2012-03-27 10:57:05

+0

NO。檢查y更新的答案。 – 2012-03-27 11:02:20