07-14-2009 08:12 AM
Hi,
First i want to convert string to date. and then get a day from that date. can anyone help?
07-14-2009 08:19 AM
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
07-14-2009 08:24 AM
07-14-2009 08:40 AM - edited 07-14-2009 08:45 AM
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
07-14-2009 08:43 AM - edited 07-14-2009 08:44 AM
You would have to parse the string yourself.
Should be easy, the delimiter ist "-"
You could have a look at this Java SE code:
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);
...
07-14-2009 09:29 AM
I don't get clear idea after this Date myDate = calendar.getTime();
How i get day(Sun, Mon) from this??
07-14-2009 11:22 AM
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.
01-07-2010 04:42 AM
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.
01-07-2010 08:15 AM
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.
02-03-2011 01:42 AM
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