0

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?

Jason arora
  • 550
  • 3
  • 8
  • 22
  • Where do you print the Date ? Since you are return a `Date` instance, there is no format here. Print `fdf.format(c.getTime());` to see the correct output – AxelH Apr 20 '17 at 08:09
  • 2
    Your requirements do not make sense, sorry. If you need to return the date in a specific format (for example, `2017-04-21 13:16:13`), then you need to return a `String`, you cannot have a specific format with a `Date`. Maybe if you told us why you want that, we could guide you in some useful direction. – Ole V.V. Apr 20 '17 at 09:28
  • 1
    Even though `FastDateFormat` seems to have som advantages over `SimpleDateFormat`, in 2017 I recommend using the newer Java date and time classes, including `DateTimeFormatter`, which is thread-safe too. – Ole V.V. Apr 20 '17 at 09:31
  • @OleV.V. Exactly he can not have a specific format with a `Date` object. – Oghli Apr 20 '17 at 09:32
  • you can not return Date class Object in specific format `yyyy-MM-dd HH:mm:ss` you have to use `format` method which will return string representation of this format so return type of your function should be string. – Oghli Apr 20 '17 at 09:39
  • You have your answer. A `Date` is an object that hold the information, but **doesn't format it**. You need to generate a `String` to format the instance like you want, you are using `FastDateFormat` for this but you can't ask a `Date` instance to remember the format after that. So `fgf.format(d1)`) is what you want (after you have build the `FastDateFormat`). And you can get rid of that `parse(format(c.getTime)`, `c.getTime` is enough – AxelH Apr 20 '17 at 10:01
  • BTW, with a `FastDateFormat` you don’t need `fdf.format(c.getTime())`, you may use the simpler `fdf.format(c)`. There is [an overloaded `format` method](https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/time/FastDateFormat.html#format-java.util.Calendar-) that accepts a `Calendar` argument. – Ole V.V. Apr 20 '17 at 12:09
  • @OleV.V. Okay Please Refer the below question for more Clarity. – Jason arora Apr 20 '17 at 12:14
  • http://stackoverflow.com/questions/43518495/unexpected-output-while-trying-yo-convert-string-to-date-using-fastdateformat/43518788#43518788 – Jason arora Apr 20 '17 at 12:14

4 Answers4

2

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);
Jesper
  • 202,709
  • 46
  • 318
  • 350
0

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()));
Naga Pavan
  • 26
  • 2
0

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:

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;
}
Oghli
  • 2,200
  • 1
  • 15
  • 37
0

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

check this out for further Reference - Date Formatting

When you create a SimpleDateFormat object, you specify a pattern 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. The String returned by the format method contains the formatted date and time that are to be displayed.

-from Oracle Docs.
Kaiizok
  • 90
  • 10