-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumofdaysintwodates.java
More file actions
128 lines (113 loc) · 3.63 KB
/
numofdaysintwodates.java
File metadata and controls
128 lines (113 loc) · 3.63 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
122
123
124
125
126
127
128
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package practice;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;
/**
*
* @author USER
*/
public class DaysNumber {
public static void main(String[] args) {
int day = 0;
System.out.println("Enter the date in yyyy-mm-dd");
Scanner sc = new Scanner(System.in);
String userdate = sc.nextLine();
java.sql.Date currentdate = new java.sql.Date(Calendar.getInstance().getTime().getTime());
String[] ud = userdate.split("-");
String[] cd = currentdate.toString().split("-");
int cyear = Integer.parseInt(cd[0]);
int cmonth = Integer.parseInt(cd[1]);
int cday = Integer.parseInt(cd[2]);
int uyear = Integer.parseInt(ud[0]);
int umonth = Integer.parseInt(ud[1]);
int uday = Integer.parseInt(ud[2]);
//userdate = 2014-10-05 currentdate=2018-03-03
//for years 2015 2016 2017
for (int i = uyear + 1; i < cyear; i++) {
if ((i % 4 == 0 && i % 100 != 0) || (i % 400) == 0) {
day += 366;
} else {
day += 365;
}
}
//userdate = 2014-10-05 thus, adding the days of months 11 and 12
for (int i = umonth + 1; i <= 12; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day += 31;
break;
case 2:
if ((uyear % 4 == 0 && uyear % 100 != 0) || (uyear % 400) == 0) {
day += 29;
} else {
day += 28;
}
break;
default:
day += 30;
break;
}
}
//adding the days of usermonth ie 10
switch (umonth) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day += 31 - uday;
break;
case 2:
if ((uyear % 4 == 0 && uyear % 100 != 0) || (uyear % 400) == 0) {
day += 29 - uday;
} else {
day += 28 - uday;
}
break;
default:
day += 30 - uday;
break;
}
//currentdate = 2018-03-03 thus adding the days of month 01 and 02
for (int i = 1; i < cmonth; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day += 31;
break;
case 2:
if ((cyear % 4 == 0 && cyear % 100 != 0) || (cyear % 400) == 0) {
day += 29;
} else {
day += 28;
}
break;
default:
day += 30;
break;
}
}
//adding the days of month 03 excluding 3 march itself
day += cday - 1;
System.out.println(day);
}
}