Below is my program and it is returning the wrong day name when I enter the related month, date and year.
What I am missing here?
My Program
import java.util.Calendar;
import java.util.Locale;
import java.util.Scanner;
public class TimeTest {
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));
}
private static String getDay(String day, String month, String year) {
Calendar calendar = Calendar.getInstance();
calendar.set(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day));
return calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
}
}
Output:
09
24
2018
Wednesday
It should be returning "Monday", as that is the current day.
You are passing 09 as Month assuming it's September but actually in term of Calendar class it's August because months start from 00(Jan), 01(Feb)...
and so on in Calendar Class.
Hence in order to the get correct output you need to pass
08 // September Month not August
24
2018
Here is your running code
Neeraj Jain has already in another answer explained why you got the unexpected output. I should like to contribute the modern way of obtaining your result.
First, your users may prefer to enter the date on one line in a format of their own locale, for example 9/24/18
in the USA or 24/09/2018
in French. It’s not more complicated (when you know how):
DateTimeFormatter dateFormatter
= DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
.withLocale(Locale.getDefault(Locale.Category.FORMAT));
String dateString = in.next();
LocalDate date = LocalDate.parse(dateString, dateFormatter);
DayOfWeek dow = date.getDayOfWeek();
String dowString = dow.getDisplayName(
TextStyle.FULL_STANDALONE,
Locale.getDefault(Locale.Category.FORMAT)
);
System.out.println(dowString);
Here’s a sample run of the program in the US locale:
9/25/18
Tuesday
If you or your users do prefer to enter three separate numbers as in your program in the question:
Scanner in = new Scanner(System.in);
int month = in.nextInt();
int day = in.nextInt();
int year = in.nextInt();
LocalDate date = LocalDate.of(year, month, day);
Now we’ve got the LocalDate
, the remainder is the same as before. A sample run:
9
27
2018
Thursday
Note that there’s no adding or subtracting of 1 anywhere because LocalDate
numbers months the same way humans do. I am letting the scanner convert to integers, I find it a bit simpler than calling Integer.parseInt
.
The Calendar
class you were using is long outdated and poorly designed. I recommend you forget about the trouble with it and instead use LocalDate
from java.time, the modern Java date and time API.
Link: Oracle tutorial: Date Time explaining how to use java.time
.
By using java.time.LocalDate
you can pass your 3 paramter (year, month, day) and get the dayOfWeek:
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));
}
private static String getDay(String day, String month, String year) {
return LocalDate.of(Integer.valueOf(year), Integer.valueOf(month), Integer.valueOf(day)).getDayOfWeek()
.toString();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With