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);

    }

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM