Skip to content

Commit c82aa5d

Browse files
authored
Add a class example with the use of Dates
You can se how to calculate from and to an age express in years, months and days.
1 parent bbe3b15 commit c82aa5d

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

DateAndAge.java

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

0 commit comments

Comments
 (0)