【问题标题】:Is there other way of getting an input like date format?还有其他方法可以获取日期格式之类的输入吗?
【发布时间】:2021-01-21 16:32:52
【问题描述】:

我希望并寻找其他一些想法或方法来做到这一点。如您所见,我要求用户输入 mmddyyyy(05022001 = 2001 年 5 月 2 日)并使用子字符串以便我在索引中获取特定输入然后解析它。现在,我想要完成的是找到另一种方法来做到这一点,例如使用正式的日期格式化程序,在我的情况下我没有使用。我认为日期格式化程序比这要好得多,但我不明白它的想法。

这是我到目前为止所得到的。提前致谢。

import java.util.*;

public class ZodiaChineseBirthSign {
    public static void main(String [] args) {
       Scanner reader = new Scanner(System.in);
       String mm,dd,yyyy;
       int mm1,dd1,yyyy1;
       
       /**
        * To keep asking for the input from the user I have to use looping
        * which is the while loop.
        * 
        */
       while(true) {
       System.out.print("Enter your birthdate: ");
       String birth = reader.next();
       
       //Substring is to get the specific input of the user.
       mm = birth.substring(0, 2);
       dd = birth.substring(2, 4);
       yyyy = birth.substring(4);
        
       //ParseInt which is used to convert String into Integer in order for me to be able to determine the Animal Sign and Zodiac.
       mm1 = Integer.parseInt(mm);
       dd1 = Integer.parseInt(dd);
       yyyy1 = Integer.parseInt(yyyy);
       
       try {
           if (mm1 < 1 || mm1 > 12 || dd1 < 1 || dd1 > 31) {
               throw new ArrayIndexOutOfBoundsException();
           }
       
       
       switch (mm1) {
            case 1: System.out.println("You were born on January " + dd1 + ", " + yyyy1 ); break;
            case 2: System.out.println("You were born on February " + dd1 + ", " + yyyy1 ); break;
            case 3: System.out.println("You were born on March " + dd1 + ", " + yyyy1 ); break;
            case 4: System.out.println("You were born on April " + dd1 + ", " + yyyy1 ); break;
            case 5: System.out.println("You were born on May " + dd1 + ", " + yyyy1 ); break;
            case 6: System.out.println("You were born on June " + dd1 + ", " + yyyy1 ); break;
            case 7: System.out.println("You were born on July " + dd1 + ", " + yyyy1 ); break;
            case 8: System.out.println("You were born on August " + dd1 + ", " + yyyy1 ); break;
            case 9: System.out.println("You were born on September " + dd1 + ", " + yyyy1 ); break;
            case 10: System.out.println("You were born on October " + dd1 + ", " + yyyy1 ); break;
            case 11: System.out.println("You were born on November " + dd1 + ", " + yyyy1 ); break;
            case 12: System.out.println("You were born on December " + dd1 + ", " + yyyy1 ); break;
       }
       
       switch (yyyy1 % 12) {
            case 0: System.out.println("in the Year of the Monkey"); break;
            case 1: System.out.println("in the Year of the Rooster"); break;
            case 2: System.out.println("in the Year of the Dog"); break;
            case 3: System.out.println("in the Year of the Pig"); break;
            case 4: System.out.println("in the Year of the Rat"); break;
            case 5: System.out.println("in the Year of the Ox"); break;
            case 6: System.out.println("in the Year of the Tiger"); break;
            case 7: System.out.println("in the Year of the Rabbit"); break;
            case 8: System.out.println("in the Year of the Dragon"); break;
            case 9: System.out.println("in the Year of the Snake"); break;
            case 10: System.out.println("in the Year of the Horse"); break;
            case 11: System.out.println("in the Year of the Sheep"); break;
       }
       
       if ((dd1>21 && mm1 == 12) || (dd1<=19 && mm1== 1)){
           System.out.println("under the sign of Capricorn");
       }
       if ((dd1>20 && mm1 ==1) || (dd1<=18 && mm1 == 2)){
           System.out.println("under the sign of Aquarius");
       }
       if ((dd1>19 && mm1 == 2) || (dd1 <=20 && mm1 == 3)){
           System.out.println("under the sign of Pisces");
       }
       if ((dd1>21 && mm1 == 3) || (dd1 <=20 && mm1 == 4)){
           System.out.println("under the sign of Aries");
       }
       if ((dd1>21 && mm1 == 4) || (dd1 <=20 && mm1 == 5)){
           System.out.println("under the sign of Taurus");
       }
       if ((dd1>21 && mm1 == 5) || (dd1<=20 && mm1 == 6)){
           System.out.println("under the sign of Gemini");
       }
       if ((dd1>21 && mm1 == 6) || (dd1<=20 && mm1 == 7)){
           System.out.println("under the sign of Cancer");
       }
       if ((dd1>21 && mm1  == 7) || (dd1<=20 && mm1== 8)){
           System.out.println("under the sign of Leo");
       }
       if ((dd1>21 && mm1 ==8) || (dd1<=22 && mm1 == 9)){
           System.out.println("under the sign of Virgo");
       }
       if ((dd1>23 && mm1 == 9) || (dd1<=20 && mm1 ==10)){
           System.out.println("under the sign of Libra");
       }
       if ((dd1>21 && mm1 == 10) || (dd1<=22 && mm1 == 11)){
           System.out.println("under the sign of Scorpio");
       }
       if ((dd1>23 && mm1 == 11) || (dd1<=20 && mm1 == 12)){
           System.out.println("under the sign of Sagittarius");
       }
       }
       catch (ArrayIndexOutOfBoundsException e) { //Catch-Block which will print(enter correct birthdate) whenever it throws exception error.
           System.out.println("Please enter the correct Birthdate");
       } 
       System.out.println("");
       }
       
       /**
        * Thanks to some of my classmate for giving me some snippets of codes and ideas ^_^
        */
    }  
}

