Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7697921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:07:19+00:00 2026-05-31T22:07:19+00:00

I am trying to get the difference between time which is in the form

  • 0

I am trying to get the difference between time which is in the form of string but i am getting the difference has 0 always,i am working on 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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:07:20+00:00Added an answer on May 31, 2026 at 10:07 pm

    If you want to calculate only time between two times in same day which are formatted as “HH:MM:SS” then it is pretty much easy

    Use simple HttpDateParser and take any date which have proper format

    like “Tue, 27 Mar 2012 09:11:37 GMT”

    here we replace only time then you will get time in millies between two times

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

    I got output as

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

    Note : If you want to calculate between two different days
    Here you need to pass date format should be proper like example: “YYYY-mm-dd hh:mm:ss”

    here i provide some sample example

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

    you can see as following output in your screen
    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get the difference between 2 containers but the containers are
I've spent a long time trying different things to get this to work but
I am trying to get a difference between the type casting methods. eg. Method
I'm trying to get a feel for the difference in performance between integer multiplication
I'm trying to get the difference between 2 dates in days, hours, and seconds:
I am trying get all html links within a string and replace them using
I am trying get all html links within a string and replace them using
I wrote an instance method in my model for calculating a time difference between
I'm trying to understand the difference between Ruby threads pre-1.9 and 1.9 (in the
This might be long and I apologize up front, I can get wordy trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.