gpt4 book ai didi

java - Java 中的日期范围

转载 作者:行者123 更新时间:2023-12-01 07:30:21 25 4
gpt4 key购买 nike

我有一个日期。现在我想计算,如果条件成立,则年龄为 2-12 岁,否则年龄应在 0-2 岁之间。那么我们如何才能计算出给定日期的年龄是否介于 2-12 岁或 0-2 岁之间。

我已经做了一些事情,但我很困惑如何继续。

public class Test {
public static void main(String args[]) throws ParseException {
String string = "29/07/13";
Date oneWayTripDate = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(string);
String myFormat = "dd/MM/yy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
}
}

那么条件将是这样的

for child age should be between 2-12 (So from Current Date will calulate to  12 yrs .if the date given lies between 2-12yrs then it is true otherwise false) 
for infant age should be between 0-12 (So from Current Date will calulate to 2 yrs .if the date given lies between 0-2yrs then it is true otherwise false)

最佳答案

如果您想查找年龄,您需要两个日期值。你可以尝试这样的事情

    DateFormat df=new SimpleDateFormat("dd/MM/yy");
Date birthDay=df.parse("29/07/13");
Date currentDay=new Date();
Calendar cal1=Calendar.getInstance();
Calendar cal2=Calendar.getInstance();
cal1.setTime(birthDay);
cal2.setTime(currentDay);
int years=cal2.get(Calendar.YEAR)-cal1.get(Calendar.YEAR);
if(3>years){
System.out.println("Infant");
}else if(years<13){
System.out.println("Child");
} else{
System.out.println("adult");
}

关于java - Java 中的日期范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18379350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com