亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

讓picocli解析本地日期格式

讓picocli解析本地日期格式

慕村225694 2023-02-16 15:38:42
PicoCLI 接受2019-04-26作為LocalDate變量的輸入,但它不接受像26.04.2019.為此你需要:SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy",Locale.GERMANY);您如何告訴 PicoCLI 使用此格式化程序而不依賴于美國日期輸入?
查看完整描述

1 回答

?
UYOU

TA貢獻1878條經驗 獲得超4個贊

您可以為特定選項或全局為特定類型的所有選項和位置參數定義自定義類型轉換器。


注冊自定義轉換器最緊湊的方式通常是使用 lambda 表達式:


new CommandLine(new DateFormatDemo())

      .registerConverter(Date.class,

                         s -> new SimpleDateFormat("dd.MM.yyyy", Locale.GERMANY).parse(s))

      .execute(args);

如果需要為特定選項設置轉換器,則需要定義一個類并在@Option(converter = X.class)該選項的注釋中指定該類。


ITypeConverter.convert請注意,如果用戶輸入無效,則可以從方法中拋出異常。Picocli 將捕獲此異常并向最終用戶顯示一條錯誤消息。


例如:


class StrictGermanDateConverter implements ITypeConverter<Date> {

    @Override

    public Date convert(String value) throws Exception {

        Date result = new SimpleDateFormat("dd.MM.yyyy", Locale.GERMANY).parse(value);

        if (result.getYear() < 0) {

            throw new IllegalArgumentException("year should be after 1900");

        }

        return result;

    }

}

下面是一個使用這個更嚴格的轉換器來演示的例子:


錯誤檢查

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 {


    @Option(names = {"-d", "--date"}, description = "Date in German format `dd.MM.yyyy`",

            converter = StrictGermanDateConverter.class, paramLabel = "dd.MM.yyyy")

    Date specialDate;


    @Option(names = {"-x", "--default"}, paramLabel = "yyyy-MM-dd",

            description = "This option uses the default converter")

    Date defaultDate;


    @Spec CommandSpec spec;


    public void run() {

        List<String> args = spec.commandLine().getParseResult().originalArgs();

        System.out.printf("%s -> %s; %s%n", args, specialDate, defaultDate);

    }


    public static void main(String[] args) {


        // invalid input: shows error message and usage help

        new CommandLine(new DateFormatDemo()).execute("-d=55.55.55");


        // alternatively, register a global converter

        // for _all_ Date options

        new CommandLine(new DateFormatDemo())

              .registerConverter(Date.class,

                   s -> new SimpleDateFormat("MMM.dd.yyyy", Locale.ITALIAN).parse(s))

              .execute("-d=31.07.1969", "--default=Gennaio.01.2020");

    }

}

使用無效輸入的第一次調用-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]

  -d, --date=dd.MM.yyyy      Date in German format `dd.MM.yyyy`

  -x, --default=yyyy-MM-dd   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



查看完整回答
反對 回復 2023-02-16
  • 1 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號