How to determine day of week by passing specific date?

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

    This works correctly...

    java.time.LocalDate; //package related to time and date
    

    It provides inbuilt method getDayOfWeek() to get the day of a particular week:

    int t;
    Scanner s = new Scanner(System.in);
    t = s.nextInt();
    s.nextLine();
     while(t-->0) { 
        int d, m, y;
        String ss = s.nextLine();
        String []sss = ss.split(" ");
        d=Integer.parseInt(sss[0]);
        m = Integer.parseInt(sss[1]);
        y = Integer.parseInt(sss[2]);
    
        LocalDate l = LocalDate.of(y, m, d); //method to get the localdate instance
        System.out.println(l.getDayOfWeek()); //this returns the enum DayOfWeek
    

    To assign the value of enum l.getDayOfWeek() to a string, you could probably use the method of Enum called name() that returns the value of enum object.

提交回复
热议问题