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

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

问题描述

问题:

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

解决方案:

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

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

 public static void main(String[] args) 抛出 ParseException {String dateFormat="yyyy-MM-dd HH:mm:ss";字符串 s2=addDate(dateFormat);convertStringToDate(s2,dateFormat);}public static Date convertStringToDate(String dateInStr, String dateFormat) 抛出 ParseException{FastDateFormat fdf=FastDateFormat.getInstance(dateFormat);//("yyyy-MM-dd HH:mm:ss");日期日期=空;日期 = fdf.parse(dateInStr);System.out.println("From convertStringToDate ");System.out.println(日期);归期;}公共静态字符串 addDate(String dateFormat) 抛出 ParseException{FastDateFormat fdf=FastDateFormat.getInstance(dateFormat);日历 c = Calendar.getInstance();c.add(Calendar.DATE,1);字符串 s1=fdf.format(c.getTime());System.out.println("从添加日期");System.out.println(s1);返回 s1;}

convertStringToDate 的预期输出:

2017-04-21 17:01:31

从 convertStringToDate 显示的输出:

星期五 4 月 21 日 17:01:31 IST 2017

<块引用>

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

解决方案

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

fdf.format(c.getTime());返回 fdf.parse(fdf.format(c.getTime()));

format 方法返回格式化为字符串的日期.在第一行中,您调用 format 但您不对返回值执行任何操作.所以,实际上,这条线什么都不做.

在第二行中,您首先将日期格式化为字符串,然后立即再次将字符串解析为 Date 对象.这也有效地什么都不做.你也可以写return c.getTime();.

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

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

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

Problem:

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

Solution:

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

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;
        }

Expected Output from convertStringToDate:

2017-04-21 17:01:31

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()));

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.

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();.

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.

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 将字符串解析为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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