如何比较字符串与日历日期?

时间:2011-08-26 15:50:53

标签: android calendar

我使用它来阅读HTML项目列表。

while (postIt.hasNext()) {
    Element name = postIt.next();
    nameOf = name.text();

    String platform = postIt.next().text();

    //Get the URL
    Element url = name.select("a").first();
    urlString = url.attr("href");

    //Get the Genre of the item
    String genre = postIt.next().text();

    //Get the date
    Date = postIt.next().text();
}

我想要做的是将日期字符串与Android设备上当前日期的字符串进行比较? 我将如何通过日历进行此操作?

1 个答案:

答案 0 :(得分:5)

我会使用DateSimpleDateFormat

执行此操作
Date d = new Date(); //todays date (current time)

//set the pattern here to look like the pattern you are expecting in your 'Date' variable
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

String formattedDate = sdf.format(d);
//for today the string would look like this: "2011-08-26"

boolean isToday = formattedDate.equals(Date); //your answer...

如果您不熟悉SimpleDateFormatter,请查看documentation here。它有很多有用的信息来设置你的模式。