+ 1

Can someone Pls help me with writing a program to display the day when we enter the date Month and year?

18th Nov 2016, 6:01 AM
Prarabdh Garg
Prarabdh Garg - avatar
7 Answers
+ 3
import java.time.LocalDate; import java.util.Scanner; public class Testclass { public static void main(String[] args) { // Getting user input System.out.println("Please enter Date (yyyy-mm-dd):"); Scanner in = new Scanner(System.in); String inputDate = in.nextLine(); // Converting to int String[] splitDate = inputDate.split("-"); int year = Integer.valueOf(splitDate[0]); int month = Integer.valueOf(splitDate[1]); int day = Integer.valueOf(splitDate[2]); // Weekday of the given date LocalDate localDate = LocalDate.of(year, month, day); System.out.println(localDate.getDayOfWeek()); } } Note: there is no Exceptionhandling if the user enters bad values.
18th Nov 2016, 9:19 AM
Roland
+ 1
Here are the steps: 1) Create variables year_code, month_code, code (all int). Initialise them to 0. 2) Prompt user for year, month and date. Store these values in variables year, month, date respectively (all int). 3) Use the % operator to find out the first 2 and last 2 digits of the year. We'll store the first 2 digits in a variable f2d and last 2 digits in l2d. 4) Set year_code to l2d, then increase its value by (l2d / 4). year_code should have 1.25 times the value of l2d, ignoring remainders. 5) Look at f2d. Initialise j to f2d % 4. If j is 1, add 5 to year_code; if j is 2, add 3 instead; if j is 3, add 1 instead; if j is 0, leave it. 6) year_code %= 7 7) Look at the month. Convert it to numbers 1 - 12 for simplicity's sake. 8) If month is 1 and it's not a leap year, set month_code to 6. If it's a leap year, set it to 5. For simplicity's sake (mc is month_code): month: 1, mc: 6, if leap then mc: 5 month: 2, mc: 2, if leap then mc: 1 month: 3 or 11, mc: 2 month: 4 or 7, mc: 5 month: 5, mc: 0 month: 6, mc: 3 month: 8, mc: 1 month: 9 or 12, mc: 4 month: 10, mc: 6 9) Add the date, month_code and year_code together. Store the result in code. 10) code %= 7 11) If code is 0, print "Sunday"; if code is 1, print "Monday", if code is 2, print "Tuesday", etc. 12) We're done! Hopefully this is helpful.
18th Nov 2016, 7:33 AM
Samuel Neo
Samuel Neo - avatar
0
Just to confirm, do you mean day of the week (Monday, Tuesday, etc.)?
18th Nov 2016, 6:05 AM
Samuel Neo
Samuel Neo - avatar
0
I'm sorry, I only have a C# code for that.
18th Nov 2016, 6:18 AM
Samuel Neo
Samuel Neo - avatar
0
Yes @ Samuel Neo
18th Nov 2016, 6:22 AM
Prarabdh Garg
Prarabdh Garg - avatar
0
Why don't you explain me the logic in brief @Samuel Neo
18th Nov 2016, 6:23 AM
Prarabdh Garg
Prarabdh Garg - avatar
0
Apparently there's a getDayOfWeek() method in Java as shown in Roland's answer. You can use that.
18th Nov 2016, 8:29 AM
Samuel Neo
Samuel Neo - avatar