tags:

views:

101

answers:

2

I have a date propety set up like this

        cal1.setTime(StartDate.getDate());
        strStartDate = cal1.get(cal1.DAY_OF_MONTH) + "/" +
                 (cal1.get(cal1.MONTH) + 1) + "/" +
                 cal1.get(cal1.YEAR);

And I need to extract it when a search is performed.

I so far have this

            int endOfName = record.indexOf(";");
            int endOfDesc = record.indexOf(";" , endOfName + 1);
            int endOfTown = record.indexOf(";", endOfDesc + 1);
            int endOfPlace = record.indexOf(";", endOfTown + 1);
            int endOfStart = record.indexOf(";", endOfPlace +1);

            String Name = record.substring(0, endOfName);
            String Desc = record.substring(endOfName + 1, endOfDesc);
            String Town = record.substring(endOfDesc + 1, endOfTown);
            String Place= record.substring(endOfTown + 1, endOfPlace);
            String Start =record.substring(endOfPlace +1, endOfStart);
            mListForm.append(Name + " aged: " + Desc + Town + Place + Start);

I have successfully extracted the other data but this does not return anything for the date property can anyone help by giving me the code to extract the data?

Thanks

+2  A: 

Instead of using indexOf and substring multiple times it might be easier to use split.

String [] pieces = record.split(";");

Then you can access the parts of the record using the array. Eg:

String name = pieces[0];
String desc = pieces[1];
String date = pieces[N];

Once you have the date in a String called date, you can call split on that too:

String[] dateParts = date.split("/");

Then you can use the dateParts array get specific information about the date. I'm not very familiar with how to store dates/calendars in java but this site probably can help you.

theycallmemorty
For date parsing/extracting, use JodaTime: http://joda-time.sourceforge.net/
flicken
+1  A: 

I think what you're looking for is the DateFormat class. Try looking at SimpleDateFormat

You can do something like this:

DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date date = df.parse(dateString);
Theo
Yes, SimpleDateFormat should be used for both parsing and formatting of dates, rather then writing it from scratch.
javashlook