如何获得在android studio中使用Calendar的给定日期的工作日

时间:2015-06-19 16:42:16

标签: android

这是我的代码:

// Date birthDay;
            GregorianCalendar birthDay = new GregorianCalendar();

            birthDay.set(year_int,mo,da,0,0,0);
            int weekday = birthDay.get(GregorianCalendar.DAY_OF_WEEK);
            String weekDay = String.valueOf(weekday -1);
            weekDay = weekDay.replace("0","Sunday.");
            weekDay = weekDay.replace("1","Monday.");
            weekDay = weekDay.replace("2","Tuesday.");
            weekDay = weekDay.replace("3","Wednesday.");
            weekDay = weekDay.replace("4","Thursday.");
            weekDay = weekDay.replace("5","Friday.");
            weekDay = weekDay.replace("6","Saturday.");

这里有什么问题?请帮我。

1 个答案:

答案 0 :(得分:0)

使用joda-time代替。

DateTime birthDay = new DateTime(year, mo,day, 0, 0, 0);
DateTimeFormatter format = DateTimeFormat.forPattern("EEEE");
String weekDay = birthday.toString(format);

将返回星期几。

如果您想使用日历:

SimpleDateFormat sdf = new SimpleDateFormat("EEEE");    
Calendar calendar = new GregorianCalendar(year, mo,day, 0, 0, 0);   
String weekDay =  sdf.format(calendar.getTime());