【问题标题】:How to parse String to Date using FastDateFormat in a specified format如何使用指定格式的 FastDateFormat 将字符串解析为日期
【发布时间】:2017-04-20 19:24:14
【问题描述】:

问题:

我需要在给定的日期中添加指定的天数。 该函数应返回 Date 类 Object 并应使用 FastDateFormat。

解决方案:

我编写了代码,但我需要 yyyy-MM-dd HH:mm:ss 中的输出。

当我将输入传递给这个函数时

 public static void main(String[] args) throws ParseException {
    String dateFormat="yyyy-MM-dd HH:mm:ss";
        String s2=addDate(dateFormat);
        convertStringToDate(s2,dateFormat);

    }
    public static Date convertStringToDate(String dateInStr, String dateFormat) throws ParseException
    {
        FastDateFormat fdf=FastDateFormat.getInstance(dateFormat);//("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        date = fdf.parse(dateInStr);
        System.out.println("From convertStringToDate ");
        System.out.println(date);
        return date;
    }
public static String addDate(String dateFormat) throws ParseException{
            FastDateFormat fdf=FastDateFormat.getInstance(dateFormat);
            Calendar c = Calendar.getInstance();    
            c.add(Calendar.DATE,1);
            String s1=fdf.format(c.getTime());
            System.out.println("From addDate ");
            System.out.println(s1);
            return s1;
        }

convertStringToDate 的预期输出:

2017-04-21 17:01:31

convertStringToDate 的输出显示:

Fri Apr 21 17:01:31 IST 2017

谁能指导我如何解决上述问题?

【问题讨论】:

  • 你在哪里打印日期?由于您返回 Date 实例,因此这里没有格式。打印fdf.format(c.getTime()); 以查看正确的输出
  • 您的要求没有意义,抱歉。如果需要返回特定格式的日期(例如2017-04-21 13:16:13),则需要返回String,不能有Date 的特定格式。也许如果你告诉我们你为什么要这样做,我们可以指导你一些有用的方向。
  • 尽管FastDateFormat 似乎比SimpleDateFormat 有一些优势,但在2017 年,我建议使用更新的Java 日期和时间类,包括DateTimeFormatter,它也是线程安全的。跨度>
  • @OleV.V.确切地说,他不能具有带有Date 对象的特定格式。
  • 您不能以特定格式返回 Date 类对象 yyyy-MM-dd HH:mm:ss 您必须使用 format 方法,该方法将返回此格式的字符串表示,因此您的函数的返回类型应该是字符串。

标签: java date parsing datetime apache-commons


【解决方案1】:

你方法的最后两行没有意义:

fdf.format(c.getTime());
return  fdf.parse(fdf.format(c.getTime()));

format 方法以字符串格式返回您的日期。在第一行中,您调用format,但您没有对返回值做任何事情。所以,实际上,这条线什么都不做。

在第二行中,首先将日期格式化为字符串,然后立即将字符串再次解析为Date 对象。这实际上也无济于事。你也可以写return c.getTime();

请注意,Date 对象本身没有格式。 Date 对象不能以特定格式自动打印自身,例如 yyyy-MM-dd HH:mm:ss,因为 Date 对象本身不知道任何关于格式化自身的事情。

相反,您应该使用FastDateFormat 对象将Date 对象格式化为String,然后打印该字符串。例如:

String text = fdf.format(c.getTime());
System.out.println(text);

【讨论】:

    【解决方案2】:

    以下代码行将以您需要的格式打印日期和时间。

      DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      Calendar cal = Calendar.getInstance();
      cal.add(Calendar.DATE,1);
      System.out.println(dateFormat.format(cal.getTime()));
    

    【讨论】:

    • 要求是:“函数应该返回 Date 类 Object 并且应该使用 FastDateFormat。”您的代码实际上两者都没有。
    • 很抱歉我没有看到第一行(:
    【解决方案3】:

    您在这些代码行中的问题:

    fdf.format(c.getTime());
    return  fdf.parse(fdf.format(c.getTime())); 
    

    您的目标很容易实现,您只需要使用format() 方法将Date 格式化为日期/时间字符串:

    String date = fdf.format(c.getTime());
    

    解决方案是:

    public static String addDate(String dateFormat){
            FastDateFormat fdf=FastDateFormat.getInstance(dateFormat);
            Calendar c = Calendar.getInstance(); 
            c.add(Calendar.DATE,1);  
            String date = fdf.format(c.getTime()); 
            return date;
    }
    

    【讨论】:

      【解决方案4】:

      当您返回时,您将返回一个日期对象,并且您可以格式化日期对象,但随后您将获得作为字符串的输出。日期对象的格式是不变的。

      使用 FastDateFormat- fdf.format(c.getTime()) 时。这部分将为您提供作为字符串的日期格式以及您需要的所需格式,但是当您尝试对其进行解析并将其转换为日期对象时,它将具有以前的常量格式。

      您可以使用 FastDateFormat 将日期的格式化输出作为字符串获取,而不是格式化日期对象。

      查看此内容以获取更多参考 - Date Formatting

      当您创建一个 SimpleDateFormat 对象时,您指定一个模式字符串。模式字符串的内容决定了日期和时间的格式。

      代码根据传递给 SimpleDateFormat 构造函数的模式字符串格式化日期和时间。 format 方法返回的 String 包含要显示的格式化日期和时间。

      -from Oracle Docs.
      

      【讨论】:

        猜你喜欢
        • 2017-02-26
        • 2017-11-21
        • 1970-01-01
        • 2016-05-17
        • 1970-01-01
        • 2010-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多