Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
sergiotapia
Posts: 33
Registered: ‎10-01-2010
My Device: Not Specified
Accepted Solution

How can I get the current date?

Hi there! :smileyhappy:

 

I have two EditFields that will act as a filter so a user can say show me records from here to here.

 

I'd like the dates to default from TODAY to 7 days prior.

 

Here is the code I currently have:

 

 

Border border = BorderFactory.createSimpleBorder(new XYEdges(1,1,1,1));		
txtStartDate = new EditField("", "");
txtStartDate.setBorder(border);
Background txtBackground = BackgroundFactory.createSolidBackground(0xFFFFFF);
txtStartDate.setBackground(txtBackground);
add(txtStartDate);

Border borderB = BorderFactory.createSimpleBorder(new XYEdges(1,1,1,1));		
txtEndDate = new EditField("", "");
txtEndDate.setBorder(borderB);
Background txtBackgroundB = BackgroundFactory.createSolidBackground(0xFFFFFF);
txtEndDate.setBackground(txtBackgroundB);
add(txtEndDate);

 

Is there something like a DateTime.Now in Blackberry? How can I achieve this? Thanks a lot for the help guys. :smileyvery-happy:

 

Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: How can I get the current date?

For the current Date

 

Use this

Date date;
String currentdate = new SimpleDateFormat(":"+"dd/MM/yyyy"+":").format(new Date(System.currentTimeMillis()));

 ------------------------------------------------------------------------------------
Press Kudo to say thank to developer.
Also Press the Accept as solution Button when u got the Solution.

 

Contributor
sergiotapia
Posts: 33
Registered: ‎10-01-2010
My Device: Not Specified

Re: How can I get the current date?

Thanks, that worked, but two colons appear at the beginning and end of the string. Any ideas why? I just want the application to show the date as in "01/10/2010". 

 

Also, how can I subtract 7 days from this and display that? 

 

Thanks a lot for your help man, it's really useful to me.

Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: How can I get the current date?

Ok........

Use this...

 

 

Date date;
String currentdate = new SimpleDateFormat("dd/MM/yyyy").format(new Date(System.currentTimeMillis()));



------------------------------------------------------------------------------------
Press Kudo to say thank to developer.
Also Press the Accept as solution Button when u got the Solution.
Developer
bedathur
Posts: 25
Registered: ‎11-23-2008
My Device: BlackBerry Bold

Re: How can I get the current date?

To increment/decrement hours, days, months, etc., check the .add method in Calendar class.

 

Something like:

  

 

Calendar cal = Calendar.getInstance();
int daysOffset = -7;
cal.add(Calendar.DATE, daysOffset);

 

 

Developer
noopesh
Posts: 60
Registered: ‎05-04-2011
My Device: Torch 9800, Bold 9700, Curve 9380

Re: How can I get the current date?

I want my date to displayed like that ;

Wed, 29 Feb 2012 07: 22: 25GMT

Is it possible ?
Developer
noopesh
Posts: 60
Registered: ‎05-04-2011
My Device: Torch 9800, Bold 9700, Curve 9380

Re: How can I get the current date?

How to get the day from the date...???
like 29 feb,2012 day is Wed.
How to get that thing ?
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: How can I get the current date?

@noopesh - I recommend that you don't post in Solved Threads.  Start a new Thread for a new problem.

 

In your specific case, I would search the forum before asking.  Approaches to parsing String dates and extracted parts of them have been discussed frequently.

Developer
noopesh
Posts: 60
Registered: ‎05-04-2011
My Device: Torch 9800, Bold 9700, Curve 9380

Re: How can I get the current date?

okay... keep that thing in mind peter...
Developer
noopesh
Posts: 60
Registered: ‎05-04-2011
My Device: Torch 9800, Bold 9700, Curve 9380

Re: How can I get the current date?

.
DateFormat dateFormat = new SimpleDateFormat( "EEE, dd MMM yyyy HH: mm: ss'GMT'");

Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

c.setTime(new Date(miliseconds));

String formattedDate = dateFormat.format(c, new StringBuffer(), null).toString();

System.out.println(formattedDate);