简体   繁体   English

Java Date to String解析/验证需要帮助

[英]Java Date to String parsing / validation help needed

I'm having trouble changing my date field in java to a string. 我在将java中的日期字段更改为字符串时遇到麻烦。 It works in my output toString but the validation piece doesn't recognize. 它可以在我的输出toString中工作,但验证程序无法识别。 What is wrong with my code? 我的代码有什么问题? I cannot have users posting anything but a true "M/d/yyyy" date but my validation isn't working. 除了真正的“ M / d / yyyy”日期,我无法让用户发布任何东西,但我的验证无法正常工作。 Thoughts? 有什么想法吗?

I've posted the start date code below, the "end date" code is the same but thought you should know it is present. 我在下面发布了开始日期代码,“结束日期”代码相同,但是您应该知道它已经存在。

Many thanks in advance. 提前谢谢了。

        String stringStartDate = this.txtStartDate.getText();
        SimpleDateFormat formatter = new SimpleDateFormat("M/d/yyyy");
        Date pStartDate = null;

        if (stringStartDate.length() > 0)
        {
            try {
                pStartDate = formatter.parse(stringStartDate);
            } catch (ParseException e1) {
                e1.printStackTrace();
            }

          if (pStartDate == null)
          {
            this.textArea.setText(this.textArea.getText() + "\n"
              + "Error! Please enter a valid start date in the format M/d/yyyy (e.g. 1/2/2015)!");
            return;
          }
        }
        else
        {
          this.textArea.setText(this.textArea.getText() + "\n"
            + "Error! Please enter a valid start date in the format M/d/yyyy (e.g. 1/2/2015)!");
          return;
        }

Here is my toString()... this is working perfectly. 这是我的toString()...这很正常。

    @Override 
    public String toString(){
    return "Pay Period Start Date = " +        simpleDateFormatMonthDayYear.format(this.pStartDate) + "\n" 
            + "Pay Period End Date = " + simpleDateFormatMonthDayYear.format(this.pEndDate) + "\n";

    }

From the comments and my own trial and error I've change a few things around but now neither work. 从评论和我自己的尝试和错误中,我改变了一些内容,但现在都无效。 What is the deal? 怎么了

        SimpleDateFormat simpleDateFormatMonthDayYear = new SimpleDateFormat("M/d/yyyy");
        String stringStartDate = simpleDateFormatMonthDayYear.format(this.txtStartDate.getText());
        Date pStartDate = null;

        if (stringStartDate.length() > 0)
        {
            try {
                pStartDate = simpleDateFormatMonthDayYear.parse(stringStartDate);
            } catch (ParseException e1) {
                e1.printStackTrace();
            }

          if (pStartDate == null)
          {
            this.textArea.setText(this.textArea.getText() + "\n"
              + "Error! Please enter a valid start date in the format M/d/yyyy (e.g. 1/2/2015)!");
            return;
          }
        }
        else
        {
          this.textArea.setText(this.textArea.getText() + "\n"
            + "Error! Please enter a valid start date in the format M/d/yyyy (e.g. 1/2/2015)!");
          return;
        }

You are trying to use a SimpleDateFormat to format a String ... 您正在尝试使用SimpleDateFormat格式化String ...

SimpleDateFormat simpleDateFormatMonthDayYear = new SimpleDateFormat("M/d/yyyy");
String stringStartDate = simpleDateFormatMonthDayYear.format(field.getText());

which simply doesn't make sense. 这根本没有道理。 You want to parse the String value to a Date 您想将StringparseDate

The first thing you want to do is parse the String value to a Date , this will check to see if the String is in the correct format. 您要做的第一件事是将StringparseDate ,这将检查String的格式是否正确。 Once you've done that, you want to check to see if the formatted version of the parsed Date is equal to the text in the field, to see if the date is actually valid... 完成此操作后,您要检查解析后的Date的格式化版本是否等于字段中的文本,以查看日期是否实际有效...

SimpleDateFormat simpleDateFormatMonthDayYear = new SimpleDateFormat("M/d/yyyy");
try {
    Date date = simpleDateFormatMonthDayYear.parse(field.getText());

    if (date != null) {

        String formatted = simpleDateFormatMonthDayYear.format(date);
        if (!formatted.equals(field.getText())) {

            System.out.println("Date is not valid");

        }

    } else {

        System.out.println("Error! Please enter a valid start date in the format M/d/yyyy (e.g. 1/2/2015)!");

    }
} catch (ParseException exp) {

    System.out.println("Error! Please enter a valid start date in the format M/d/yyyy (e.g. 1/2/2015)!");

}

But I'd just use JSpinner or JFormattedTextField as these can auto valid the text themselves, but that's just me (or JXDatePicker from SwingLabs SwingX library, because I'm just that lazy) 但是我只是使用JSpinnerJFormattedTextField因为它们可以自动验证文本本身,但这就是我自己(或SwingLabs SwingX库中的JXDatePicker ,因为我很懒)

You did not tell us what tests the method is failing on. 您没有告诉我们该方法失败了哪些测试。 My usual method of finding errors in code like this is to test, find a test input that fails, step through it with a debugger and see where it is failing. 在这样的代码中查找错误的常用方法是进行测试,查找失败的测试输入,使用调试器逐步检查并查看失败的地方。

But, here is a hint. 但是,这里有一个提示。 I note that you are using "M/d/yyyy" for a format. 我注意到您正在使用“ M / d / yyyy”作为格式。 This is problematic because some months have two digits, and some days have two digits. 这是有问题的,因为有些月份的数字是两位数,有些日子的数字是两位数。 Why not use "MM/dd/YYYY"? 为什么不使用“ MM / dd / YYYY”?

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

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