66
77import java .math .BigDecimal ;
88import java .text .ParseException ;
9- import java .text .SimpleDateFormat ;
109import java .util .Calendar ;
1110import java .util .Date ;
1211import java .util .TimeZone ;
@@ -75,7 +74,11 @@ public void parseOffsetPlus() throws ParseException {
7574
7675 @ Test
7776 public void parseOffsetPrecise () throws ParseException {
77+ assertEquals (851017197100L , parse ("1996-12-19T16:39:57.1-01:00" ).getTime ());
78+ assertEquals (851017197120L , parse ("1996-12-19T16:39:57.12-01:00" ).getTime ());
7879 assertEquals (851017197123L , parse ("1996-12-19T16:39:57.123-01:00" ).getTime ());
80+ assertEquals (851017197123L , parse ("1996-12-19T16:39:57.1234-01:00" ).getTime ());
81+ assertEquals (851017197123L , parse ("1996-12-19T16:39:57.12345-01:00" ).getTime ());
7982 assertEquals (851017197123L , parse ("1996-12-19T16:39:57.123456-01:00" ).getTime ());
8083 }
8184
@@ -163,55 +166,4 @@ public void parsePreciseFail() throws ParseException{
163166 String timeString = "1996-12-19T16:39:57.123−01:30" ;
164167 Rfc3339 .parsePrecise (timeString );
165168 }
166-
167- @ Test
168- public void exec (){
169- assertEquals (851008197000L , modTimeToMilis ("1996-12-19T16:39:57.123+01:30" ));
170- assertEquals (851008197000L , modTimeToMilis ("1996-12-19T16:39:57.123456+01:30" ));
171- assertEquals (482196050000L , modTimeToMilis ("1985-04-12T23:20:50.520000Z" ));
172- }
173- private long modTimeToMilis (String modTime ) {
174-
175- if (modTime .lastIndexOf ("+" ) > 19 || modTime .lastIndexOf ("-" ) > 19 ){
176- return modTimeZonedToMillis (modTime );
177- }
178-
179- modTime = modTime .toUpperCase ();
180- String [] dateTime = modTime .split ("T" );
181- String yearMonthDay = dateTime [0 ];
182- String hourMinuteSecond = dateTime [1 ].substring (0 , dateTime [1 ].length () - 1 );
183- if (hourMinuteSecond .contains ("." )) {
184- int index = hourMinuteSecond .indexOf ("." );
185- hourMinuteSecond = hourMinuteSecond .substring (0 , index );
186- }
187-
188- String formattedDate = yearMonthDay + " " + hourMinuteSecond + " UTC" ;
189- long dateInMillis ;
190- Date date ;
191- SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss z" );
192- try {
193- date = simpleDateFormat .parse (formattedDate );
194- dateInMillis = date .getTime ();
195- } catch (ParseException e ) {
196- e .printStackTrace ();
197- dateInMillis = 0 ;
198- }
199-
200- return dateInMillis ;
201- }
202-
203- private long modTimeZonedToMillis (String modTime ){
204- SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ssXXX" );
205- int index = modTime .lastIndexOf ("+" );
206- if (index == -1 ){
207- index = modTime .lastIndexOf ("-" );
208- }
209- int fractionIndex = modTime .indexOf ('.' );
210- String reducedString = fractionIndex == -1 ? modTime : modTime .substring (0 , fractionIndex ) + modTime .substring (index );
211- try {
212- return format .parse (reducedString ).getTime ();
213- } catch (ParseException e ) {
214- return 0L ;
215- }
216- }
217169}
0 commit comments