Java Date and Time

Sort by

recency

|

1473 Discussions

|

  • + 0 comments

    import java.util.; import java.io.;

    public class DayOfWeekFinder {

    public static String findDay(int day, int month, int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day);
        return calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault()).toUpperCase();
    }
    
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        int month = scanner.nextInt();
        int day = scanner.nextInt();
        int year = scanner.nextInt();
        scanner.close();
    
        String dayOfWeek = findDay(day, month, year);
        System.out.println(dayOfWeek);
    }
    

    }

  • + 0 comments
    public static String findDay(int month, int day, int year) {
        LocalDate 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);
            return calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG,Locale.getDefault()).toUpperCase();
            
        };
    
  • + 0 comments

    I couldn't figure out how to solve the problem using the Calender class.

    So I used LocalDateTime and DateTimeFormatter classes instead. This approach appears clearer to me also. Note :- You would also need to import java.time.LocalDateTime and java.time.format.DateTimeFormatter on the toplevel of the file. This is the code of the method -

    public static String findDay(int month, int day, int year) {
            LocalDateTime date = LocalDateTime.of(year, month, day,0,0);
            
            DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("EEEE");
            
            String formattedDate = date.format(dateFormat).toString();
            
            String DAY = formattedDate.toUpperCase();
            return DAY;
        }
    
  • + 1 comment

    /* * Complete the 'findDay' function below. * * The function is expected to return a STRING. * The function accepts following parameters: * 1. INTEGER month * 2. INTEGER day * 3. INTEGER year */

    public static String findDay(int month, int day, int year) {
        Calendar g = new GregorianCalendar();
        g.set(Calendar.YEAR, year);
        g.set(Calendar.MONTH, month-1);
        g.set(Calendar.DAY_OF_MONTH, day);
        return g.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault()).toUpperCase();
    }