java 日期转星期

/**
     * 判断当前日期是星期几<br>
     * <br>
     * 
     * @param pTime
     *            修要判断的时间<br>
     * @return dayForWeek 判断结果<br>
     * @Exception 发生异常<br>
     
*/
    public int getDayForWeek(String pTime) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(format.parse(pTime));
        int dayForWeek = 0;
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            dayForWeek = 7;
        } else {
            dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
        }
        return dayForWeek;
    }

    /**
     * 判断当前星期中文名
     
*/
    public String getWeekName(int week) {
        String weekName = "周日";
        switch (week) {
        case 1: {
            weekName = "周一";
            break;
        }
        case 2: {
            weekName = "周二";
            break;
        }
        case 3: {
            weekName = "周三";
            break;
        }
        case 4: {
            weekName = "周四";
            break;
        }
        case 5: {
            weekName = "周五";
            break;
        }
        case 6: {
            weekName = "周六";
            break;
        }
        default: {
            break;
        }
        }
        return weekName;
    }

    public String getWeekNameForDay(int days) {
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = new Date();
        dt.setDate(dt.getDate() + days);
        String date = sDateFormat.format(dt);
        int onlineDay = 0;
        try {
            onlineDay = getDayForWeek(date);
            date = getWeekName(onlineDay);
        } catch (Exception e) {

        }
        return date;
    }
posted @ 2012-11-09 09:37  小さいです哥  阅读(676)  评论(0编辑  收藏  举报