集册 Java实例教程 Java Parse预期格式:a 3

Java Parse预期格式:a 3

欢马劈雪     最近更新时间:2020-01-02 10:19:05

636
Java Parse预期格式:一个月、一天(一个或两个数字)和一个四位数年份的3字符表示

import java.io.PrintStream;

import java.time.LocalDate;/** from N o w J a v a . c o m - 时代Java**/

import java.time.format.DateTimeParseException;

import java.time.format.DateTimeFormatter;


/*

 * Parses the input at the command line. The expected format is

 * MMM d yyyy. In other words, a 3-character representation of

 * the month, the day (one or two digits), and a 4-digit year.

 */

public class Parse {


    public static void main(String[] args) {

        if (args.length < 3) {

            System.out

                    .printf("Usage: Parse <3-char month> <day> <4-digit year>%n");

            throw new IllegalArgumentException();

        }

        String input = args[0] + ' ' + args[1] + ' ' + args[2];

        try {/*来 自 n  o  w  j  a  v  a . c o m*/

            DateTimeFormatter formatter = DateTimeFormatter

                    .ofPattern("MMM d yyyy");

            LocalDate date = LocalDate.parse(input, formatter);

            System.out.printf("%s%n", date);

        } catch (DateTimeParseException exc) {

            System.out.printf("%s is not parsable!%n", input);

            throw exc; // Rethrow the exception.

        }

        // 'date' has been successfully parsed

    }

}


展开阅读全文