我试图用java制作一个简单的程序 用户将输入他的生日 并且app给了他约会 这是我的代码
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;
}
}
}
但日期名称不正确,例如我的生日是18/1/1987它是星期天我的代码的结果是星期四
N.B int month2 = month1-1; 我写这个是因为java中的月份从0开始而不是1
所以任何帮助??
答案 0 :(得分:2)
因为Calender.set
按顺序要求参数:
public final void set(int year, int month, int date);
你的订单错了。 将其更改为:
cal.set(year, month2, day);
您将获得正确的结果。
答案 1 :(得分:1)
我认为代码有一些错误; cal.set(year, day, month2);
不得按此顺序排列。
public final void set(int year,int month,int date)
是订单。
您设置错误,请执行cal.set(year,month2,day);
。
答案 2 :(得分:1)
工作代码:
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);
}
}
}
答案 3 :(得分:1)
其他答案都是正确的。但既然你反复询问是否有更简单的方法,是的,就有了。
使用合适的日期时间库而不是滚动自己的日期时间逻辑。日期时间工作令人惊讶地棘手且令人困惑。
但要避免与Java捆绑在一起的臭名昭着的java.util.Date/.Calendar类。在Java 8中,这些类已被新的java.time
package取代。该软件包的灵感来自古老的Joda-Time库。
当您只想要一个没有任何时间的日期时,Joda-Time会提供LocalDate课程。
以下是一些使用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 );
跑步时......
Your Birthdate : Saturday - 3/2/2001
更好的是,日期时间库可以完成本地化措辞和订单的工作。
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 );
跑步时......
samedi 3 février 2001