0

I need to format a date (month, year), but I can't do it correctly.

The problem is: Given the following data
Year: 2016
Month: 12
Date: 31
Next N days: 5

Produce the following output:
"December has as many as 31 days"
"Today date: December 31st 2016"
"Tomorrow's date: January 1, 2017"
"5 days to go: January 5, 2017".

How can I format those dates by using a switch statement (as requested by my teacher)?

import java.util.Scanner;

import javax.lang.model.util.ElementScanner6;

public class TugasNo4 {
public static void main(String[] args) {
    int years, month, date, nday;
    String monthname = "";

    Scanner input = new Scanner(System.in);

    System.out.print("Tahun: ");
    years = input.nextInt();

    System.out.print("Bulan: ");
    month = input.nextInt();

    System.out.print("Tanggal: ");
    date = input.nextInt();
    if(date <= 0){
        System.out.println("Anda memasukkan tanggal yang salah!");
        input.close();
    }

    System.out.print("N hari kedepan: ");
    nday = input.nextInt();        

    switch(month){
        case 1:
        monthname = "Januari";
        System.out.println(monthname+" memiliki hari sebanyak 31 hari");            
        break;
        case 3:
        monthname = "Maret";
        System.out.println(monthname+" memiliki hari sebanyak 31 hari");            
        break;
        case 5:
        monthname = "May";
        System.out.println(monthname+" memiliki hari sebanyak 31 hari");            
        break;
        case 7:
        monthname = "July";
        System.out.println(monthname+" memiliki hari sebanyak 31 hari");            
        break;
        case 8:
        monthname = "Agustus";
        System.out.println(monthname+" memiliki hari sebanyak 31 hari");
        break;
        case 10:
        monthname = "Oktober";
        System.out.println(monthname+" memiliki hari sebanyak 31 hari");
        break;
        case 12:
        monthname = "Desember";
        System.out.println(monthname+" memiliki hari sebanyak 31 hari");
        break;
        case 4:
        monthname = "April";
        System.out.println(monthname+" memiliki hari sebanyak 30 hari");
        break;
        case 6:
        monthname = "Juni";
        System.out.println(monthname+" memiliki hari sebanyak 30 hari");
        break;
        case 9:
        monthname = "September";
        System.out.println(monthname+" memiliki hari sebanyak 30 hari");
        break;
        case 11:
        monthname = "November";
        System.out.println(monthname+" memiliki hari sebanyak 30 hari");
        break;
        case 2:
        monthname = "Februari";
        if((years % 4 == 0) && !(years % 100 == 0))
        System.out.println(monthname+" memiliki hari sebanyak 29 hari");     
        else 
        System.out.println(monthname+" memiliki hari sebanyak 28 hari");       
        break;            
        default:
        System.out.println("Anda memasukkkan bulan yang salah");
        break;
    }
    System.out.println("Hari ini tanggal: "+date+" "+monthname+" "+years);

    int datebesok = date + 1;
    System.out.println("Besok tanggal "+datebesok+" "+monthname+" "+years);

    int datenday = date + nday;
    if((month == 2) && (datenday <= 28) && (datenday <= 29)){
        System.out.println(nday+" hari lagi tanggal: "+datenday+" "+monthname+" "+years);
    }
  }
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69

3 Answers3

1

I suggest you do it using the modern date-time API. Learn more about the modern date-time API from Trail: Date Time.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.Locale;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int year, month, dayOfMonth, nDays;
        Scanner input = new Scanner(System.in);

        System.out.print("Enter year: ");
        year = input.nextInt();

        System.out.print("Enter month: ");
        month = input.nextInt();

        System.out.print("Enter day: ");
        dayOfMonth = input.nextInt();

        System.out.print("Enter next N days: ");
        nDays = input.nextInt();

        LocalDate date = LocalDate.of(year, month, dayOfMonth);
        String monthName = date.getMonth().getDisplayName(TextStyle.FULL, Locale.ENGLISH);

        System.out.println(monthName + " has as many days as " + date.getMonth().length(date.isLeapYear()) + " days");

        // Entered date
        System.out.println("Today's date: " + monthName + " " + ordinal(dayOfMonth) + " " + year);

        // Format for other dates
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM dd, uuuu", Locale.ENGLISH);

        System.out.println("Tomorrow's date: " + date.plusDays(1).format(formatter));

        System.out.println("After " + nDays + " days: " + date.plusDays(5).format(formatter));
    }

    static String ordinal(int num) {
        String[] suffix = { "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" };
        int m = num % 100;
        return String.valueOf(num) + suffix[(m > 3 && m < 21) ? 0 : (m % 10)];
    }
}

A sample run:

Enter year: 2016
Enter month: 12
Enter day: 31
Enter next N days: 5
December has as many days as 31 days
Today's date: December 31st 2016
Tomorrow's date: January 01, 2017
After 5 days: January 05, 2017

Note: The function, ordinal has been copied from this answer.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

I believe you need to change your month data type and input to string input by using String month = input.nextLine(); to get a String

Luke
  • 154
  • 9
0

First to find tomorrows date: You add 1 to the day of month, date. Then check if it has become greater than the number of days in the month (you already know how to find the number of days in the month). If it has, we need to go to the first day of the following month. Increment month by 1 and set day of month to 1. Now we need to check whether month has become greater than the number of months in a year. If so, increment year and set month to 1.

To find the date after 5 days, or after any number of days that the user enters is similar, only a bit more complicated. After adding the appropriate number of days, if the date is greater than the number of days in the month, subtract that number and increase the month. Taking your example: if date is 31 and month is 12 (December), you add 5 and get 36, you see that this is greater than 31, so you subtract 31 and get 5. After adjusting month and year as before, we have got January 5 the following year. We’re not done yet. The user may enter so many days that we get past the end of January too. What if the user enters 594 days? So we need to do that check in a loop, repeatedly subtracting the number of days in the month and adjusting month and year, until we get into a situation where date is less than or equal to the number of days in the month where we have landed.

PS As others have said, for production code one would and should use java.time, the modern Java date and time API, and its LocalDate class. But as an exercise you should of course do as your teacher says, and it’s a fine exercise, you are surely learning.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161