用java将字符串“2014/04/19”转换成date类,并求出那天是星期几。哪位大神会,帮我解答一下

如题所述

package test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class B{
    public static void main(String[] args) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            Date date = sdf.parse("2014/04/19");
            Calendar cal=Calendar.getInstance();
            cal.setTime(date);
            String week = getWeekDay(cal);
            System.out.println(week);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    
    private static String getWeekDay(Calendar c){
        if(c == null){
         return "星期一";
        }
       
        else if(Calendar.MONDAY == c.get(Calendar.DAY_OF_WEEK)){
         return "星期一";
        }
        else if(Calendar.TUESDAY == c.get(Calendar.DAY_OF_WEEK)){
         return "星期二";
        }
        else if(Calendar.WEDNESDAY == c.get(Calendar.DAY_OF_WEEK)){
         return "星期三";
        }
        else if(Calendar.THURSDAY == c.get(Calendar.DAY_OF_WEEK)){
         return "星期四";
        }
        else if(Calendar.FRIDAY == c.get(Calendar.DAY_OF_WEEK)){
         return "星期五";
        }
        else if(Calendar.SATURDAY == c.get(Calendar.DAY_OF_WEEK)){
         return "星期六";
        }
        else if(Calendar.SUNDAY == c.get(Calendar.DAY_OF_WEEK)){
         return "星期日";
        }
        else{
         return "星期一";
        }
        
     }

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-05-08
public class Test {

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

String str="2014/04/19";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy/MM/dd");
Date date = sdf.parse(str);
Calendar c = new GregorianCalendar();
c.setTime(date);

System.out.println(c.get(Calendar.DAY_OF_WEEK)-1);
System.out.println(date.getDay());

}
}本回答被提问者采纳
大家正在搜