如何使用FastDateFormat以指定格式将字符串解析为Date [英] How to parse String to Date using FastDateFormat in a specified format

查看:281
本文介绍了如何使用FastDateFormat以指定格式将字符串解析为Date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

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

I need to add specified number of Days in a given Date. The function should return Date class Object and should use FastDateFormat.

解决方案:

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

I wrote the Code but I need the Output in yyyy-MM-dd HH:mm:ss.

当我将Input传递给此函数

When I pass Input to this function

 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的预期输出:

Expected Output from convertStringToDate:

2017-04-21 17:01:31

2017-04-21 17:01:31

convertStringToDate的OutputShown:

OutputShown from convertStringToDate:

Fri Apr 21 17:01:31 IST 2017

任何人都可以指导我如何解决上述问题?

Can anyone guide me how should I solve the above problem?

推荐答案

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

The last two lines of your method do not make sense:

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

format方法返回日期格式为字符串的日期.在第一行中,您调用format,但是对返回值不做任何事情.因此,有效地,此行不执行任何操作.

The format method returns your date formatted as a string. In the first line, you call format but you don't do anything with the return value. So, effectively, this line does nothing.

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

In the second line, you first format the date to a string, and then you immediately parse the string again into a Date object. That also effectively does nothing. You could just as well have written return c.getTime();.

请注意,Date对象本身没有格式.您不能有Date对象以特定格式自动打印自身,例如yyyy-MM-dd HH:mm:ss,因为Date对象本身对格式化本身一无所知.

Note that Date objects do not have a format by themselves. You cannot have a Date object that prints itself automatically in a specific format, such as yyyy-MM-dd HH:mm:ss, because the Date object itself does not know anything about formatting itself.

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

Instead, you should use the FastDateFormat object to format the Date object to a String, and then you print that string. For example:

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

这篇关于如何使用FastDateFormat以指定格式将字符串解析为Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