cancel
Showing results for 
Search instead for 
Did you mean: 

Binding String value to date field.

Former Member
0 Kudos

Hi All,

I have a string which I am getting after adding one day to it.I want to set that value to an inputfield of type Date.

This is the string I am having.

mnth "/"calendar.get(Calendar.DATE)"/"calendar.get(Calendar.YEAR);

The ouput I am getting is 3/11/2008.

I am unable to bind this value to the inputfield.Kindly tell me is it possible to do it.

Thank You,

Praveen

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

use as follows


int mnth = 1;
		String dt = mnth +"/" + calendar.get(Calendar.DATE) +"/"+calendar.get(Calendar.YEAR);
//Above is what you have		

Calendar calendar1 = Calendar.getInstance();
		calendar.set(Calendar.MONTH, mnth-1);
		
		Date dt = new Date(calendar1.getTimeInMillis());
//Bind dt to respective field.

Regards

Ayyapparaj

Answers (9)

Answers (9)

Former Member
0 Kudos

Hi,

String dateString ="3/11/2009"
Date today = df.parse(dateString);
java.sql.Date datepass = new java.sql.Date(today.getTime());
wdContext.currentContextElement().setdateinput(datepass );

Define this in declaration space in code.

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

Hope this may help u.

Regards,

H.V.Swathi

Edited by: H.V Swathi on Mar 11, 2009 8:11 AM

Edited by: H.V Swathi on Mar 11, 2009 8:11 AM

Former Member
0 Kudos

Hi,

Suppose you get the below string

String sDate = 11/3/2009;

You can set it into a field of type Date as below

Date date = null;

wdContext.currentContextElement.set<Context>(date.valueOf(sDate));

I hope this works

Former Member
0 Kudos

Hi Praveen,

In order to convert the string to date use below code it worked for me.

I think it may help you.

Suppose you have a string as String str = 03/11/2009

03-month

11-date

2009-Year

Calendar c= Calendar.getInstance();

c.set(Integer.parseInt(str.substring(6,10)),Integer.parseInt(str.substring(0,2))-1,Integer.parseInt(str.substring(3,5)));

java.sql.Date d = new java.sql.Date((c.getTime()).getTime());

now you can bind this d directly to the input field.

Thanks,

Kiranmai.S

nikhil_bose
Active Contributor
0 Kudos

hello surya,

If the context attribute is of Date type, why don't you directly go for getting date from the Calendar which you already have. Please refer Ayyapparaj's reply.


                int mnth = 1; // you have some month value in variable mnth
		String str1 = mnth +"/" + calendar.get(Calendar.DATE) +"/"+calendar.get(Calendar.YEAR);
                // this is what you are trying to do.	
 

Instead of getting date converted to string, first go for incrementing/setting desired month in the Calendar object and then convert it to the format you want.


    int mnth = 1;   // mnth variable holds the new month - already have
    calendar.set(Calendar.MONTH, mnth-1);   // calendar you already have
    Date dt = new Date(calendar.getTimeInMillis());   // date with month fixed.

for displaying it in specified format, you can use [SimpleDateFormat|http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html]

 
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
String datestring = df.format( dt);

nikhil

Former Member
0 Kudos

Hi

Code that convert string to java.sql.Date format..



Calendar cal = Calendar.getInstance();
    cal.set( 2008, 03, 11);
    
    String date_str = cal.get( Calendar.MONTH) + "/" +
					  cal.get( Calendar.DATE) + "/" + 
					  cal.get( Calendar.YEAR);
	
	try {
		long date_lng = new SimpleDateFormat( "mm/dd/yyyy").parse( date_str).getTime();
		wdContext.currentContextElement().setMydate( new Date( date_lng));
	} catch (ParseException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} 	

vinod

Former Member
0 Kudos

Hi Anup,

Still i am unable to get the correct output.I am sending you the code that I have written :

String a = mnth "/"calendar.get(Calendar.DATE)"/"calendar.get(Calendar.YEAR);

String strFormat =a;

DateFormat myDateFormat = new SimpleDateFormat(strFormat);

java.util.Date myDate = null;

