0

假设我有一个像 2012 年 1 月 1 日这样的日期,我想看看今天是哪一天,我该怎么做?

4

2 回答 2

4

我假设您将日期作为字符串。在这种情况下,您应该首先使用 SimpleDateFormat 解析日期,然后使用Date.getDay()从该日期获取星期几。

SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy"); 
Date dateObj = curFormater.parse(dateStr);
int day = dateObj.getDay(); 
于 2012-05-07T13:03:44.323 回答
1

通过使用 SimpleDateFormate 对象,我们可以从字符串日期中获取日期:

SimpleDateFormat  format = new SimpleDateFormat("dd/MM/yyyy");  
        try {  
            Date date = format.parse("01/01/2012");  
            switch(date.getDay()) {
            case 0: "sunday";
            break;
            case 1: "monday";
            break;
            ....
            }

        } catch (ParseException e) {
            e.printStackTrace();  
        }
于 2012-05-07T13:07:37.617 回答