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;
SimpleDateFormat formate=new SimpleDateFormat("HH:mm:ss");
String formateddate=formate.formatLocal(diff);
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;
SimpleDateFormat formate=new SimpleDateFormat("HH:mm:ss");
String formateddate=formate.formatLocal(diff);
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][1]
[1]: http://i.stack.imgur.com/4Rggl.png
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;
SimpleDateFormat formate=new SimpleDateFormat("HH:mm:ss");
String formateddate=formate.formatLocal(diff);
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;
SimpleDateFormat formate=new SimpleDateFormat("HH:mm:ss");
String formateddate=formate.formatLocal(diff);
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][1]
[1]: http://i.stack.imgur.com/4Rggl.png
No comments:
Post a Comment