forked from cbeust/jcommander
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ISO8601 formats for Date Converter, converter for LocalDate - c…
…beust#493 resolves
- Loading branch information
1 parent
0525445
commit b9e7eed
Showing
6 changed files
with
214 additions
and
22 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
src/main/java/com/beust/jcommander/converters/DateConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Copyright (C) 2010 the original author or authors. | ||
* See the notice.md file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.beust.jcommander.converters; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Converts a String to a Date. | ||
* | ||
* @author Ernest Kalimullin | ||
*/ | ||
public abstract class DateConverter<T> extends BaseConverter<T> { | ||
|
||
protected static final List<String> DATE_FORMAT_LIST = new ArrayList<>(); | ||
|
||
static { | ||
initPatterns( | ||
"yyyy-MM-dd", | ||
"yyyy-MM", | ||
"dd.MM.yyyy", | ||
"MM/dd/yy", | ||
"hh:mm", | ||
"hhmmss", | ||
"hh:mm:ss" | ||
); | ||
} | ||
|
||
public DateConverter(String optionName) { | ||
super(optionName); | ||
} | ||
|
||
private static void initPatterns(String... patterns) { | ||
for (String pattern : patterns) { | ||
try { | ||
DATE_FORMAT_LIST.add(pattern); | ||
} catch (IllegalArgumentException e) { | ||
throw new IllegalStateException("Incorrect date pattern.", e); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/com/beust/jcommander/converters/ISO8601LocalDateConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* Copyright (C) 2010 the original author or authors. | ||
* See the notice.md file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.beust.jcommander.converters; | ||
|
||
import com.beust.jcommander.ParameterException; | ||
|
||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeParseException; | ||
|
||
public class ISO8601LocalDateConverter extends DateConverter<LocalDate> { | ||
|
||
public ISO8601LocalDateConverter(String optionName) { | ||
super(optionName); | ||
} | ||
|
||
public LocalDate convert(String value) { | ||
for (String pattern : DATE_FORMAT_LIST) { | ||
try { | ||
return LocalDate.parse(value, DateTimeFormatter.ofPattern(pattern)); | ||
} catch (DateTimeParseException ignored) { | ||
continue; | ||
} | ||
} | ||
throw new ParameterException(getErrorString(value, "an ISO-8601 formatted time")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/test/java/com/beust/jcommander/converters/ISO8601DateConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Copyright (C) 2010 the original author or authors. | ||
* See the notice.md file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.beust.jcommander.converters; | ||
|
||
import com.beust.jcommander.JCommander; | ||
import com.beust.jcommander.Parameter; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
|
||
@Test | ||
public class ISO8601DateConverterTest { | ||
|
||
@Test | ||
public void testDate() throws ParseException { | ||
class Arg { | ||
@Parameter(names = "-date", converter = ISO8601DateConverter.class) | ||
Date date; | ||
} | ||
String format = "dd.MM.yyyy"; | ||
|
||
SimpleDateFormat formatter = new SimpleDateFormat(format); | ||
Date dateNow = formatter.parse(formatter.format(new Date())); | ||
Arg command = new Arg(); | ||
JCommander jc = JCommander.newBuilder().addObject(command).build(); | ||
jc.parse("-date", new SimpleDateFormat(format).format(dateNow)); | ||
|
||
Assert.assertEquals(command.date, dateNow); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
src/test/java/com/beust/jcommander/converters/ISO8601LocalDateConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Copyright (C) 2010 the original author or authors. | ||
* See the notice.md file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.beust.jcommander.converters; | ||
|
||
import com.beust.jcommander.JCommander; | ||
import com.beust.jcommander.Parameter; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
@Test | ||
public class ISO8601LocalDateConverterTest { | ||
|
||
@Test | ||
public void testDateTime() { | ||
class Arg { | ||
@Parameter(names = "-datetime", converter = ISO8601LocalDateConverter.class) | ||
LocalDate date; | ||
} | ||
String format = "hhmm"; | ||
|
||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format); | ||
LocalDate dateNow = LocalDate.parse(formatter.format(LocalDate.now())); | ||
Arg command = new Arg(); | ||
JCommander jc = JCommander.newBuilder().addObject(command).build(); | ||
jc.parse("-datetime", dateNow.format(DateTimeFormatter.ofPattern(format))); | ||
|
||
Assert.assertEquals(command.date, dateNow); | ||
} | ||
|
||
} |