5

如何以字符串形式获取星期几或月份?无需执行以下操作:

        DateTime now = DateTime.now();
        String dayOfWeek = null;
        switch(now.getDayOfWeek()) {
            case DateTimeConstants.MONDAY:
                dayOfWeek = "Monday";
                break;
            case DateTimeConstants.TUESDAY:
                dayOfWeek = "Tuesday";
                break;
            case DateTimeConstants.WEDNESDAY:
                dayOfWeek = "Wednesday";
                break;
            case DateTimeConstants.THURSDAY:
                dayOfWeek = "Thursday";
                break;
            case DateTimeConstants.FRIDAY:
                dayOfWeek = "Friday";
                break;
            case DateTimeConstants.SATURDAY:
                dayOfWeek = "Saturday";
                break;
            case DateTimeConstants.SUNDAY:
                dayOfWeek = "Sunday";
                break;
        }
4

2 回答 2

13

好吧,显然它就像这里dateTime.dayOfWeek().getAsText();记录的那样。

于 2012-12-26T18:06:09.403 回答
0

您可以使用上述逻辑创建 DateTime 扩展方法,并在需要的地方使用,而无需重复整个逻辑。

于 2012-12-26T18:07:56.783 回答