简体   繁体   English

如何将字符串格式的日期插入Web表单

[英]How to Insert Date in a String format to Web form

I want to insert a date as a string like 23/8/1956 on a Web form. 我想在Web表单上将日期作为字符串插入,例如23/8/1956。 It should have the format: aug 23 1956 and the day , month, year is in a separate field. 它应采用以下格式:1956年8月23日,并且日,月,年位于单独的字段中。

I used the following function: 我使用了以下功能:

 public void dateInString() throws ParseException 
 {
     SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
     SimpleDateFormat parser = new SimpleDateFormat("dd/MMM/yyyy");
     String dateString = "31-10-2013";
      try {
        Date date = formatter.parse(dateInString);
        System.out.println(parser.format(date));
     } 
      catch (ParseException e) {
      e.printStackTrace();
     } 
 }

I received an error on the line : Date date = formatter.parse(dateInString); 我在行上收到错误: Date date = formatter.parse(dateInString); It tells me that this is not a variable - why? 它告诉我这不是变量-为什么? And after this function - how I can insert this to the Web ? 在执行此功能之后,如何将其插入Web?

You have 2 problems 你有两个问题

  1. As Lonely Neuron has already pointed out: your variable is called NameString , not NameInString 正如Lonely Neuron指出的那样:您的变量名为NameString ,而不是NameInString
  2. You need to get MMM dd YYYY(aug 23 1956) format as output 您需要获取MMM dd YYYY(aug 23 1956)格式作为输出

So change SimpleDateFormat parser = new SimpleDateFormat("dd/MMM/yyyy"); 因此,更改SimpleDateFormat parser = new SimpleDateFormat("dd/MMM/yyyy"); to SimpleDateFormat parser = new SimpleDateFormat("MMM dd yyyy"); SimpleDateFormat parser = new SimpleDateFormat("MMM dd yyyy");

working code: 工作代码:

public void  dateInString()  
{
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
    SimpleDateFormat parser = new SimpleDateFormat("MMM dd yyyy");
    String dateString = "31-10-2013";
    try {
        Date date = formatter.parse(dateString);
        System.out.println(parser.format(date));
    } 
    catch (ParseException e) {
        e.printStackTrace();
    } 
}

output: Oct 31 2013 输出: 2013年10月31日

With dateInString you are referencing a variable which doesn't exist. 使用dateInString您将引用一个不存在的变量。 Change your declaration to: 将声明更改为:

String dateInString = "31-10-2013";

I would also suggest using a professional IDE like the (free) community version of intelliJ or eclipse . 我也建议使用专业的IDE,例如intelliJeclipse的(免费)社区版本。 That way you should be able to catch such errors easily for yourself 这样,您应该可以自己轻松地捕获此类错误

Check this following code. 检查以下代码。 I think it will get you desired output. 我认为它将为您提供所需的输出。

  public void dateInString() throws ParseException
  {
     SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
     SimpleDateFormat parser = new SimpleDateFormat("dd/MMM/yyyy");
     String dateString = "31-10-2013";
     try {
        Date date = formatter.parse(dateString);
        System.out.println(parser.format(date));
     } 
     catch (ParseException e) {
        e.printStackTrace();
     }
  }

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

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