noura20,
I have yet to figure out the best way to deal with DateFields on the BB. It doesn't seem that you were meant to do things the easy like you suggest dateField.getDate().
What I have done that works (but is ugly) is to create a Calendar variable for the date I want to display in the DateField. When I want to deal with what the user has entered in the field, I have to take the data from the field as a String and set the individual fields of the Calendar variable.
You can then use the Calendar variable to do your before/after comparisons or further manipluation.
I welcome suggestions if there is an easier way...
Here's a chunk of code to try and show you what I mean:
Code:
package com.yourname.datetest;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import net.rim.device.api.i18n.*;
import net.rim.device.api.util.*;
import java.util.*;
public final class TestScreen extends MainScreen {
// Screen fields
private DateField test;
private SimpleDateFormat myDtTm = new SimpleDateFormat("MM/dd/yy HH:mm");
private Calendar testDt = Calendar.getInstance();
public TestScreen() {
// I didn't need resolution of sec or msec, so I zero them
testDt.set(Calendar.SECOND, 0);
testDt.set(Calendar.MILLISECOND, 0);
test = new DateField("Date/Time: ", testDt.getTime().getTime(), myDtTm, Field.FIELD_LEFT);
add(arv);
}
protected void makeMenu( Menu menu, int instance ) {
MenuItem save = new MenuItem("Save", 40, 40) {
public void run() {
Calendar newTest = Calendar.getInstance();
Calendar testDt = Calendar.getInstance();
Date date = new Date();
boolean isError = false;
// Validate data
String newDtTm = test.toString();
// Calendar variable stores months relative to 0, ie Jan = 0, ...
newTest.set(Calendar.MONTH, Integer.parseInt(newArvDtTm.substring(0, 2))-1);
newTest.set(Calendar.DATE, Integer.parseInt(newArvDtTm.substring(3,5)));
// Calendar variable needs the entire year, ie, 06 is not 2006
newTest.set(Calendar.YEAR, 2000+Integer.parseInt(newArvDtTm.substring(6,8)));
newTest.set(Calendar.HOUR_OF_DAY, Integer.parseInt(newArvDtTm.substring(9,11)));
newTest.set(Calendar.MINUTE, Integer.parseInt(newArvDtTm.substring(12)));
newTest.set(Calendar.SECOND, 0);
newTest.set(Calendar.MILLISECOND, 0);
// If Date/Time more than 1 hour in future, error, retry
// *don't forget working in msec*
date.setTime(testDt.getTime().getTime()+60L*60L*1000L);
testDt.setTime(date);
if (newTest.after(testDt)) {
isError = true;
Dialog.inform("Date/Time cannot be more than one hour greater than current time");
}
// If no errors
if (!isError) {
// Do whatever you want with the data here ...
// Close this screen, on to next?
UiApplication.getUiApplication().popScreen(TestScreen.this);
}
}
};
MenuItem quit = new MenuItem("Quit", 40, 40) {
public void run() {
if (Dialog.ask(Dialog.D_YES_NO, "Are you sure you want to quit and lose all information?", Dialog.NO) == Dialog.YES) {
UiApplication.getUiApplication().popScreen(TestScreen.this);
}
}
};
menu.add(save);
menu.add(quit);
}
public boolean onClose() {
if (Dialog.ask(Dialog.D_YES_NO, "Are you sure you want to quit and lose all information?", Dialog.NO) == Dialog.YES) {
UiApplication.getUiApplication().popScreen(TestScreen.this);
return true;
} else {
return false;
}
}
}