如何获得字符串形式的时间差异

时间:2012-03-27 10:22:02

标签: java datetime blackberry

我正在尝试以字符串的形式获得时间差异,但我总是得到差异,我正在黑莓上工作。

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

3 个答案:

答案 0 :(得分:1)

如果您只计算两次在同一天之间的格式为“HH:MM:SS”的时间,则非常容易

使用简单的HttpDateParser并获取具有适当格式的任何日期

喜欢“星期二,2012年3月27日09:11:37 GMT”

这里我们只更换时间,然后你会得到两次之间的毫秒时间

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

答案 1 :(得分:0)

我认为你传递的字符串不是预期的格式。有关详细信息,请参阅此link RIM BlackBerry API。

格式化的字符串应该是这些

中的任何一个
"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 YYY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, 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)

答案 2 :(得分:0)

HttpDateParser只接受时间吗? 根据{{​​3}},它也应该有日期。 因此,如果你真的需要使用这个类,可能需要快速修复,将“1:00:00”改为“2010-10-10T01:00:00”和“2:00:00”改为“2010-10-10T02” :00:00"