-1

Problem:

I need to pass Date class Object to a function and that Date Object should contain one Day ahead of the System Time. For Ex: If Today's Date is 2017-04-20 17:01:31.Then,Date Object should contain 2017-04-21 17:01:31

Is it possible to store a specified format into Date Class Object and pass into it.

I tried the following thing and it didn't work.

Can anyone guide me if it is possible through Code or should I use SQL Query Concept to add a Day.

Below is my Code

     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
Jason arora
  • 550
  • 3
  • 8
  • 22
  • I don't understand your question. What is the actual output, what is the expected output? –  Apr 20 '17 at 11:43
  • 1
    "Is it possible to store a specified format into Date Class Object" <- The date class does not have a format, it just contains the data to represent the date. How you format that data when you display it in a GUI is another thing. – OH GOD SPIDERS Apr 20 '17 at 11:43
  • @RC.Edited the code – Jason arora Apr 20 '17 at 11:44
  • @RC. Yes But IT Should be in Date Class – Jason arora Apr 20 '17 at 11:45
  • 2
    No, the format used when a Date is printed via Date#toString is not yours to choose, if you really need that, then use some wrapper around date and a proper Wrapper#toString –  Apr 20 '17 at 11:47
  • @RC. Can explain with implementation please – Jason arora Apr 20 '17 at 12:00
  • Possible duplicate of [How to parse String to Date using FastDateFormat in a specified format](http://stackoverflow.com/questions/43513600/how-to-parse-string-to-date-using-fastdateformat-in-a-specified-format) – Ole V.V. Apr 20 '17 at 12:35
  • 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 20:38

1 Answers1

0

You need to obtain the instance of class Date. In your method addDate you get the required Date and then convert it to String and return a String. And then you convert your String back to Date Why don't you just return Date from your method addDate and be done with it?

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36