Java Date and Time

  • + 3 comments

    Java solution - passes 100% of test cases

    Use LocalDate (available in Java 8 but not Java 7) instead of Calendar. It's simpler.

    import java.util.Scanner;
    import java.time.LocalDate;
    
    public class Solution {
    
        public static void main(String[] args) {
            /* Read input */
            Scanner scan = new Scanner(System.in);
            int month = scan.nextInt();
            int day   = scan.nextInt();
            int year  = scan.nextInt();
            scan.close();
            
            /* Create LocalDate instance */
            LocalDate date = LocalDate.of(year, month, day);
            System.out.println(date.getDayOfWeek());
        }
    }
    

    From my HackerRank Java solutions.