简体   繁体   English

解析方法未正确处理格式

[英]Parse method not handling formatting correctly

My code is supposed to print out the difference between the entered date and now.我的代码应该打印出输入日期和现在之间的差异。 Then the output will be formatted according to the ISO LocalDate format.然后输出将根据 ISO LocalDate 格式进行格式化。

Parse method not handling formatting correctly.解析方法未正确处理格式。

import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.io.*;
import java.time.*;

//Main class
public class TimeBetween{
public static void main(String[] args){
    String dateFormat = "MMMM d, yyyy";
    LocalDate aDate = null;
    boolean validStr = false;

 //parse method not handling formatting correctly.
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    while(validStr==false){
        System.out.print("Enter a date: ");
//Try and catch 
        try {
            String dateEntered = br.readLine();
            aDate = LocalDate.parse(dateEntered,formatter);
            validStr = true;
        }catch(IOException | DateTimeParseException ex){
            validStr = false;
        }

    }

    System.out.println("Date entered was: "+aDate);
    LocalDate now = LocalDate.now(); 

    Period between;
    if(aDate.isBefore(now)){
        between = Period.between(aDate,now);
    }else{
        between = Period.between(now,aDate);
    }

    int years = between.getYears();
    int months = between.getMonths();
    int days =  between.getDays();

//Printing out year month and day differences
    System.out.println("There are"    +years+"years"+months+"months"+days+"days");
    }
}

Enter month in the case used in your locale在您的语言环境中使用的情况下输入月份

As your program stands in the question the user is required to enter the month in the expected case, for example uppercase F and lowercase ebruary for February.由于您的程序出现在问题中,用户需要在预期的情况下输入月份,例如大写F和小写ebruary为二月。 (Edit:) Pattern letter uppercase M is used for month while lowercase m means minutes. (编辑:)模式字母大写M用于月份,而小写m表示分钟。 Therefore the M s in the format pattern string have to be in uppercase even though you are not supposed to type the month that way, and it also isn't formatted in uppercase if you format a date back into a string.因此,即使您不应该以这种方式输入月份,格式模式字符串中的M也必须是大写的,而且如果您将日期重新格式化为字符串,它也不会被格式化为大写。

Tip: When parsing using a formatter won't work, try formatting a value with the same formatter to see what it expects your input string to look like.提示:当使用格式化程序解析不起作用时,请尝试使用相同的格式化程序格式化值以查看它期望您的输入字符串的外观。 For example:例如:

    DateTimeFormatter formatter
            = DateTimeFormatter.ofPattern(dateFormat, Locale.forLanguageTag("en-IE"));
    System.out.println(LocalDate.now(ZoneId.of("Europe/Dublin")).format(formatter));

Example output:示例输出:

October 17, 2018 2018 年 10 月 17 日

Here you can see that the month is not in uppercase.在这里您可以看到月份不是大写的。

Tip 2: give explicit locale for your formatter so you know it will also work as expected on computers with a different default locale and if some day you happen to play with the locale setting of your own computer.提示 2:为您的格式化程序提供明确的语言环境,以便您知道它也可以在具有不同默认语言环境的计算机上按预期工作,并且如果有一天您碰巧使用自己计算机的语言环境设置。

To accept month in uppercase接受大写月份

I don't think it was what you wanted, but for the sake of completeness: To allow the month to be entered in all uppercase letters use a DateTimeFormatterBuilder and its parseCaseInsensitive method like this:我认为这不是您想要的,但为了完整起见:要允许以所有大写字母输入月份,请使用DateTimeFormatterBuilder及其parseCaseInsensitive方法,如下所示:

    DateTimeFormatter formatter = new DateTimeFormatterBuilder()
            .parseCaseInsensitive()
            .appendPattern(dateFormat)
            .toFormatter(Locale.forLanguageTag("en-IE"));

With this change here's a sample session:通过此更改,这里有一个示例会话:

Enter a date: FEBRUARY 27, 1999
Date entered was: 1999-02-27
There are19years7months19days

The date is output in ISO 8601 format for a calendar date.日期以 ISO 8601 格式输出为日历日期。 And I believe the calculation of the period is correct.我相信这个时期的计算是正确的。

Link关联

DateTimeFormatter documentation where you can see the uppercase and lowercase pattern letters and study the difference between m and M , d and D and y and Y . DateTimeFormatter文档,您可以在其中查看大写和小写模式字母并研究mMdD以及yY之间的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM