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
Developer
Posts: 126
Registered: ‎06-08-2009
My Device: Not Specified

How to get day from date(String).

Hi,

 

 First i want to convert string to date. and then get a day from that date. can anyone help?

Developer
Posts: 212
Registered: ‎07-18-2008
My Device: Storm 9550

Re: How to get day from date(String).

What kind is your String? Do you need something like a DateParser?

 

 

Getting a day from date is easy. Use the calendar class:

 

Calendar.getInstance() 

and set the date with "setTime(Date date)"

 

and then you can check the day with 

int     get(int field)   and one of the attributes, for days: DAY_OF_WEEK

 

 

 

If your problem was solved, please mark answer as "Accepted solution"
If your want to thank, click the "kudo" symbol
___________
visit me: http://mobilejavadevelopment.blogspot.com/
visit the Berlin BlackBerry Developer Group: http://berlinblackberrydevelopers.blogspot.com/
Developer
Posts: 126
Registered: ‎06-08-2009
My Device: Not Specified

Re: How to get day from date(String).

My String is "2008-10-12" i want to get a day of this date.
Developer
Posts: 5,339
Registered: ‎09-20-2008
My Device: ***
My Carrier: ***

Re: How to get day from date(String).

[ Edited ]

Use substring() method of your String instance to get day, month and year separately.

After that use Calendar class instance, for example:

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonthFromYourStringConvertedToInt);
calendar.set(Calendar.MONTH, monthFromYourStringConvertedToInt);
calendar.set(Calendar.YEAR, yearFromYourStringConvertedToInt);


so, now you can get date from the calendar via:

Date myDate = calendar.getTime();


Please note that months for calendar are counting in different way.


The best way is implement a conversion method that will get a friendly month number: 1..12 and will return one of constants:


Calendar.JANUARY .... Calendar.DECEMBER

Message Edited by tbilisoft on 14-07-2009 01:45 PM
Developer
Posts: 212
Registered: ‎07-18-2008
My Device: Storm 9550

Re: How to get day from date(String).

[ Edited ]

You would have to parse the string yourself.

Should be easy, the delimiter ist "-"

 

You could have a look at this Java SE code:

http://demo.spars.info/j/frameset.cgi?compo_id=353243&packagename=org.w3c.util&componame=org.w3c.uti...

 

It uses not available classes and methods but should give you an idea:

 

Split up the string with the "-" character, convert to Integer and then set the parts on Calendar instance:

 

calendar.set(Calendar.YEAR, year); 

calendar.set(Calendar.MONTH, month);

calendar.set(Calendar.DAY_OF_MONTH, day);

 

...

 

 

 

 

 

Message Edited by dpreussler on 07-14-2009 08:44 AM
If your problem was solved, please mark answer as "Accepted solution"
If your want to thank, click the "kudo" symbol
___________
visit me: http://mobilejavadevelopment.blogspot.com/
visit the Berlin BlackBerry Developer Group: http://berlinblackberrydevelopers.blogspot.com/
Developer
Posts: 126
Registered: ‎06-08-2009
My Device: Not Specified

Re: How to get day from date(String).

I don't get clear idea after this Date myDate = calendar.getTime();

 

How i get day(Sun, Mon) from this?? 

Developer
Posts: 126
Registered: ‎06-08-2009
My Device: Not Specified

Re: How to get day from date(String).

Got it.

 

As you mentioned - 

 

>> Please note that months for calendar are counting in different way.


>> The best way is implement a conversion method that will get a friendly month number: 1..12 and will return one of constants:


>> Calendar.JANUARY .... Calendar.DECEMBER

 

My observation is calendar gives next month

 

Can i use 

 

int iMonth = Integer.parseInt(strMonth)-1;

 

Please suggest me.

 

 

 

New Developer
Posts: 9
Registered: ‎12-23-2009
My Device: Not Specified

Re: How to get day from date(String).

Hi,

I am making one calender application in which user is able to store their daily events ,and also able to go next and previous month .i am new in blackberry,will u plz help me in order to do this.

 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to get day from date(String).

I don't think it is appropriate to ask new (and unrelated) questions on existing Threads.  So if you have a new questions, please start a new Thread.

 

With respect to this question, there is lot to it.  Remember this is a forum to answer technical questions, not a training forum.

 

In this case I suggest you look at the samples provided by RIM, especially the PIM sample that does some work with the Calendar.  Get that working, look at and understand how it works, then you will be in a better position to ask more appropriate questions on this forum

 

Hope this helps and good luck.

New Contributor
Posts: 8
Registered: ‎01-05-2011
My Device: 8330

Re: How to get day from date(String).

I want to convert string date(date format is: dd/MM/yyyy) to Date or long.
I am using calender to store date.

Here is my code:
String strDate = 01/01/2011
Calendar oCalendar = Calendar.getInstance();
        oCalendar.set(Calendar.DAY_OF_MONTH, 01);
        oCalendar.set (Calendar.MONTH,01 );
        oCalendar.set(Calendar.YEAR, 2011);
        oCalendar.setTime(oCalendar.getTime());

Date oDate = oCalendar.getTime(); -> If convert this date to string it gives me  "01/02/2011" instead of original date.
What am I doing wrong?

 

Thanks & Regards,

Medha