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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
| package test.birthdays;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
public class Birthdays {
public static void main(String[] args) {
ArrayList<Friends> arr = new ArrayList<Friends>();
Date d = new Date();
Calendar cal = Calendar.getInstance();
Birthday b;
cal.set(1982, 1, 15);
d = cal.getTime();
arr.add(new Friends("Игорь", "Биг", d));
cal.set(1980, 8, 11);
d = cal.getTime();
arr.add(new Friends("Вова", "Уткин", d));
for (Friends item : arr) {
b = item.setBir();
System.out.println(item.name + " " + item.sirname);
String s = String.format("Дата рождения: %tA, %td, %tB, %tY", d, d, d, d);
System.out.println(s);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, d MMMMM yyyy");
System.out.println("Следующий ДР будет: " + sdf.format(b.findDateOfBirthday()));
if (b.findDateOfBirthdayWeekend())
System.out.println("Следующий ДР приходится на выходные");
else
System.out.println("Следующий ДР не приходится на выходные");
System.out.println("До следующего ДР остается " + b.findDaysBefDateOfBirthday() + "дней");
System.out.println("Возраст друга - " + b.findAge() + " лет");
b.findAgeInMonthsAndDays();
System.out.println("Возраст друга в днях: " + b.findAgeInDays());
}
}
}
import java.util.Date;
public class Friends {
public String name;
public String sirname;
private final Date d1;
public Birthday b;
public Friends(String name, String sirname, Date d) {
this.name = name;
this.sirname = sirname;
d1 = d;
}
public Birthday setBir() {
b = new Birthday(d1);
return b;
}
}
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Birthday {
private final Date d1;
public Birthday(Date dp1){
d1 = dp1;
}
public Date getDateOfBirthday(){
return d1;
}
public Date findDateOfBirthday(){
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(d1);
cal1.set(Calendar.YEAR, cal2.get(Calendar.YEAR));
if (cal1.before(cal2))
cal1.set(Calendar.YEAR, cal1.get(Calendar.YEAR) + 1);
return cal1.getTime();
}
public boolean findDateOfBirthdayWeekend(){
Date d2 = new Date();
Date d3 = d1;
Calendar cal1 = Calendar.getInstance();
cal1.setTime(findDateOfBirthday());
cal1.get(Calendar.DAY_OF_WEEK);
if (cal1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY || cal1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY){
return true;
}
return false;
}
public long findDaysBefDateOfBirthday(){
Date now = new Date();
Date nb = findDateOfBirthday();
long days = nb.getTime() - now.getTime();
//System.out.println("ПРОВЕРКА days = " + days);
days = days / 1000 / 60 / 60 / 24 + 1;
return days;
}
public int findAge(){
Date d2 = new Date();
Calendar cal3 = Calendar.getInstance();
Calendar cal4 = Calendar.getInstance();
cal3.setTime(d2);
int thisYear = cal3.get(Calendar.YEAR);
cal4.setTime(d1);
int birYear = cal4.get(Calendar.YEAR);
int age = thisYear - birYear;
cal4.set(Calendar.YEAR, cal3.get(Calendar.YEAR));
if (cal4.after(cal3))
age--;
return age;
}
public void findAgeInMonthsAndDays() {
int razn_m, razn_d = 0;
Date d2 = new Date();
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(d1);
cal1.set(Calendar.YEAR, cal2.get(Calendar.YEAR));
if (cal1.before(cal2)) {
razn_m = cal2.get(Calendar.MONTH) - cal1.get(Calendar.MONTH) - 1;
if (cal1.get(Calendar.DAY_OF_MONTH) < cal2.get(Calendar.DAY_OF_MONTH)) {
razn_m = razn_m + 1;
razn_d = cal2.get(Calendar.DAY_OF_MONTH) - cal1.get(Calendar.DAY_OF_MONTH);
System.out.println("Этому человеку " + findAge() + " лет " + razn_m + " месяцев " + razn_d + " дней");
}
else if (cal1.get(Calendar.DAY_OF_MONTH) > cal2.get(Calendar.DAY_OF_MONTH)) {
switch (cal1.get(Calendar.MONTH)){
case 0:{
razn_d = 31 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
case 1:{
if ((cal1.get(Calendar.YEAR) % 4 == 0) && (cal1.get(Calendar.YEAR) % 100 != 0 || cal1.get(Calendar.YEAR) % 400 == 0)) {
razn_d = 29 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
}
else
razn_d = 28 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
case 2:{
razn_d = 31 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
case 3:{
razn_d = 30 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
case 4:{
razn_d = 31 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
case 5:{
razn_d = 30 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
case 6:{
razn_d = 31 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
case 7:{
razn_d = 31 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
case 8:{
razn_d = 30 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
case 9:{
razn_d = 31 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
case 10:{
razn_d = 30 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
case 11:{
razn_d = 31 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
}
System.out.println("Этому человеку " + findAge() + " лет " + razn_m + " месяцев " + razn_d + " дней");
}
else {
razn_m = razn_m + 1;
System.out.println("Этому человеку " + findAge() + " лет " + razn_m + " месяцев " + 0 + " дней");
}
}
else {
razn_m = 12 - (cal1.get(Calendar.MONTH) - cal2.get(Calendar.MONTH)) - 1;
if (cal2.get(Calendar.DAY_OF_MONTH) < cal1.get(Calendar.DAY_OF_MONTH)) {
//razn_d = cal1.get(Calendar.DAY_OF_MONTH) - cal2.get(Calendar.DAY_OF_MONTH);
switch (cal1.get(Calendar.MONTH)){
case 0:{
razn_d = 31 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
case 1:{
if ((cal1.get(Calendar.YEAR) % 4 == 0) && (cal1.get(Calendar.YEAR) % 100 != 0 || cal1.get(Calendar.YEAR) % 400 == 0)) {
razn_d = 29 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
}
else
razn_d = 28 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
case 2:{
razn_d = 31 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
case 3:{
razn_d = 30 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
case 4:{
razn_d = 31 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
case 5:{
razn_d = 30 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
case 6:{
razn_d = 31 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
case 7:{
razn_d = 31 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
case 8:{
razn_d = 30 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
case 9:{
razn_d = 31 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
case 10:{
razn_d = 30 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
case 11:{
razn_d = 31 - cal1.get(Calendar.DAY_OF_MONTH) + cal2.get(Calendar.DAY_OF_MONTH);
break;}
}
System.out.println("Этому человеку " + findAge() + " лет " + razn_m + " месяцев " + razn_d + " дней");
}
else if (cal2.get(Calendar.DAY_OF_MONTH) > cal1.get(Calendar.DAY_OF_MONTH)) {
razn_m = razn_m + 1;
razn_d = cal2.get(Calendar.DAY_OF_MONTH) - cal1.get(Calendar.DAY_OF_MONTH);
System.out.println("Этому человеку " + findAge() + " лет " + razn_m + " месяцев " + razn_d + " дней");
}
else {
razn_m = razn_m + 1;
System.out.println("Этому человеку " + findAge() + " лет " + razn_m + " месяцев " + 0 + " дней");
}
}
}
public long findAgeInDays(){
Date d2 = new Date();
long days = d2.getTime() - d1.getTime();
days = days / 1000 / 60 / 60 / 24 + 1;
return days;
}
public void getDateOfBirsday(){
SimpleDateFormat sdf = new SimpleDateFormat("День рождения dd MMMM yyyy");
System.out.println(sdf.format(d1));
}
public void getNextDateOfBirsday(){
Date d2 = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("День рождения будет dd.MM.yyyy");
Calendar cal5 = Calendar.getInstance();
Calendar cal6 = Calendar.getInstance();
cal5.setTime(d1);
cal5.set(Calendar.YEAR, cal6.get(Calendar.YEAR));
if (cal5.before(cal6)) {
cal5.set(Calendar.YEAR, cal6.get(Calendar.YEAR) + 1);
System.out.println(sdf.format(cal5.getTime()));
}
else
System.out.println(sdf.format(cal5.getTime()));
}
} |