让picocli解析本地日期格式

PicoCLI 接受2019-04-26作为LocalDate变量的输入,但它不接受像26.04.2019.
为此你需要:

SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy",Locale.GERMANY);

您如何告诉 PicoCLI 使用此格式化程序而不依赖于美国日期输入?


慕村225694
浏览 189回答 1
1回答

UYOU

您可以为特定选项或全局为特定类型的所有选项和位置参数定义自定义类型转换器。注册自定义转换器最紧凑的方式通常是使用 lambda 表达式:new CommandLine(new DateFormatDemo())&nbsp; &nbsp; &nbsp; .registerConverter(Date.class,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s -> new SimpleDateFormat("dd.MM.yyyy", Locale.GERMANY).parse(s))&nbsp; &nbsp; &nbsp; .execute(args);如果需要为特定选项设置转换器,则需要定义一个类并在@Option(converter = X.class)该选项的注释中指定该类。ITypeConverter.convert请注意,如果用户输入无效,则可以从方法中抛出异常。Picocli 将捕获此异常并向最终用户显示一条错误消息。例如:class StrictGermanDateConverter implements ITypeConverter<Date> {&nbsp; &nbsp; @Override&nbsp; &nbsp; public Date convert(String value) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; Date result = new SimpleDateFormat("dd.MM.yyyy", Locale.GERMANY).parse(value);&nbsp; &nbsp; &nbsp; &nbsp; if (result.getYear() < 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException("year should be after 1900");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }}下面是一个使用这个更严格的转换器来演示的例子:错误检查java.util.Date为所有选项注册一个全局类型转换器无效输入导致 picocli 显示一条错误消息,后跟使用帮助消息import picocli.CommandLine;import picocli.CommandLine.Command;import picocli.CommandLine.ITypeConverter;import picocli.CommandLine.Model.CommandSpec;import picocli.CommandLine.Option;import picocli.CommandLine.Spec;import java.text.SimpleDateFormat;import java.util.Date;import java.util.List;import java.util.Locale;@Command(name = "demo")public class DateFormatDemo implements Runnable {&nbsp; &nbsp; @Option(names = {"-d", "--date"}, description = "Date in German format `dd.MM.yyyy`",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; converter = StrictGermanDateConverter.class, paramLabel = "dd.MM.yyyy")&nbsp; &nbsp; Date specialDate;&nbsp; &nbsp; @Option(names = {"-x", "--default"}, paramLabel = "yyyy-MM-dd",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description = "This option uses the default converter")&nbsp; &nbsp; Date defaultDate;&nbsp; &nbsp; @Spec CommandSpec spec;&nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; List<String> args = spec.commandLine().getParseResult().originalArgs();&nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("%s -> %s; %s%n", args, specialDate, defaultDate);&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; // invalid input: shows error message and usage help&nbsp; &nbsp; &nbsp; &nbsp; new CommandLine(new DateFormatDemo()).execute("-d=55.55.55");&nbsp; &nbsp; &nbsp; &nbsp; // alternatively, register a global converter&nbsp; &nbsp; &nbsp; &nbsp; // for _all_ Date options&nbsp; &nbsp; &nbsp; &nbsp; new CommandLine(new DateFormatDemo())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .registerConverter(Date.class,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s -> new SimpleDateFormat("MMM.dd.yyyy", Locale.ITALIAN).parse(s))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .execute("-d=31.07.1969", "--default=Gennaio.01.2020");&nbsp; &nbsp; }}使用无效输入的第一次调用-d=55.55.55打印以下输出:Invalid value for option '--date': cannot convert '55.55.55' to Date (java.lang.IllegalArgumentException: year should be after 1900)Usage: demo [-d=dd.MM.yyyy] [-x=yyyy-MM-dd]&nbsp; -d, --date=dd.MM.yyyy&nbsp; &nbsp; &nbsp; Date in German format `dd.MM.yyyy`&nbsp; -x, --default=yyyy-MM-dd&nbsp; &nbsp;This option uses the default converter第二次调用,我们传递--default=Gennaio.01.2020以确认全局类型转换器现在以我们的自定义意大利语格式处理日期,提供以下输出:[-d=31.07.1969, --default=Gennaio.01.2020] -> Thu Jul 31 00:00:00 JST 1969; Wed Jan 01 00:00:00 JST 2020
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java