Skip to main content

Java Date and Time Solution | Returns the day on that date.

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. To simplify your task, we have provided a portion of the code in the editor. You just need to write the method, , which returns the day on that date.
For example, if you are given the date , the method should return  as the day on that date.
Input Format
A single line of input containing the space separated month, day and year, respectively, in   format.
Constraints
Output Format
Output the correct day in capital letters.
Sample Input
08 05 2015
Sample Output
WEDNESDAY
Explanation
The day on August th  was WEDNESDAY.
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 intyear = Integer.parseInt(year);
                int intDay = Integer.parseInt(day);
                int intmonth = Integer.parseInt(month);
        
                String dateString = String.format("%d-%d-%d", intyear, intmonth, intDay);
                Date date = null;
                try {
                     date = new SimpleDateFormat("yyyy-M-d").parse(dateString);
                }
                catch(ParseException ref){
                    ref.printStackTrace();
                }  
            
                // Then get the day of week from the Date based on specific locale.
                String dayOfWeek = new SimpleDateFormat("EEEE", Locale.ENGLISH).format(date);
                
                return dayOfWeek.toUpperCase();
    }
    
   
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String month = in.next();
        String day = in.next();
        String year = in.next();
        
        System.out.println(getDay(day, month, year));
    }
}

Comments

  1. The blog article very surprised to me! Your writing is good related to personal care In this I learned a lot! Thank you!, please checkout more information on Lotus Notes xpages Consultant

    ReplyDelete
  2. nice information for the freshers.thank you.
    learn java tutorial

    ReplyDelete
  3. Great Content. It will useful for knowledge seekers. Keep sharing your knowledge through this kind of article.
    IELTS Writing Skills
    IELTS Writing Tasks

    ReplyDelete
  4. I'm really impressed with your writing skills, as smart as the structure of your weblog.

    GoLand Full Crack

    Waves v12 Complete Crack

    ReplyDelete
  5. I am very impressed with your post because this post is very beneficial for me and provide a new knowledge to me
    https://vstpatch.net/goland-crack/
    https://vstpatch.net/wondershare-allmymusic/
    https://vstpatch.net/soundtheory-gullfoss/
    https://vstpatch.net/downie/

    ReplyDelete
  6. Thank you for the useful information. Share more updates.
    Vocabulary
    IELTS Map


    ReplyDelete

Post a Comment