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 开发者_JAVA技巧can I do this? Can anyone quote example?
Thanks
//get an instance
Calendar cal = Calendar.getInstance();
//initialise the start or current date
Date today = cal.getTime();
//find the week start date
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
cal.add(Calendar.DATE, dayofweek*-1);
Date startWeek = cal.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);
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.
精彩评论