सीधे मुख्य सामग्री पर जाएं

HackerRank Java Date and Time | Problem Solution

HackerRank Java Date and Time | Problem Solution








 

Problem:

The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.


You are given a date. You just need to write the method, , which returns the day on that date. To simplify your task, we have provided a portion of the code in the editor.

Example


The method should return  as the day on that date.

image

Function Description

Complete the findDay function in the editor below.

findDay has the following parameters:

  • int: month
  • int: day
  • int: year

Returns

  • string: the day of the week in capital letters

Input Format

A single line of input containing the space-separated month, day, and year, respectively, in format.

Constraints

Sample Input

08 05 2015

Sample Output

WEDNESDAY

Explanation

The day on August th  was WEDNESDAY.


Solution 1:

import java.util.Scanner;
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Solution{
    public static String getDay(String day,String month,String year){
         int y = Integer.parseInt(year);
        int d = Integer.parseInt(day);
        int m = Integer.parseInt(month);
        
        String dateString = String.format("%d-%d-%d",y,m,d);
        
        Date date = null;
        
        try{
            date = new SimpleDateFormat("yyyy-M-d").parse(dateString);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        
        String dayOfWeek = new SimpleDateFormat("EEEE",Locale.ENGLISH).format(date);
        return dayOfWeek.toUpperCase();
    }

    
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        String month = scanner.next();
        String day = scanner.next();
        String year = scanner.next();
        System.out.println(getDay(day,month,year));
    }
    
}







Solution 2:



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;

class Result {
    
    public static String findDay(int month, int day, int year) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH,month-1);
        cal.set(Calendar.DAY_OF_MONTH,day);
        cal.set(Calendar.YEAR,year);
String dayOfWeek = cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.US).toUpperCase();
        return dayOfWeek;
    }
}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
        String[] firstMultipleInput = br.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);
        bw.write(res);
        bw.newLine();
        br.close();
        bw.close();
    }
}


Solution 3:


import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            int month = scanner.nextInt();
            int day = scanner.nextInt();
            int year = scanner.nextInt();

            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DATE, day);
            calendar.set(Calendar.MONTH, month - 1);
            calendar.set(Calendar.YEAR, year);

            System.out.println(calendar.getDisplayName(Calendar.DAY_OF_WEEK,
                    Calendar.LONG, new Locale("en""US")).toUpperCase());
        }
    }
}

Solution 4:


import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Scanner;

public class JavaDateAndTime {
    static List<String> days = Arrays.asList("SUNDAY""MONDAY""TUESDAY""WEDNESDAY""THURSDAY""FRIDAY""SATURDAY");
    
    public static String getDay(String day, String month, String year) {
          
        int y = Integer.parseInt(year);
        int m = Integer.parseInt(month);
        int d = Integer.parseInt(day);
          
        Calendar c = Calendar.getInstance();
        c.set(y, m-1, d); 
            
        int p = c.get(Calendar.DAY_OF_WEEK);
        String s = days.get(p-1);
        return s;
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String month = scanner.next();
        String day = scanner.next();
        String year = scanner.next();
        
        System.out.println(getDay(day, month, year));
        
    }
}



टिप्पणियाँ

इस ब्लॉग से लोकप्रिय पोस्ट