Java Date and BlackBerry DateField

working with Dates is always a pleasure in Java – using Dates inside your BlackBerry Java App there’s much more pleasure 😉

BlackBerry Java has a special Field to deal with Dates:

net.rim.device.api.ui.component.DateField

If this Field is editable, clicking inside the Field you get a great looking UI Element with wheels to select the values:

it depends from the part of the DateField which wheels appear – clicking into the Time part of a Timestamp you can change the Time.

In this blog entry I don’t talk about HowTo Format DateFields, how to work with Dates, Times, TimeStamps, TimeZones, I18n… – this will be another blog. The examples of this blog always use a full Date including Time.

Now lets see how to set and get Dates to/from this DateField.

You can set a Date using java.util.Date or the long Value of a Date:

After editing the DateField you get the value back always as a long:

If you’re setting valid Dates, editing them and then getting back as long – there will be no surprises,

new Date(myBBDateField.getDate())
myJavaDate.setTime(myBBDateField.getDate())

both will give you the correct Date.

But you have to be carefully if your Date is empty. You can set the BlackBerry DateField with empty Dates in two ways:

myBBDateField.setDate(null)
myBBDateField.setDate(Long.MIN_VALUE)

this will produce an empty DateField where only the Formatting is visible:

If you now click into this empty Field, you get the wheels from above where the current Date and Time was preselected. Now you have to select a value. Its not possible to ‘clear’ the Field to get an empty DateField again. If you need this you should provide a MenuItem or a small button beside the Field.

Warning: if you don’t change the empty Date and you’re using getDate() from BlackBerry DateField you always get Long.MIN_VALUE as result. Don’t put this into your Java Date – otherwise next time you’ll see this:

You can avoid this:

public void setDate(long date) {
	if (date == Long.MIN_VALUE) {
		this.date = null;
		return; }
	if (this.date != null) {
		this.date.setTime(date);
	} else {
		this.date = new Date(date); }
}

hint: all work was done using BlackBerry Java SDK 5.0.0.25, Eclipse PlugIn 1.1.2 and tested using Devices + Simulators on 9700 + 9550

back to the overview

if you like my work, you can Flattr this

Leave a comment