Java Date and Time

Sort by

recency

|

1466 Discussions

|

  • + 0 comments

    import java.io.; import java.math.; import java.security.; import java.text.; import java.util.; import java.util.concurrent.; import java.util.function.; import java.util.regex.; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; import java.time.LocalDate; import java.time.DayOfWeek;

    class Result {

    /*
     * 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) {
      LocalDate date = LocalDate.of(year, month, day);
      DayOfWeek dayOfWeek = date.getDayOfWeek();
      return dayOfWeek.toString();
    } 
    

    }

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
    
        int month = Integer.parseInt(firstMultipleInput[0]);
    
        int day = Integer.parseInt(firstMultipleInput[1]);
    
        int year = Integer.parseInt(firstMultipleInput[2]);
    
        String res = Result.findDay(month, day, year);
    
        bufferedWriter.write(res);
        bufferedWriter.newLine();
    
        bufferedReader.close();
        bufferedWriter.close();
    }
    

    }

  • + 1 comment

    import java.util.Calendar;

    class Result {

    /*
     * 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 calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day); // Calendar months are 0-based
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
    
        // Array of day names in uppercase
        String[] days = { "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" };
        return days[dayOfWeek - 1];
    }
    
  • + 0 comments

    import java.time.LocalDate; import java.time.DayOfWeek;

    class Result {

    /*
     * 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) {
        LocalDate firstDay = LocalDate.of(year, month, day);
        DayOfWeek firstDOW = (DayOfWeek) firstDay.getDayOfWeek();
        return String.valueOf(firstDOW);
    }
    
  • + 0 comments

    ** SOLUTION:**

      Calendar c = new GregorianCalendar();
       c.set(year, month-1,day );    
    
          return (c.getDisplayName(c.DAY_OF_WEEK, Calendar.LONG, Locale.ENGLISH)).toUpperCase();
             
    
  • + 0 comments

    This will definitely help me understand how to handle date and time operations in Java better. Thanks again for taking the time to share this! Matchbox 9