-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDateDemo.java
98 lines (86 loc) · 1.43 KB
/
DateDemo.java
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
class Date{
private int date,month;
/*private String[] months = { "January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "October" ,"November" ,"December"};*/
private int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
Date()
{
this(1,1);
}
Date(int d, int m)
{
date=d;
month=m;
}
/*public int daysInMonth()
{
for(int i=0;i<12;i++)
{
System.out.println(months[i]+" : "+days[i]);
}
return -1;
}*/
public int daysInMonth(int x)
{
return ((x>0&&x<13)?days[x-1]:-1);
}
public int daysInMonth()
{
return ((month>0&&month<13)?days[month-1]:-1);
}
public int getDay()
{
return date;
}
public int getMonth()
{
return month;
}
void nextDay()
{
date=date%days[month-1]+1;
if(date==1)
{
month=month%12+1;
}
}
public String toString()
{
String s=" ";
if(date<10)
{
s+="0";
}
s+=date+"/";
if(month<10)
{
s+="0";
}
s+=month;
return s;
}
int absoluteDay()
{
int a=date;
for(int i=0;i<month-1;i++)
{
a+=days[i];
}
return a;
}
}
public class DateDemo{
public static void main(String[] args)
{
Date jan1 = new Date(1,1);
for(int i=0;i<367;i++)
{
System.out.print(jan1);
System.out.println(" " + jan1.absoluteDay());
jan1.nextDay();
}
System.out.println(jan1.daysInMonth());
System.out.println(jan1.daysInMonth(4));
System.out.println(jan1.getMonth());
System.out.println(jan1.getDay());
}
}