我的出生日期为06-03-2014(dd-mm-yyyy)格式。现在我想检查特定员工的年龄是否等于或超过18岁,与今天的日期相比。 我怎么能在java.Please帮助
中这样做答案 0 :(得分:1)
你可以这样试试:
public static void main(String[] args) throws Exception {
String dateString = "06-03-2014";
Date date = new SimpleDateFormat("dd-MM-yyyy").parse(dateString);
Calendar calendar = GregorianCalendar.getInstance();
calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 18);
System.out.printf("Date %s is older than 18? %s", dateString, calendar.getTime().after(date));
}
答案 1 :(得分:0)
你可以试试这个
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = new Date();
System.out.println("Current Date : "+dateFormat.format(date));
String dateInString = "24-03-2014";
Date date1 = dateFormat.parse(dateInString);
System.out.println(dateFormat.format(date1));
long diff = Math.abs(date.getTime() - date1.getTime());
long diffDays = diff / (24 * 60 * 60 * 1000);
System.out.println("Difference"+diffDays);
if(diffDays > 18){
System.out.println("The employee above 18..");
}else{
System.out.println("The employee below 18..");
}