views:

36

answers:

1

In my Blackberry application I'd like to get today's date so I can display it in a textbox.

Similar to C#'s DateTime.Now. I'm using two EditFields to act as a filter so a user can say show me records between today and 7 days ago.

So I need:

  1. Show today's date as default.
  2. Show the date 7 days prior to today.

Thanks a bunch for the help.

+4  A: 

I think you want java.util.Date and RIM's DateField (not the J2ME one):

Date d = new Date(System.currentTimeMillis());
DateField df = new DateField();
df.setDate(d);

To show seven days prior:

d.setTime(d.getTime() - 7*24*60*60*1000);
Date sevenPrior = new Date(d.getTime() - 7*24*60*60*1000);

Not sure if you want to display both dates, but at this point you could have two Date objects and two DateFields, or change the one DateField with another call to DateField.setDate()...

By the way, I forgot that DateField allows the user to edit the displayed date by default. If you just want to display it with formatting and also don't want the user to focus on the field (so there is no blue border around it do this:

DateField df = new DateField(DateField.NON_FOCUSABLE);
df.setEditable(false)

I think you probably want to use DateFormat to get only the fields you want:

df.setFormat(DateFormat.getInstance(DateFormat.DATE_MEDIUM));

DateFormat.DATE_MEDIUM formats the date like "Mar 08, 2006"

Alternatively take a look at SimpleDateFormat - apparently RIM has implementations of these classes from standard Java which are compatible.

reference:

http://www.blackberry.com/developers/docs/4.3.0api/java/util/Date.html

http://www.blackberry.com/developers/docs/4.3.0api/net/rim/device/api/ui/component/DateField.html

http://www.blackberry.com/developers/docs/4.3.0api/net/rim/device/api/i18n/DateFormat.html

http://www.blackberry.com/developers/docs/4.3.0api/net/rim/device/api/i18n/SimpleDateFormat.html

spacemanaki
Ok, that solves the current date. How can I get the date 7 days prior so the span is a week?
Serg
Edited to add it
spacemanaki
Thanks so much! There's a little problem though, the DateField is showing time as well, how can I tell it to only show me the date and not the time?
Serg
Edited again - use DateFormat or SimpleDateFormat
spacemanaki