This repository was archived by the owner on Jan 6, 2026. It is now read-only.
forked from microsoftgraph/msgraph-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateOnly.java
More file actions
106 lines (90 loc) · 2.3 KB
/
Copy pathDateOnly.java
File metadata and controls
106 lines (90 loc) · 2.3 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
package com.microsoft.graph.models.extensions;
import java.text.ParseException;
import java.util.Locale;
/**
* A timezone-nonspecific date
*/
public class DateOnly {
/**
* The year
*/
private final int mYear;
/**
* The month
*/
private final int mMonth;
/**
* The day
*/
private final int mDay;
/**
* Constructs a timezone-nonspecific DateOnly
*
* @param dateStr date string of the form <code>yyyy-mm-dd</code>
* @return the parsed DateOnly instance
* @exception ParseException If there was a failure parsing the dateStr
*/
public static DateOnly parse(final String dateStr) throws ParseException {
// break the date up into its constituent parts
String[] dateInfo = dateStr.split("-");
// validate the split date string
final int expectedLength = 3;
if (dateInfo.length != expectedLength) {
throw new ParseException(
"Expected datestring format 'yyyy-mm-dd' but found: " + dateStr, 0
);
}
// array indices for date parsing
final int indYear = 0;
final int indMonth = 1;
final int indDay = 2;
// unpack this array
int year = Integer.parseInt(dateInfo[indYear]);
int month = Integer.parseInt(dateInfo[indMonth]);
int day = Integer.parseInt(dateInfo[indDay]);
return new DateOnly(year, month, day);
}
/**
* Constructs a timezone-nonspecific DateOnly
*
* @param year the year
* @param month 1-indexed month value (Jan == 1)
* @param day day of the month
*/
public DateOnly(final int year, final int month, final int day) {
mYear = year;
mMonth = month;
mDay = day;
}
/**
* Gets the year
*
* @return the year
*/
public int getYear() {
return mYear;
}
/**
* Gets the month
*
* @return the month
*/
public int getMonth() {
return mMonth;
}
/**
* Gets the day
*
* @return the day
*/
public int getDay() {
return mDay;
}
@Override
public String toString() {
return String.format(
Locale.ROOT,
"%04d-%02d-%02d", mYear, mMonth, mDay
);
}
}