-1

我正在尝试验证日历日期,但在验证时,无论输入如何,代码都会返回 false。

String strDateOfBirth = userInputArrayList.get(7) + "/" + userInputArrayList.get(8) + "/" + userInputArrayList.get(9);
Log.d(TAG, "1Date of Birth: " + strDateOfBirth);
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
Date dtDateOfBirth = parseDate(strDateOfBirth, sdf);

//Convert to epoch millis
long dobmillis = dtDateOfBirth.getTime();
Log.d(TAG, "date of birth in milli: " + dobmillis);

//Current time in epoch millis
long currmillis = Calendar.getInstance().getTimeInMillis();
Log.d(TAG, "current date in milli: " + currmillis);

Log.d(TAG, "less than: " + (19L * 365L * 24L * 60L * 60L * 1000L));
Log.d(TAG, "less than: " + (11L * 365L * 24L * 60L * 60L * 1000L));

if ((currmillis - dobmillis) > (19L * 365L * 24L * 60L * 60L * 1000L)) {
    //More than 19 years
    txtCalenderErrorCreateAccountPage.setText("must be 19 years of age");
    nextPage = false;
} else if ((currmillis - dobmillis) < (11L * 365L * 24L * 60L * 60L * 1000L)) {
    //less than 11 years
    txtCalenderErrorCreateAccountPage.setText("must be 11 years of age");
    nextPage = false;
} else {             
    txtCalenderErrorCreateAccountPage.setText("correct");
}


public Date parseDate(String date, SimpleDateFormat sdf) {
    try {
        Date d = sdf.parse(date);
        return d;
    } catch (ParseException e) {
        showToast("There has been an error");
        return null;
    }
}

当出生日期意味着用户将在 11 到 19 岁之间时,我希望日期返回 true。

任何帮助将不胜感激!

4

2 回答 2

0
String strDateOfBirth = userInputArrayList.get(7) + "/" + userInputArrayList.get(8) + "/" + userInputArrayList.get(9);
Log.d(TAG, "1Date of Birth: " + strDateOfBirth);
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
Date dtDateOfBirth = parseDate(strDateOfBirth, sdf);

//Convert to epoch millis
long dobmillis = dtDateOfBirth.getTime();
Log.d(TAG, "date of birth in milli: " + dobmillis);

//Current time in epoch millis
long currmillis = Calendar.getInstance().getTimeInMillis();
Log.d(TAG, "current date in milli: " + currmillis);

Log.d(TAG, "less than: " + (19L * 365L * 24L * 60L * 60L * 1000L));
Log.d(TAG, "less than: " + (11L * 365L * 24L * 60L * 60L * 1000L));

if ((currmillis - dobmillis) > (19L * 365L * 24L * 60L * 60L * 1000L)) {
  //More than 19 years
  txtCalenderErrorCreateAccountPage.setText("more than 19 years of age");
} else if ((currmillis - dobmillis) < (11L * 365L * 24L * 60L * 60L * 1000L)) {
  //less than 11 years
  txtCalenderErrorCreateAccountPage.setText("less than 11 years of age");
  nextPage = false;
} else {             
  txtCalenderErrorCreateAccountPage.setText("11 - 19");
  nextPage = false;
}


  public Date parseDate(String date, SimpleDateFormat sdf) {
    try {
      Date d = sdf.parse(date);
      return d;
    } catch (ParseException e) {
      showToast("There has been an error");
      return null;
  }
}

通过将所有内容转换为纪元,基本上降低了代码复杂性并消除了任何和所有时区依赖性

于 2019-08-21T09:13:52.270 回答
0

问题

您使用了mm( Minute in hour ) 而不是MM( Month in year )。

使用正确的符号将解决问题。但是,java.util日期时间 API 及其格式化 APISimpleDateFormat已过时且容易出错。建议完全停止使用它们并切换到现代 Date-Time API *

使用java.time现代 API 的解决方案:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Locale;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("d/M/u", Locale.ENGLISH);
        
        // Test
        Stream.of(
                28 + "/" + 5 + "/" + 2000,
                28 + "/" + 5 + "/" + 2008,
                28 + "/" + 5 + "/" + 2012
        ).forEach(strDateOfBirth -> {
            LocalDate dtDateOfBirth = LocalDate.parse(strDateOfBirth, dtf);
            LocalDate today = LocalDate.now();
            long years = ChronoUnit.YEARS.between(dtDateOfBirth, today);
            System.out.println( 
                                years > 19 ? "must be 19 years of age" :
                                years < 11 ? "must be 11 years of age" :
                                             "correct"
            );
        });     
    }
}

输出:

must be 19 years of age
correct
must be 11 years of age

Trail: Date Time了解更多关于java.time现代日期时间 API *的信息。


* 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,则可以使用ThreeTen-Backport,它将大部分java.time功能向后移植到 Java 6 和 7。如果您正在为 Android 项目和 Android API 工作level 仍然不符合 Java-8,请检查Java 8+ APIs available through desugaringHow to use ThreeTenABP in Android Project

于 2021-05-28T17:36:38.450 回答