cancel
Showing results for 
Search instead for 
Did you mean: 

Convert String to Date

Former Member
0 Kudos

Hi

I have a date value returning from Xi thats of the type String, and I need to convert that to type Date so I can populate an input field of type Date(for the purpose of having a date picker in that input field).

How to convert? Request you to give an example.

Thanks

jack

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Jack,

I m afraid that this scenario won't work.

U cannot convert a date value of type String to type date and then store it in the input box.

Only option is u can have the input box type to string and then store the value.

Regards,

Nagarajan.

Former Member
0 Kudos

That's not correct. The "value" property of InputField can be bound to any simple DDIC type like "integer", "string", "date" etc.

The conversion between external and internal format is done by Web Dynpro automatically.

Armin

Former Member
0 Kudos

Hi

I found a way to do it. It is possible to convert string to date. You would have to use DateFormat class for it.

Did it this way:

String reqdate = "<somedatestring>"

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

Date rdate = dateFormat.parse(reqdate);

jack

Former Member
0 Kudos

I haven't tried binding a java.util.Date to an input box, maybe it works. I don't have NWDS installed on this computer so I can't easily test.

If it doesn't work you can convert rdate to java.sql.Date and bind the new sql-date to the input box.

java.sql.Date sqlDate = new sqlDate( rdate.getTime()); 

Pleasy correct me if I'm wrong, since I can't test it.

Former Member
0 Kudos

First, let's be a little bit pedantic and correct the terminology: You do not "bind a Date to an input box", but you bind the "value" property of an input field to a context attribute of (dictionary) type "date".

You cannot bind the "value" property to an attribute of Java native type java.util.Date. Generally, all data binding must be done to dictionary types.

The runtime equivalent to dictionary type "date" is java.sql.Date and not java.util.Date.

To convert types, e.g. from java.util.Date to java.sql.Date,, calculated attributes can be useful.

Armin

Former Member
0 Kudos

Hi Jack,

Split the individual parts of the date into Day, Month and Year. Create a Calendar Object, set each part and from this create a java.sql.Date Object.

Here's some test code

String d = "31";
String m = "06";
String y = "2005";
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(d));
c.set(Calendar.MONTH, Integer.parseInt(m));
c.set(Calendar.YEAR, Integer.parseInt(y));
Date da = new Date(c.getTimeInMillis());

Remember that months in Java start at 0 so "06" in the example there is July.

Regards,

Patrick.