views:

340

answers:

2

Hi there,

I am displaying some event data based on the todays event and a list of events in a week. Currently I am displaying all the events in form of list from the file, As the file contains out dated events as well but I want to display on the basis of today's date events and a week events then week after. In short I want to restrict the list on the basis of this and extract information. I know there is a class java.util containing Date class, but need some quick idea and help how can I do this? Can anyone quote example?

Thanks

+3  A: 

If you get a new instance of a java.util.Date or java.util.Calendar object it will be set to the current date and time, down to the millisecond. The Calendar object will also be set to the current timezome of the device.

Using Date or Calendar you should be able to easily check if the dates coming in from your file fit within a certain time frame relative to the current date and time.

Date and Calendar are just standard Java classes, these aren't specific to Android. I'm sure there are tons of tutorials on using these classes on the internet if you do a search.

mbaird
I tried to use `Calendar.get(Calendar.DATE);` but its giving error `Cannot make a static reference to a non-static method get(int) of type calendar`
rob
Sounds like you might need to brush up on basic Java. You need to get an instance of a Calendar object first, before you can call non-static methods. You do this by calling `Calendar.getInstance()` or by calling a constructor of a concrete Calendar implementation such as `new GregorianCalendar()`
mbaird
+3  A: 
//get an instance
Calendar calToday = Calendar.getInstance();

//initialise the start or current date
Date today = calendar.getTime();

//find the week start date
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
cal.add(Calendar.DATE, dayofweek*-1);
Date startWeek = calendar.getTime();

//compare if the date from file falls in the current week
if (today.compareTo(fileDate)<=0 && startWeek.compareTo(fileDate)>=0) 

Then you can get the end and start dates of the next consecutive week by doing adding -1 and -7 to the current startWeek

OR you can also try this way

//get an instance
Calendar cal = Calendar.getInstance();

//set date from file
cal.setTime(fileDate);

//find week of year
int weekOfYear = cal.get(Calendar.WEEK_OF_YEAR))

assign it according to List or Array for display

To format date

String strFileDate = "20100316T0305";
DateFormat formatter = new SimpleDateFormat("YYYYMMDDThhmm");
Date fileDate = (Date)formatter.parse(strFileDate); 
zapping
I have tried to do this but the application stops working and ask force to close. The date format in the file that needs to be check is in the format of YYYYMMDDThhmm where 'T' is just a character, on the other hand the date format available that we get using Calendar is like Mon 15 March 2010 GMT time. I tried to use the string functions to convert is as required but it doesn't work. Any suggestions?
rob
@rob the above answer is edited to include string to date conversion.
zapping