-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathJava.java
More file actions
46 lines (38 loc) · 1.37 KB
/
Java.java
File metadata and controls
46 lines (38 loc) · 1.37 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
/****************************************/
/* */
/* CodinGame.com Solutions by pathosDev */
/* */
/* Puzzle: How time flies */
/* Difficulty: Easy */
/* Date solved: 05.03.2019 */
/* */
/****************************************/
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Solution
{
public static void main(String[] args) throws ParseException
{
//Read dates.
Scanner scanner = new Scanner(System.in);
SimpleDateFormat dateParser = new SimpleDateFormat("dd.MM.yyyy");
long begin = dateParser.parse(scanner.nextLine()).getTime();
long end = dateParser.parse(scanner.nextLine()).getTime();
//Calculate result.
long days = (end - begin) / (1000 * 60 * 60 * 24);
int months = new Date(end - begin).getMonth();
long years = days / 365;
//Print result.
if (years > 0)
{
System.out.printf("%d year%s, ", years, years > 1 ? "s" : "");
}
if (months > 0)
{
System.out.printf("%d month%s, ", months, months > 1 ? "s" : "");
}
System.out.printf("total %d days", days);
}
}