简体   繁体   English

如何使用FastDateFormat以指定格式将字符串解析为Date

[英]How to parse String to Date using FastDateFormat in a specified format

Problem: 问题:

I need to add specified number of Days in a given Date. 我需要在给定的日期中添加指定的天数。 The function should return Date class Object and should use FastDateFormat. 该函数应返回Date类Object,并应使用FastDateFormat。

Solution: 解:

I wrote the Code but I need the Output in yyyy-MM-dd HH:mm:ss. 我写了代码,但我需要yyyy-MM-dd HH:mm:ss中的输出。

When I pass Input to this function 当我将Input传递给此功能时

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

2017-04-21 17:01:31 2017-04-21 17:01:31

OutputShown from convertStringToDate: 来自convertStringToDate的OutputShown:

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. format方法返回日期格式为字符串的日期。 In the first line, you call format but you don't do anything with the return value. 在第一行中,您调用format但是对返回值不做任何事情。 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. 在第二行中,首先将日期格式化为字符串,然后立即将字符串再次解析为Date对象。 That also effectively does nothing. 这实际上也无能为力。 You could just as well have written return c.getTime(); 您也可以编写return c.getTime(); .

Note that Date objects do not have a format by themselves. 请注意, Date对象本身没有格式。 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. 您不能拥有以特定格式自动打印自身的Date对象,例如yyyy-MM-dd HH:mm:ss ,因为Date对象本身对格式化本身一无所知。

Instead, you should use the FastDateFormat object to format the Date object to a String , and then you print that string. 相反,应该使用FastDateFormat对象将Date对象格式化为String ,然后打印该字符串。 For example: 例如:

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

The below lines of code will print the date and time in that format you required. 以下代码行将以您需要的格式打印日期和时间。

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

your problem in these lines of code: 这些代码行中的问题:

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

your goal is simple to achieve you only need to use format() method which will Formats a Date into a date/time string: 您的目标很容易实现,您只需要使用format()方法即可将Date格式化为日期/时间字符串:

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

solution will be : 解决方案将是:

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

when you are returning, you are returning a date Object and you can format the Date Object but then you would get the output as a String. 当您返回时,您将返回一个日期对象,您可以设置日期对象的格式,但随后您将获得字符串形式的输出。 The Format of the Date Object would be constant. 日期对象的格式将是恒定的。

When using the FastDateFormat- fdf.format(c.getTime()) . 使用FastDateFormat- fdf.format(c.getTime())时 This part will give you the format of the Date as a String and in the required format that you need, but when you try to parse it and convert it into Date Object, it would have the previous Constant format. 这部分将为您提供日期格式的字符串形式和所需的所需格式,但是当您尝试解析它并将其转换为日期对象时,它将具有以前的常量格式。

You can use the FastDateFormat to get the formatted output of a Date as a string,Not formatting the Date Object. 您可以使用FastDateFormat以字符串形式获取Date的格式化输出,而不格式化Date对象。

check this out for further Reference - Date Formatting 请查看此内容以获取更多参考- 日期格式

When you create a SimpleDateFormat object, you specify a pattern String. 创建SimpleDateFormat对象时,可以指定模式String。 The contents of the pattern String determine the format of the date and time. 模式字符串的内容确定日期和时间的格式。

The code formats a date and time according to the pattern String passed to the SimpleDateFormat constructor. 该代码根据传递给SimpleDateFormat构造函数的模式String格式化日期和时间。 The String returned by the format method contains the formatted date and time that are to be displayed. format方法返回的字符串包含要显示的格式化日期和时间。

-from Oracle Docs.

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

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