How to determine day of week by passing specific date?

前端 未结 25 1821
夕颜
夕颜 2020-11-22 05:12

For Example I have the date: \"23/2/2010\" (23th Feb 2010). I want to pass it to a function which would return the day of week. How can I do this?

I

25条回答
  •  自闭症患者
    2020-11-22 05:39

    Adding another way of doing it exactly what the OP asked for, without using latest inbuilt methods:

    public static String getDay(String inputDate) {
        String dayOfWeek = null;
        String[] days = new String[]{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
    
        try {
            SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy");
            Date dt1 = format1.parse(inputDate);
            dayOfWeek = days[dt1.getDay() - 1];
        } catch(Exception e) {
            System.out.println(e);
        }
    
        return dayOfWeek;
    }
    

提交回复
热议问题