当我输入"今天"在下面的代码中,if(date_holder.contains("/"))
执行。我该如何解决?
很抱歉我的代码很邋..任何帮助表示赞赏。
import java.util.*;
import java.io.*;
import java.text.*;
/**
* Write a description of class Driver here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Driver
{
//extra credit
//do you want to enter a date or use todays date can use api to get it
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int day = 0;
int month = 0;
int year = 0;
char choice;
String date_holder;
StringTokenizer stok;
int count = 0; // use this variable to create all of the sub menus
//for example 0 is the first sub menu 1 s the next sub menu
while(count == 0)
{
System.out.println("Enter a date or type 'today' for todays date \nor enter 'quit' to quit: ");
date_holder = keyboard.next();
date_holder.trim();//trim any white space
System.out.println("!@# " + date_holder);
if(date_holder.contains("/"))
{
stok = new StringTokenizer(date_holder,"/");
month = Integer.parseInt(stok.nextToken());
day = Integer.parseInt(stok.nextToken());
year = Integer.parseInt(stok.nextToken());
Date date = new Date(day, month, year);
count = 1;
while(count == 1)
{
do
{
//zz
System.out.println("Current Date: " + month + "/" + day + "/" + year +
"\nEnter 'a' if you want to add days to the date \n" +
"or enter a 's' if you want to subtract the date \n" +
"or enter a 'd' get the days between the current date and another date \n " +
"or enter a 'f' if you want to format the date.\n");
choice = keyboard.next().charAt(0);
}while(choice != 'a' && choice != 's' && choice != 'd' && choice != 'f');
String number; //to store the user's number input
String answer;
if(choice == 'a')//too add the date
{
System.out.println("Enter a number to add:");
number = keyboard.next();
date.add(Integer.parseInt(number));
answer = date.toString();
System.out.println("answer: " + answer);
count = 0;
}
else if(choice == 's')//too subtract the date
{
System.out.println("Enter a number of days to subtract:");
number = keyboard.next();
date.subtract(Integer.parseInt(number));
answer = date.toString();
System.out.println("answer: " + answer);
count = 0;
}
else if(choice == 'd')//too get the days between
{
int inMonth, inDay, inYear = 0;
System.out.println("Enter a date to use with " + month + "/" + day +
"/" + year + ":");
number = keyboard.next();
if(number.contains("/"))
{
stok = new StringTokenizer(number,"/");
inMonth = Integer.parseInt(stok.nextToken());
inDay = Integer.parseInt(stok.nextToken());
inYear = Integer.parseInt(stok.nextToken());
Date inDate = new Date(inDay, inMonth, inYear);
Date outDate = new Date(day, month, year);
if(year < inYear)
{
System.out.println("The days between " + date.toString() +
"and" + inDate.toString() + " is " + inDate.daysBetween(outDate) + " days.");
}
else
{
System.out.println("The days between " + date.toString() +
"and" + inDate.toString() + " is " + outDate.daysBetween(inDate) + "days.");
}
count = 0;
}
}
else if(choice == 'f')//too change the format and (extra credit)
{
do
{
//zz
System.out.println("Current Date: " + month + "/" + day + "/" + year +
"\nEnter 's' if you want the format day//month//year \n" +
"or enter a 'l' if you want the format day of month, year \n" +
"or enter a 'j' to get the Julian date \n" +
"or enter a 'h' if you want to get the horoscope Zodiac of the date.\n" +
"or enter a 'c' if you want to get the Chinese Zodiac of the date.\n"+
"or enter a 'e' if you want to get the Easter date of the year.\n");
choice = keyboard.next().charAt(0);
}while(choice != 's' && choice != 'l' && choice != 'j' && choice != 'h' && choice != 'c' && choice != 'e');
//I don't need any if statments to verify the choices because
//it is all handled in the getDate() method
System.out.println(date.getDate(choice));
count = 0;//end the submenu loop
}
}
}
else if(date_holder.equals("today"))
{
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal));
}
else if(date_holder.equals("quit"))
{
count = -1;
}
}
System.out.println("goodbye!");
}
}
&#13;
答案 0 :(得分:0)
今天我提供的代码工作正常,它会以您提到的格式打印今天的日期,但将代码更改为以下代码。
{{1}}
您无法格式化日历对象,您必须使用getTime方法来获取Date对象。 您的代码看起来很笨拙,请详细了解日期api here