cancel
Showing results for 
Search instead for 
Did you mean: 

String to date conversion error

Former Member
0 Kudos

Hello Experts,

This is Fayaz.

Let me explain about the issue that i am facing .

In the output table i have a  column which displays the date  but the attribute type is String(On screen output : 20/09/2012).Because of string type sorting is not properly done(Done based on the string value not on the basis of date)

Can you please correct me where i did the  mistake

Below code :

SimpleDateFormat df=new SimpleDateFormat("dd/MM/yyyy");

java.util.Date date;

try{

date d=df.parse(string);   //String returns date

ele.setDate_finc(df.format(d));      (Getting exception in this line)  //Date_finc is of type Date 

// i want same format after converting the string to date(i.e 20/09/2012)

}

catch(ParseException e)

{ }

}

Please let me know is there any alternative way for applying the filter without changing the attribute type from string to date

Can you please help me?

Thanks in Advance,

Regards

Fayaz Shaik

Accepted Solutions (0)

Answers (3)

Answers (3)

amol_gaddamwar
Explorer
0 Kudos

Hi,

In your code the line which is giving the error, you are using df.format() method which returns String instead Date which you are trying to set in the context attribute Date_finc which is of type Date.

You cannot assign String value to a Date attribute.

You have to set attribute as ele.setDate_finc(d);

Also for the display format of the data datatype, it will depend on the user configuration. You cannot change the display format of date with this code.

Regards,

Amol

Qualiture
Active Contributor
0 Kudos

You haven't assigned your variable 'date' a value yet, other than that your code seems correct

vijay_kumar49
Active Contributor
0 Kudos

     Fayaz,

        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

        String dateInString = "7/12/2013";

        try {

               Date date = formatter.parse(dateInString);

               System.out.println(date);

               System.out.println(formatter.format(date));

        } catch (ParseException e) {

               e.printStackTrace();

        }


or


         String startDateString = "20/06/2013";

        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

         Date startDate;

    try {

        startDate = df.parse(startDateString);

        String newDateString = df.format(startDate);

        System.out.println(newDateString);

    }

     catch (ParseException e)

     {

        e.printStackTrace();

    }

Please try above code. Hope this is helpful !!!


Regards

Vijay K