-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDateAndAge.java
More file actions
121 lines (93 loc) · 3.8 KB
/
DateAndAge.java
File metadata and controls
121 lines (93 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class DateAndAge {
public static Integer[] calculateAge(LocalDate dateOfBirth) {
LocalDate now = LocalDate.now();
LocalDate reference = now.minusDays(1);
Period diference = Period.between(now, dateOfBirth);
int years = Math.abs(diference.getYears());
int months = Math.abs(diference.getMonths());
int days = Math.abs(diference.getDays());
return new Integer[] {years, months, days};
}
public static Date calculateDate (int years, int months, int days) {
LocalDate now = LocalDate.now(ZoneId.systemDefault());
int localDay;
if ((months == 1 || months == 3) && days-1 > now.getDayOfMonth()) {
localDay = days - 1;
}
else if ((months == 5) && days-2 > now.getDayOfMonth()) {
localDay = days - 2;
}
else {
localDay = days;
}
Period datePeriod = Period.of(years, months, localDay);
LocalDate localDateOfBirth = now.minus(datePeriod);
Date dateOfBirth = Date.from(localDateOfBirth.atStartOfDay(ZoneId.systemDefault()).toInstant());
localDateOfBirth.getDayOfMonth();
return dateOfBirth;
}
public static LocalDate calculateDate2 (int years, int months, int days) {
Period datePeriod = Period.of(years, months, days);
LocalDate now = LocalDate.now(ZoneId.systemDefault());
LocalDate dateOfBirth = now.minus(datePeriod);
return dateOfBirth;
}
public static void main (String [] args) throws ParseException {
//CalcularEdad calcular = new CalcularEdad();
//Date fechaReferencia = new Date();
int years = 0;
int months = 0;
int days = 0;
// - - - - - - - - - - TEST CONSECUTIVE AGES STARTING WITH A REFERENCE DATE UNTIL AN END DATE - - - - - - - - - - - - - -
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = formatter.parse("1921-01-01");
Date endDate = formatter.parse("2020-31-12");
int errors = 0;
int pass = 0;
int total = 0;
Calendar start = new GregorianCalendar();
start.setTime(startDate);
Calendar end = new GregorianCalendar();
end.setTime(endDate);
for (Date date = start.getTime(); start.before(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
// Do your job here with `date`.
Calendar customDate = Calendar.getInstance();
customDate.setTime(date);
//You can play with the final age in year here
years = 2022 - start.get(Calendar.YEAR);
months = start.get(Calendar.MONTH);
days = start.get(Calendar.DAY_OF_MONTH);
LocalDate referenceDate = calculateDate2(years, months, days);
Integer estimatedAge [] = calculateAge(referenceDate);
if(years != estimatedAge [0] || months != estimatedAge[1] || days != estimatedAge[2]) {
Period inAge = Period.of(years, months, days);
LocalDate inDate = LocalDate.now().minus(inAge);
Period outAge = Period.of(estimatedAge[0], estimatedAge[1], estimatedAge[2]);
LocalDate outDate = LocalDate.now().minus(outAge);
Period diference = Period.between(inDate, outDate);
if(diference.getDays() <=1 && diference.getMonths() == 0 && diference.getYears() == 0) {
pass++;
}
else {
errors++;
}
}
else {
pass++;
//System.out.println("P A S S - Años: " + edad + " Meses: " + meses + " Días: " + dias );
}
}
total = errors + pass;
System.out.println("Total ages test: " + total);
System.out.println("Errors: " + errors + " - " + (((float)errors/total)*100) + "%");
System.out.println("Pass: " + pass + " - " + (((float)pass/total)*100) + "%");
}
}