Java Date and Time

Sort by

recency

|

1472 Discussions

|

  • + 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();
    }
    
  • + 1 comment

    Hi i am not able to solve some this problem because they have used built-in functions. So where can i learn these concepts as hackerrank is solving the questions but we need know the basics.