How to determine day of week by passing specific date?

前端 未结 25 1487
夕颜
夕颜 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:55

    Program to find the day of the week by giving user input date month and year using java.util.scanner package:

    import java.util.Scanner;
    
    public class Calender {
        public static String getDay(String day, String month, String year) {
    
            int ym, yp, d, ay, a = 0;
            int by = 20;
            int[] y = new int[]{6, 4, 2, 0};
            int[] m = new int []{0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5};
    
            String[] wd = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    
            int gd = Integer.parseInt(day);
            int gm = Integer.parseInt(month);
            int gy = Integer.parseInt(year);
    
            ym = gy % 100;
            yp = ym / 4;
            ay = gy / 100;
    
            while (ay != by) {
                by = by + 1;
                a = a + 1;
    
                if(a == 4) {
                    a = 0;
                }
            }
    
            if ((ym % 4 == 0) && (gm == 2)) {
                d = (gd + m[gm - 1] + ym + yp + y[a] - 1) % 7;
            } else
                d = (gd + m[gm - 1] + ym + yp + y[a]) % 7;
    
            return wd[d];
        }
    
        public static void main(String[] args) {
    
            Scanner in = new Scanner(System.in);
    
            String day = in.next();
            String month = in.next();
            String year = in.next();
    
            System.out.println(getDay(day, month, year));
        }
    }
    
    0 讨论(0)
提交回复
热议问题