【问题标题】:android comparing dates I couldn't compare two dates...I referred many tutorialsandroid比较日期我无法比较两个日期...我参考了很多教程
【发布时间】:2017-07-05 09:17:02
【问题描述】:

我正在使用日期选择器对话框并尝试将当前日期与对话框中指定的日期进行比较。 如果日期早于当前日期,我希望显示无效日期。

当前日期和输入的日期格式相同。但我收到错误。在控制台中打印时,当前日期和输入日期的格式相同

if(date1.before(currDate))
{
     Toast.makeText(getApplicationContext(), "Enter the valid Date", Toast.LENGTH_LONG).show();
}

结果:

当前日期 2017 年 5 月 7 日

选择日期 5-7-2017

【问题讨论】:

  • 分享完整代码
  • 在执行操作后将字符串日期解析为java日期
  • 请添加您的错误日志以及应用逻辑的代码。
  • 如何解析日期
  • 当前日期 String currDate = cal.get(Calendar.DATE) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.YEAR );然后使用日期选择器我得到了用户日期并试图比较日期 if(date1.before(currDate)) { Toast.makeText(getApplicationContext(), "Enter the valid Date", Toast.LENGTH_LONG).show(); }

标签: android date android-datepicker


【解决方案1】:

试试这个

        String strCurrDate = cal.get(Calendar.DATE) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.YEAR);
        String strAnotherDate = "05/03/2017";
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

        try {
            Date currDate = formatter.parse(strCurrDate);
            Date anotherDate = formatter.parse(strAnotherDate);
            if (currDate.before(anotherDate)) {
                Toast.makeText(getApplicationContext(), "Enter the valid Date", Toast.LENGTH_LONG).show();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

【讨论】:

  • 如何将字符串转换为日期
  • 将字符串转换为日期需要使用SimpleDateFormat解析方法
  • @shwettha 尝试字符串到日期格式的 url javatpoint.com/java-string-to-date
【解决方案2】:

您可以尝试使用此代码进行日期比较

@SuppressLint("SimpleDateFormat")
public static boolean compareSameDate(String firstDate, String secondDate) {
    boolean flag = false;
    try {
        SimpleDateFormat formatter = new SimpleDateFormat(
            "dd/MM/yyyy",
            Locale.ENGLISH);

        Date date1 = formatter.parse(firstDate);
        Date date2 = formatter.parse(secondDate);


        if (date2.compareTo(date1) == 0) {
            flag = true;
        } else if (date2.compareTo(date1) < 0) {
            flag = true;
        } else {
            flag = false;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return flag;
}

【讨论】:

    【解决方案3】:

    你可以这样试试,

    try {
        String currentDateString = "5/7/2017";
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    
        Date currDate = formatter.parse(currentDateString);
    
        String pickDateString = "5-7-2017";
        formatter = new SimpleDateFormat("dd-MM-yyyy");
    
        Date pickDate = formatter.parse(pickDateString);
    
        if (pickDate.before(currDate)) {
            Toast.makeText(getApplicationContext(), "Enter the valid Date", Toast.LENGTH_LONG).show();
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }
    

    【讨论】:

      【解决方案4】:

      您可以通过

      直接在日期选择器中停用过去的日期
      datePicker.setMinDate(System.currentTimeMillis() - 1000);
      

      您只能通过以毫秒为单位来比较时间

        Date setDateTime = input.parse(todayTime);
        calendar1.setTime(setDateTime);
        selectedTime=calendar1.getTimeInMillis();
      
        msTime = System.currentTimeMillis();//current time
      //and compare msTime and setsetDateTime 
        if (selectedTime>msTime){
      //your logic
      }
      

      或者您可以通过仅以数字格式格式化日期格式来直接比较日期,例如 5-7-2017 是 572017 并将其与相同格式的选定日期进行比较

      【讨论】:

      • 谢谢你..am not compring time..你能说一下如何将字符串转换为日期
      • 或者你可以通过datePicker.setMinDate(System.currentTimeMillis() - 1000) 使日期选择器停用过去的日期;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-01
      • 2013-11-29
      相关资源
      最近更新 更多