Skip to content

Commit 868ae43

Browse files
authored
AGE CALCULATER
1 parent 7f1df0c commit 868ae43

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Diff for: AGE CALCULATER

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.candidjava.time;
2+
3+
import java.text.ParseException;
4+
import java.text.SimpleDateFormat;
5+
import java.time.LocalDate;
6+
import java.time.Period;
7+
import java.util.Calendar;
8+
import java.util.Date;
9+
10+
public class DobConversion {
11+
public static void main(String[] args) throws ParseException {
12+
//direct age calculation
13+
LocalDate l = LocalDate.of(1998, 04, 23); //specify year, month, date directly
14+
LocalDate now = LocalDate.now(); //gets localDate
15+
Period diff = Period.between(l, now); //difference between the dates is calculated
16+
System.out.println(diff.getYears() + "years" + diff.getMonths() + "months" + diff.getDays() + "days");
17+
18+
//using Calendar Object
19+
String s = "1994/06/23";
20+
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
21+
Date d = sdf.parse(s);
22+
Calendar c = Calendar.getInstance();
23+
c.setTime(d);
24+
int year = c.get(Calendar.YEAR);
25+
int month = c.get(Calendar.MONTH) + 1;
26+
int date = c.get(Calendar.DATE);
27+
LocalDate l1 = LocalDate.of(year, month, date);
28+
LocalDate now1 = LocalDate.now();
29+
Period diff1 = Period.between(l1, now1);
30+
System.out.println("age:" + diff1.getYears() + "years");
31+
}
32+
}
33+
//PLEASE SEE MY CODE.HOW IS IT.

0 commit comments

Comments
 (0)