我根据今天的活动和一周内的活动列表显示一些活动数据。目前我正在从文件中以列表的形式显示所有事件,因为该文件包含过时的事件,但我想根据今天的日期事件和一周后的事件显示。总之,我想在此基础上限制列表并提取信息。我知道有一个类java.util
包含Date
类,但需要一些快速的想法并帮助我该怎么做?有谁能引用例子?
由于
答案 0 :(得分:6)
//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)
然后你可以通过在当前的startWeek中添加-1和-7来获得下一周的结束和开始日期
或者您也可以尝试这种方式
//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))
根据列表或数组进行分配以显示
格式化日期
String strFileDate = "20100316T0305";
DateFormat formatter = new SimpleDateFormat("YYYYMMDDThhmm");
Date fileDate = (Date)formatter.parse(strFileDate);
答案 1 :(得分:3)
如果获得java.util.Date或java.util.Calendar对象的新实例,它将被设置为当前日期和时间,直到毫秒。 Calendar对象也将设置为设备的当前时间段。
使用日期或日历,您应该能够轻松检查文件中的日期是否适合相对于当前日期和时间的特定时间范围。
日期和日历只是标准的Java类,这些不是特定于Android的。我相信如果你进行搜索,有很多关于在互联网上使用这些课程的教程。