【问题标题】:Compare the month / year of a string比较字符串的月/年
【发布时间】:2017-10-05 10:01:40
【问题描述】:

当一个 INT(在本例中是一个月的数字,如 02)等于一个 STRING(这是一个完全写出的日期时,我正在使用 if 语句执行一组命令--> 02/14/1998) 完整写出的日期在文件中

所以我要求用户输入日期和月份 (INTS),并将其与文件中完全写出的日期进行比较,当月份是完全写出日期的月份时,与年做一组计算。

    int userTypedMonth, userTypedYear
    string fileDate 
    if(userTypedMonth == fileDate && userTypedYear == fileDate){
          5+5=10
    } 

我知道这是错误的代码语法,但我想展示我如何解释它。

【问题讨论】:

标签: java datetime


【解决方案1】:

您可以fileDate.split("/") 将 fileDate 拆分为 String[],其中索引 0 是月份(“02”),索引 1 是日期(“14”),索引 2 是(“1998”) .然后您可以使用userTypedMonth.equals(fileDateArray[0])userTypedYear.equals(fileDateArray[2]) 进行比较,假设 userTypedMonth/Year 是字符串而不是整数。如果是整数,请使用String.valueOf(userTypedMonth/Year) 进行比较。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-23
    • 2011-07-06
    相关资源
    最近更新 更多