由日期转换成星期几

来源:互联网 发布:青岛华为软件开发 编辑:程序博客网 时间:2024/05/16 08:27

  1.字符串类型的日期格式转换成星期几

String time=new SimpleDateFormat("yyyy-MM-dd").format(new Date());

                        dayForWeek(time)

public static String dayForWeek(String pTime) throws Exception {

    DateFormatformat = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    String[] weekDaysName = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; 
    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 weekDaysName[dayForWeek];

    }

2.Data类型的日期格式转换成星期几

  Date da=new Date();
    getWeekOfDate(da);

public static String getWeekOfDate(Date date) { 
     String[] weekDaysName = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; 
     String[] weekDaysCode = { "0", "1", "2", "3", "4", "5", "6" }; 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTime(date); 
     int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1; 
     return weekDaysName[intWeek]; 
   

原创粉丝点击