Reputation: 37
i'm trying to make a simple program with java the user will input his birthday and the app give him the date this is my code
import java.util.*;
public class DOW {
public static void main(String [] args){
Scanner scn = new Scanner(System.in);
System.out.println("Enter Your Birthdate");
System.out.print("Day : ");
int day = scn.nextInt();
System.out.print("Month : ");
int month = scn.nextInt();
System.out.print("Year : ");
int year = scn.nextInt();
scn.close();
Calendar cal = Calendar.getInstance();
cal.set(year, month-1, day);
int dow = cal.get(Calendar.DAY_OF_WEEK);
switch (dow){
case 1:
System.out.println("Your Birthdate : " + "Sunday - " + day + "/" + month + "/" + year);
break;
case 2:
System.out.println("Your Birthdate : " + "Monday - " + day + "/" + month + "/" + year);
break;
case 3:
System.out.println("Your Birthdate : " + "Tuesday - " + day + "/" + month + "/" + year);
break;
case 4:
System.out.println("Your Birthdate : " + "Wednesday - " + day + "/" + month + "/" + year);
break;
case 5:
System.out.println("Your Birthdate : " + "Thursday - " + day + "/" + month + "/" + year);
break;
case 6:
System.out.println("Your Birthdate : " + "Friday - " + day + "/" + month + "/" + year);
break;
case 7:
System.out.println("Your Birthdate : " + "Saturday - " + day + "/" + month + "/" + year);
break;
}
}
}
but the date name in not right for example my birthdate is 18/1/1987 it's sunday the result with my code is thursday
N.B int month2 = month1-1; i write this because the months in java start with 0 not 1
so any help??
Upvotes: 1
Views: 455
Reputation: 338211
The other answers are correct. But since you repeatedly asked if there is a simpler way, yes, there is.
Use a decent date-time library rather than roll your own date-time logic. Date-time work is surprisingly tricky and confusing.
But avoid the notoriously troublesome java.util.Date/.Calendar classes bundled with Java. In Java 8, those classes have been supplanted by the new java.time
package. That package was inspired by the venerable Joda-Time library.
Joda-Time offers a LocalDate class for when you want only a date without any time-of-day.
Here is some example code using Joda-Time 2.3.
int year = 2001;
int month = 2;
int day = 3;
LocalDate birthDate = new LocalDate( year, month, day );
DateTimeFormatter formatter = DateTimeFormat.forPattern( "'Your Birthdate : 'EEEE - d/M/yyyy" );
String output = formatter.print( birthDate );
System.out.println( output );
When run…
Your Birthdate : Saturday - 3/2/2001
Even better, a date-time library can do the work of localizing both the wording and the order.
int year = 2001;
int month = 2;
int day = 3;
LocalDate birthDate = new LocalDate( year, month, day );
java.util.Locale localeQuébécois = java.util.Locale.CANADA_FRENCH;
DateTimeFormatter formatter = DateTimeFormat.forStyle( "F-" ).withLocale( localeQuébécois );
String output = formatter.print( birthDate );
System.out.println( output );
When run…
samedi 3 février 2001
Upvotes: 1
Reputation: 5805
Its because Calender.set
requires the arguments in order:
public final void set(int year, int month, int date);
and you have the wrong order. Change it to:
cal.set(year, month2, day);
and you'll get the right result.
Upvotes: 2
Reputation: 2775
The working code:
import java.util.*;
public class DOW
{
public static void main(String[] args)
{
Scanner scn = new Scanner(System.in);
System.out.println("Enter Your Birthdate");
System.out.print("Day : ");
int day = scn.nextInt();
System.out.print("Month : ");
int month = scn.nextInt();
System.out.print("Year : ");
int year = scn.nextInt();
scn.close();
Calendar cal = Calendar.getInstance();
cal.set(year, month-1, day);
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY)
{
System.out.println("Your Birthdate : " + "Monday - " + day + "/" + month + "/" + year);
}
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY)
{
System.out.println("Your Birthdate : " + "Tuesday - " + day + "/" + month + "/" + year);
}
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY)
{
System.out.println("Your Birthdate : " + "Wednesday - " + day + "/" + month + "/" + year);
}
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY)
{
System.out.println("Your Birthdate : " + "Thursday - " + day + "/" + month + "/" + year);
}
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY)
{
System.out.println("Your Birthdate : " + "Friday - " + day + "/" + month + "/" + year);
}
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
{
System.out.println("Your Birthdate : " + "Thursday - " + day + "/" + month + "/" + year);
}
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
{
System.out.println("Your Birthdate : " + "Sunday - " + day + "/" + month + "/" + year);
}
}
}
Upvotes: 1
Reputation: 19158
I think the code has some error; cal.set(year, day, month2);
must not be in this order.
public final void set(int year,int month,int date)
is the order.
You have set it wrong,do as cal.set(year,month2,day);
.
Upvotes: 1