JAVA判断当前日期是星期几

package test.ns;


import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;


public class Demo {

    public static String dayForWeek(String pTime) throws Throwable {  

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  

        Date tmpDate = format.parse(pTime);  

        Calendar cal = Calendar.getInstance(); 

        String[] weekDays = { "7", "1", "2", "3", "4", "5", "6" };

        try {

            cal.setTime(tmpDate);

        } catch (Exception e) {

            e.printStackTrace();

        }

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。

        if (w < 0)

            w = 0;

        return weekDays[w];

    }  

    public static void main(String[] args) throws Throwable {

        String a = dayForWeek("2017-09-23");

        System.out.println(a);

    }

}

猜你喜欢

转载自www.cnblogs.com/molecule/p/9270058.html