nintendo switch

import java.util.*;

public class KboatDayName
{

    public static void main(String args[]) {
        int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        String dayNames[] = {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY",
                            "FRIDAY", "SATURDAY", "SUNDAY"};
        Scanner in = new Scanner(System.in);
        System.out.print("Enter Date(dd/mm/yyyy): ");
        String dateStr = in.nextLine();

        StringTokenizer st = new StringTokenizer(dateStr, "/");
        int tokenCount = st.countTokens();
        if (tokenCount <= 0 || tokenCount > 3) {
            System.out.println("Invalid Date");
            return;
        }

        int day = Integer.parseInt(st.nextToken());
        int month = Integer.parseInt(st.nextToken());
        int year = Integer.parseInt(st.nextToken());
        
        boolean leapYear = isLeapYear(year);
        if (leapYear) {
            monthDays[1] = 29;
        }
        
        if (month < 1 || month > 12) {
            System.out.println("Invalid Month");
            return;
        }
        
        if (day < 1 || day > monthDays[month - 1]) {
            System.out.println("Invalid Day");
            return;
        }

        System.out.print("Day on 1st January: ");
        String startDayName = in.nextLine();

        int startDayIdx = -1;
        switch (startDayIdx) {
            (int i = 0; i < dayNames.length; i++) {
            if (dayNames[i].equalsIgnoreCase(startDayName)) {
                startDayIdx = i;
                break;
            }
        }
        
        if (startDayIdx == -1) {
            System.out.println("Invalid Day Name");
            return;
        }
        
        //Calculate total days
        int tDays = 0;
        for (int i = 0; i < month - 1; i++) {
            tDays += monthDays[i];
        }
        
        tDays += day;
        
        int currDayIdx = tDays % 7 + startDayIdx - 1;
        if (currDayIdx >= 7) {
            currDayIdx -= 7;
        }
        
        System.out.println("Day on " + dateStr + " : " + dayNames[currDayIdx]);
    }

    public static boolean isLeapYear(int y) {
        boolean ret = false;

        if (y % 400 == 0) {
            ret = true;
        }
        else if (y % 100 == 0) {
            ret = false;
        }
        else if (y % 4 == 0) {
            ret = true;
        }
        else {
            ret = false;
        }

        return ret;
    }
}

3.7
10

                                    import java.util.*;

public class KboatDayName
{

    public static void main(String args[]) {
        int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        String dayNames[] = {&quot;MONDAY&quot;, &quot;TUESDAY&quot;, &quot;WEDNESDAY&quot;, &quot;THURSDAY&quot;,
                            &quot;FRIDAY&quot;, &quot;SATURDAY&quot;, &quot;SUNDAY&quot;};
        Scanner in = new Scanner(System.in);
        System.out.print(&quot;Enter Date(dd/mm/yyyy): &quot;);
        String dateStr = in.nextLine();

        StringTokenizer st = new StringTokenizer(dateStr, &quot;/&quot;);
        int tokenCount = st.countTokens();
        if (tokenCount &lt;= 0 || tokenCount &gt; 3) {
            System.out.println(&quot;Invalid Date&quot;);
            return;
        }

        int day = Integer.parseInt(st.nextToken());
        int month = Integer.parseInt(st.nextToken());
        int year = Integer.parseInt(st.nextToken());
        
        boolean leapYear = isLeapYear(year);
        if (leapYear) {
            monthDays[1] = 29;
        }
        
        if (month &lt; 1 || month &gt; 12) {
            System.out.println(&quot;Invalid Month&quot;);
            return;
        }
        
        if (day &lt; 1 || day &gt; monthDays[month - 1]) {
            System.out.println(&quot;Invalid Day&quot;);
            return;
        }

        System.out.print(&quot;Day on 1st January: &quot;);
        String startDayName = in.nextLine();

        int startDayIdx = -1;
        switch (startDayIdx) {
            (int i = 0; i &lt; dayNames.length; i++) {
            if (dayNames[i].equalsIgnoreCase(startDayName)) {
                startDayIdx = i;
                break;
            }
        }
        
        if (startDayIdx == -1) {
            System.out.println(&quot;Invalid Day Name&quot;);
            return;
        }
        
        //Calculate total days
        int tDays = 0;
        for (int i = 0; i &lt; month - 1; i++) {
            tDays += monthDays[i];
        }
        
        tDays += day;
        
        int currDayIdx = tDays % 7 + startDayIdx - 1;
        if (currDayIdx &gt;= 7) {
            currDayIdx -= 7;
        }
        
        System.out.println(&quot;Day on &quot; + dateStr + &quot; : &quot; + dayNames[currDayIdx]);
    }

    public static boolean isLeapYear(int y) {
        boolean ret = false;

        if (y % 400 == 0) {
            ret = true;
        }
        else if (y % 100 == 0) {
            ret = false;
        }
        else if (y % 4 == 0) {
            ret = true;
        }
        else {
            ret = false;
        }

        return ret;
    }
}

3.7 (10 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source