根据日期获取星期

public class DateToWeekUtil {
    /**
     * 根据日期来获取当前是星期几
     * @param datetime
     * @return
     */
    public String dateToWeek(String datetime) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String[] weekDays = { "7", "1", "2", "3", "4", "5", "6" };
        // 获得一个日历
        Calendar cal = Calendar.getInstance();
        Date date = null;
        try {
            date = format.parse(datetime);
            cal.setTime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int week = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week < 0) {
            week = 0;
        }
        return weekDays[week];
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_41432270/article/details/83277250