Binh Vova
Binh Vova

Reputation: 133

How to compare 2 time on blackberry?

in my app , I used DateField to get future time and i save the time as string like as 1 Feb 2012 23:49 to make a schedule for work. And I want to compare this time with time current. If time schedule = time current + 4 hour, a dialog will appear to remind working. But compare 2 times is so difficult . Because it depend on some cases example AM or PM, or if time schedule is 2 Feb 01:00 AM and time current is 1 Feb 21:00 PM , dialog also display.

Can anybody help me compare full correctly if time schedule = time current + 4 hours ?

Upvotes: 0

Views: 525

Answers (3)

Govindarao Kondala
Govindarao Kondala

Reputation: 2862

Try this

import net.rim.device.api.io.http.HttpDateParser;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;

public class StartUp extends UiApplication {
    /**
     * Entry point for application
     */
     public static void main(String[] args) {
         StartUp start=new StartUp();
         start.enterEventDispatcher();

     }
    /**
     * Creates a new StartUp object
     */
    public StartUp() {
        // Push a screen onto the UI stack to have something to render before we attempt

        pushScreen(new DateComparision());



    }
    class DateComparision extends MainScreen
    {
        public DateComparision() 
        {
            String first="31 Jan 2012 23:49:01";//formate should same 
            long time1=HttpDateParser.parse(first)+(4*60*60*1000);//means after 4 hours 
            //time1= 1328068141000
            String second="1 Feb 2012 03:49:01";
            long time2=HttpDateParser.parse(second);
            //time2=1328068141000
            System.out.println(time2+"  "+time1);
            if(time2==time1){
                displayMessage("Mached");
            }else 
            {
                displayMessage("Not Mached");
            }
        }
    }
    public static void displayMessage(final String data){
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                Dialog.inform(data);
            }
        });
    }
}

Upvotes: 1

BBdev
BBdev

Reputation: 4942

Save time in same Format means like this one 1 Feb 2012 23:49 and the second time you want to compare if it is in Am or Pm change that to 24 hrs format. And compare or convert Date to String and compare the String. If both String are equal then do your Task.

Upvotes: 1

rosco
rosco

Reputation: 939

You need to 'normalize' date: use Unix timestamp as returned by java.util.date.

Upvotes: 0

Related Questions