【问题讨论】:

    标签: java switch-statement try-catch


    【解决方案1】:

    您应该告知用户您期望的格式。

    获取输入字符串并尝试解析。在输入错误的情况下捕获异常。您无需检查月份和日期的范围。 LocalDate.parse 为您进行这些数据输入验证检查。

    DateTimeFormatter f = DateTimeFormatter.of( "MMdduuuu" ) ;
    try
    {
        LocalDate ld = LocalDate.parse( input ) ;
    } 
    catch ( DateTimeParseException e ) 
    {
        … handle faulty input
    }
    

    无需开启月份。使用Month#getDisplayName 生成月份的本地化名称。

    String monthName = ld.getMonth().getLocalizedDisplayName( TextStyle. FULL_STANDALONE , Locale.CANADA_FRENCH ) ;
    

    所有这些都已经在 Stack Overflow 上多次介绍过。搜索以了解更多信息。

    【讨论】:

    • 如何使用 DateFormatter?是不是应该这样System.out.print("Enter Birthdate(mmddyyyy): ");f = reader.next();
    • 那么在我的 try-block 上会是 LocalDate ld = LocalDate.parse(f)?但是我怎么能接受具体的输入,比如月份和年份,这将是能够确定标志的因素。
    • @1337 正如我所说,您正在从控制台获取输入字符串。你的工作是解析那个字符串输入。
    • 我明白了。我试试看
    • @1337 你必须学会​​阅读文档。我给了你钥匙:使用LocalDate 类。我什至向你展示了getMonth 方法,所以你可能认为还有getYear 方法。您的第一反应应该是打开该类的 Javadoc 以研究其方法,而不是寻求更多帮助。我们不是来帮你做功课的。
    猜你喜欢
    • 2015-02-03
    • 2016-03-25
    • 2019-09-14
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 2020-12-13
    相关资源
    最近更新 更多