- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDateFunctions.java
More file actions
Latest commit
executable file
·109 lines (93 loc) · 2.83 KB
/
Copy pathDateFunctions.java
File metadata and controls
executable file
·109 lines (93 loc) · 2.83 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
/* Librarian Assistant Pro - Version 1.7
* Class: DateFunctions
* Duties:
* This class helps us to deal with dates.
* Since dealing with dates are usually not
* very straightforward the methods in this
* class can be very helpful.
* Date Validation, Date Manipulation and
* Date to String conversions are functionalities
* of this class
* Creation Date: November 2008
*/
importjava.util.Date;
importjava.text.SimpleDateFormat;
classDateFunctions
{
//Validating the dates
//Extra days and months are authomatically reduced
//to the maximum possible values
publicDatedateValidator(StringdateStr)
{
intday = Integer.parseInt(dateStr.substring(0,2));
intmonth = Integer.parseInt(dateStr.substring(2,4));
intyear = Integer.parseInt(dateStr.substring(4));
if (month > 12)
month = 12;
elseif(month < 1)
month = 1;
//Months with 31 days
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
if (day > 31)
day = 31;
}
//Months with 30 days
elseif(month == 4 || month == 6 || month == 9 || month == 11)
{
if (day > 30)
day = 30;
}
//months with 28 days
else
{
if (day > 28)
day = 28;
}
if (day < 1)
{
day = 1;
}
if (year <= 1900)
year = 100; // Will set the year to 2000 --> 1900 + 100
else
year = year - 1900; // Sets the year to user's input; otherwise year
// would be equal to user input + 1900
Datedate = newDate(year, month-1, day);
System.out.println(dateToString(date));
returndate;
}
//Checks whether date is already passed or not
publicbooleanisDatePassed(Datedate)
{
DatecurrentDate = newDate();
if(currentDate.after(date)) //Checking if the date has been passed
returntrue;
else
returnfalse;
}
//Converts date to a string in the form dd/mm/yyyy
publicStringdateToString(Datedate)
{
SimpleDateFormatdf = newSimpleDateFormat("dd/MM/yyyy");
StringBuilderstring = newStringBuilder(df.format(date));
StringreturnString = "" + string;
returnreturnString;
}
//Adds 'num' days to 'date' and returns the result
//as another date
publicDateaddDaysToDate(Datedate, intnum)
{
SimpleDateFormatdf = newSimpleDateFormat("ddMMyyyy");
StringBuilderstring = newStringBuilder(df.format(date));
StringdateStr = "" + string;
intdateInt = Integer.parseInt(dateStr);
intresultDateInt = dateInt + num * 1000000;
StringresultDateStr = "" + resultDateInt;
intday = Integer.parseInt(resultDateStr.substring(0,2));
intmonth = Integer.parseInt(resultDateStr.substring(2,4));
intyear = Integer.parseInt(resultDateStr.substring(4));
DateresultDate = newDate(year-1900, month-1, day);
returnresultDate;
}
}