Java Date and Time

Sort by

recency

|

1510 Discussions

|

  • + 1 comment

    In Java 15 solution

    public static String findDay(int month, int day, int year) {
        Calendar calendar = Calendar.getInstance();
        var dayOfWeek = calendar.get(calendar.DAY_OF_WEEK);
        String[] names = new String[] {"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};
        return names[dayOfWeek - 1];
    }
    
  • + 0 comments

    In this code there is one cofusion every begginer will face that is why month -1 is there in this line of code:

    calendar.set(year, month-1, day);

    Explanation : Calendar class represents months as 0-indexed.

    So January is 0, February is 1, ... November is 10 and December is 11. So if we are giving Aughust which 8th month. you must pass 7 (i.e., 8 - 1).

    as

  • + 0 comments

    Java 8 solution:

    public static String findDay(int month, int day, int year) { LocalDate date = LocalDate.now(); if(year >2000 && year < 3000) { date = LocalDate.of(year, month, day);
    } return date.getDayOfWeek().toString(); }

  • + 0 comments
        public static String findDay(int month, int day, int year) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(year, month-1, day);
            int dayOfweek = calendar.get(Calendar.DAY_OF_WEEK);
            
            String[] days = {"", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
            
            return days[dayOfweek].toUpperCase();
        }
    
  • + 0 comments
    // SimpleDateFormat with the pattern "EEEE" formats the date into the full name of the day
    
    public static String findDay(int month, int day, int year) {
            Calendar c = Calendar.getInstance();
            c.set(year, month-1, day);
            
            return new java.text.SimpleDateFormat("EEEE", Locale.ENGLISH).format(c.getTime()).toUpperCase();
            
    
        }