我必须创建程序,以便我可以输入3个字母加上一年如" jan1999" 并显示日历。
该计划必须:
使用有意义的讯息提示用户注明特定月份。
如果输入值是有效日期(即指定所需月份的3个字符(即JAN,FEB,...,则为低和的任意组合) 大写字母,后跟一个空格和一个1900之间的整数 和2099),然后必须显示给定日期的日历
- 醇>
(或2BIS)如果输入值不是有效日期,则必须通知用户日期不可接受,然后再次提示 另一个约会。如果超过三个连续的错误日期 给定,程序必须以适当的错误终止 消息。
package assignment.pkg2;
import java.util.Scanner;
public class Assignment2 {
public static int getMonthNumber(String s) {
if (s.equalsIgnoreCase("jan")) {
return 1;
}
if (s.equalsIgnoreCase("feb")) {
return 2;
}
if (s.equalsIgnoreCase("mar")) {
return 3;
}
if (s.equalsIgnoreCase("apr")) {
return 4;
}
if (s.equalsIgnoreCase("may")) {
return 5;
}
if (s.equalsIgnoreCase("jun")) {
return 6;
}
if (s.equalsIgnoreCase("jul")) {
return 7;
}
if (s.equalsIgnoreCase("aug")) {
return 8;
}
if (s.equalsIgnoreCase("sep")) {
return 9;
}
if (s.equalsIgnoreCase("oct")) {
return 10;
}
if (s.equalsIgnoreCase("nov")) {
return 11;
}
if (s.equalsIgnoreCase("dec")) {
return 12;
} else {
System.out.println("Not valid month!");
}
return 0;
}
public static boolean isLeapYear(int year) {
int month = 0;
int s = getDaysIn(month, year);
return year % 4 == 0 && (year % 100 != 0) || (year % 400 == 0);
}
public static int getDaysIn(int month, int year) {
switch (month) {
case 1:
return 31;
case 2:
if (isLeapYear(month)) {
return 29;
} else {
return 28;
}
case 3:
return 31;
case 4:
return 30;
case 5:
return 31;
case 6:
return 30;
case 7:
return 31;
case 8:
return 31;
case 9:
return 30;
case 10:
return 31;
case 11:
return 30;
case 12:
return 31;
default:
return -1;
}
}
public static String getMonthName(int month) {
switch (month) {
case 1:
return "January";
case 2:
return "February";
case 3:
return "March";
case 4:
return "April";
case 5:
return "May";
case 6:
return "June";
case 7:
return "July";
case 8:
return "August";
case 9:
return "September";
case 10:
return "October";
case 11:
return "November";
case 12:
return "December";
default:
return "Invalid month.";
}
}
public static int getStartDay(int month, int year) {
int days = 0;
for (int i = 1900; i < year; i++) {
days = days + 365;
if (isLeapYear(i)) {
days = days + 1;
}
}
for (int i = 1; i < month; i++) {
days = days + getDaysIn(month, year);
}
int startday = (days + 1) % 7 + 1;
return startday;
}
public static void displayCalendar(int month, int year) {
String monthName = getMonthName(month);
int startDay = getStartDay(month, year);
int monthDays = getDaysIn(month, year);
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
int weekDay = startDay - 1;
for (int i = 1; i <= startDay; i = i + 1) {
System.out.print(" ");
}
for (int x = 1; x <= monthDays; x++) {
weekDay = weekDay + 1;
if (weekDay > 7) {
System.out.println();
weekDay = 1;
}
System.out.format(" %3d", x);
}
if (weekDay > 7) {
System.out.println();
}
}
public static void main(String[] args) {
Scanner c = new Scanner(System.in);
System.out.print("Give the first three letters of a month and enter the year: ");
String month, year;
month = c.next();
year = c.next();
int yearno = Integer.parseInt(year);
int monthno = getMonthNumber(month);
displayCalendar(monthno, yearno);
System.out.println();
System.out.println("Do you want to continue? y/n ");
}
}
答案 0 :(得分:1)
大多数代码都很简单,诀窍在于将输入验证为指定格式的有效日期。您可以使用YearMonth类来完成此操作。然后,您可以从中创建一个Calendar类。
如果您使用的是Java 8,请尝试以下操作:
String myInputString = null;
try
{
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
myInputString = bufferRead.readLine();
}
catch(Exception e)
{
System.out.print(e);
}
YearMonth myYM = null;
Calendar calendar = null;
try
{
DateTimeFormatterBuilder fmb = new DateTimeFormatterBuilder();
fmb.parseCaseInsensitive();
fmb.append(DateTimeFormatter.ofPattern("MMM yyyy"));
DateTimeFormatter formatter = fmb.toFormatter();
myYM = YearMonth.parse(myInputString.trim(), formatter);
calendar = new GregorianCalendar();
calendar.clear();
calendar.set(myYM.getYear(), myYM.getMonthValue(), 1);
}
catch(DateTimeParseException e)
{
// Invalid date format
System.out.println(e);
}
}
答案 1 :(得分:1)
您可以使用java日历。
public void display(int year, int month){
Calendar car = Calendar.getInstance();
car.set(Calendar.YEAR, year);
car.set(Calendar.MONTH, month);
car.set(Calendar.DAY_OF_MONTH, 1);
print(String.format("%" + 23 + "s", new SimpleDateFormat("MMM/yyyy").format(car.getTime()))+"\n");
print(String.format("%" + 5 + "s", "Su"));
print(String.format("%" + 5 + "s", "Mo"));
print(String.format("%" + 5 + "s", "Tu"));
print(String.format("%" + 5 + "s", "We"));
print(String.format("%" + 5 + "s", "Th"));
print(String.format("%" + 5 + "s", "Fr"));
print(String.format("%" + 6 + "s", "Sa\n"));
for(int i=1;i<car.get(Calendar.DAY_OF_WEEK);i++){
print(String.format("%" + 5 + "s", ""));
}
for(int i = 1;i<=LastDayOfMonth(month, year);i++){
if(car.get(Calendar.DAY_OF_WEEK)==7){
print(String.format("%" + 6 + "s", i+"\n"));
}else{
print(String.format("%" + 5 + "s", i));
}
car.set(Calendar.DAY_OF_YEAR, car.get(Calendar.DAY_OF_YEAR)+1);
}
}
public void print(String s){
System.out.print(s);
}
public boolean isLeapYear(int year){
return ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));
}
public int LastDayOfMonth(int month, int year){
if(month == 1){//February
if(this.isLeapYear(year))return 29;
else return 28;
}else if(month==0||month==2||month==4||month==6||month==7||month==9||month==11){
return 31;
}else{
return 30;
}
}</p>
注意:java日历中一年中的第一个月是JANUARY,即0。
E.g 2015年1月的日历display(2015, 0)
如果要验证日期,可以在调用显示功能之前进行验证。