try

{

myDate = myDateFormat.parse(strFormat);

}

catch (ParseException e)

{

wdComponentAPI.getMessageManager().reportSuccess("Invalid Date Parser Exception ");

e.printStackTrace();

}

wdComponentAPI.getMessageManager().reportSuccess("This is the Date " + myDate);

wdComponentAPI.getMessageManager().reportSuccess("Finished Date Function " + myDate.getMonth());

Former Member
0 Kudos

Hi,

Method of SimpleDate Format Class parse(String text, ParsePosition pos) - Parses text from a string to produce a Date.

Did you try giving parse position as 0 ? i.e,

myDate = myDateFormat.parse(strFormat, 0);

Regards,

Anagha

former_member192434
Active Contributor
0 Kudos

Hi

I just try this code is running check it out

//@@begin wdDoInit()

Calendar cl = new GregorianCalendar();

String a = cl.get(Calendar.DAY_OF_MONTH)"/"cl.get(Calendar.DATE)"/"cl.get(Calendar.YEAR);

//String a = "/1217/2008";

wdComponentAPI.getMessageManager().reportSuccess("a======= " + a);

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd");

try {

java.util.Date convertedDate = dateFormat.parse(a);

wdComponentAPI.getMessageManager().reportSuccess("convertedDate======= " + convertedDate);

}

catch(Exception e){

e.printStackTrace();

}

//@@end

Thanks

Former Member
0 Kudos

>

> String a = mnth "/"calendar.get(Calendar.DATE)"/"calendar.get(Calendar.YEAR);

> String strFormat =a;

> DateFormat myDateFormat = new SimpleDateFormat(strFormat);

>

Your code:

DateFormat myDateFormat = new SimpleDateFormat(strFormat); // Should you not give a pattern like dd-mm-yyyy here?

For e.g:

String a = mnth "/"calendar.get(Calendar.DATE)"/"calendar.get(Calendar.YEAR);

SimpleDateFormat format1 = new SimpleDateFormat("dd-MM-yyyy");

try

{

Date date1 = format1.parse(a);

}

catch (ParseException e)

{

wdComponentAPI.getMessageManager().reportException("ParseException:"+e,false);

}

Regards,

Anagha

Former Member
0 Kudos

Hi Anup,

I want to change my string into date format.

Thankx,

Praveen

former_member192434
Active Contributor
0 Kudos

Use this

String strFormat =""20/12/2005"";

DateFormat myDateFormat = new SimpleDateFormat(strFormat);

Date myDate = null;

try {

myDate = myDateFormat.parse(strDate);

} catch (ParseException e) {

System.out.println("Invalid Date Parser Exception ");

e.printStackTrace();

}

System.out.println("Finished Date Function " + myDate.getDay());

OR

uese this

http://www.kodejava.org/examples/19.html

thanks

Former Member
0 Kudos

Hi Ayyapparaj KV ,

This is the string value that I am getting .

str = 12/12/2008.

Now I want to set this value in my inputfield which I binded to a date type attribute.Can you please help me on this.

Thankx,

Praveen.

Former Member
0 Kudos

Hi Praveen,

Alternatively you can parse the date from your string. The output will be of Date type then you can map it to the context attribute of type date.

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization.

Refer: http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html

Regards,

Anagha

Former Member
0 Kudos

Hi ,

Actually this my string :

String str1=mnth "/"calendar.get(Calendar.DATE)"/"calendar.get(Calendar.YEAR);

I want to bind it to a date field.I created an attribute SDate of type date and binded it to an inputfield.

wdContext.currentcontextelement.setSDate(str1);

Can you tell me how to bind this string to this attribute.

Thankx,

Praveen

former_member192434
Active Contributor
0 Kudos

HI

Use this sample code to convert Date to String and then create a attribute with String type and bind it with attribute

public static void main(String[] args) {

try {

DateFormat formatter ;

Date date ;

formatter = new SimpleDateFormat("dd-MMM-yy");

date = (Date)formatter.parse("11-June-07");

String s = formatter.format(date);

} catch (ParseException e)

{ }

}

}

thanks