在android应用程序中使用日期函数?

rob*_*rob 0 eclipse datetime android

我正在根据今天的事件和一周内的事件列表显示一些事件数据.目前我正在从文件中以列表的形式显示所有事件,因为该文件包含过时的事件,但我想根据今天的日期事件和一周后的事件显示.总之,我想在此基础上限制列表并提取信息.我知道有一个类java.util包含Date类,但需要一些快速的想法并帮助我该怎么做?谁能引用例子?

谢谢

zap*_*ing 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) 
Run Code Online (Sandbox Code Playgroud)

然后你可以通过在当前的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))
Run Code Online (Sandbox Code Playgroud)

根据List或Array分配它以供显示

格式化日期

String strFileDate = "20100316T0305";
DateFormat formatter = new SimpleDateFormat("YYYYMMDDThhmm");
Date fileDate = (Date)formatter.parse(strFileDate); 
Run Code Online (Sandbox Code Playgroud)