2011-08-26 147 views
0

我正在使用它来读取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设备上当前日期的日期字符串进行比较? 我将如何去通过日历做这件事?

回答

4

我会做到这一点使用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。它有很多有用的信息来设置你的模式。

+0

好的很酷的声音就像我需要的!那么我会在哪里比较我的日期,我从我的HTML到你上面描述的日期? – yoshi24

+1

这是你需要的地方。我上面发布的代码几乎可以放在任何地方! –

+0

明白了!好的,还有一件事......是否可以将格式设置为2011年8月26日?我没有在文档中看到它。 – yoshi24

相关问题