cancel
Showing results for 
Search instead for 
Did you mean: 

Date and Number formatting

Former Member
0 Kudos

Hi All

I am getting data from RFC and able to bind it into table. Now I want to format date and numbers into user logged in format. i.e. If user enters country as Germany while registration,date format should be European in table displayed otherwise it should be US format.

Can any one suggest me how to do it?

Accepted Solutions (0)

Answers (1)

Answers (1)

former_member182294
Active Contributor
0 Kudos

Hi Radhakrishna,

You need to do this ways:

- Create a new Attribute in the main node and assign the formatted value to new attibute.

//Assuming sap date is available in context attribute mysapdate;

Date sapDate = wdContext.current<nodeName>Element.getMysapdate();

IUser currentUser = WDClientUser.getCurrentUser().getSAPUser();

Locale userLocale = currentUser.getLocale();

DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, userLocale);

String newDateString = dateFormat.format(sapDate);

wdContext.current<nodeName>Element.setNewFormatDate(newDateString);

Regards

Abhilash

Former Member
0 Kudos

My code is given below. I am getting number format exception. My date format displayed in table is mm.dd.yyyy. In R3 date format is different and displaye ddate format is different . Can any one tell why they are different?


//Parsing String to date format
SimpleDateFormat sdf = new SimpleDateFormat("mm.dd.yyyy");

	Date sqlDate =sdf.parse(ele.getSched_Date());

//Getting User Locale

	IUser currentUser = WDClientUser.getCurrentUser().getSAPUser();
						Locale userLocale = currentUser.getLocale();
						
						
						DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, userLocale);
						String newDateString = dateFormat.format(sqlDate);

//binding date string to context 
ele3.setPlannedReceiptsDate(newDateString);	


former_member182294
Active Contributor
0 Kudos

Couple of questions for you:

As per the below example I am sure ele.getSched_Date() is returning the String. As per my understanding if the data type is Date in RFC it should be SQL Date in WebDynpro data dictionary. So my doubt is are converting the date object to string some where?

Next, SQL date always returns the format MM/dd/yyyy. You can try this format in SimpleDateFormat. And also SimpleDateFormat always returns java.util.Date not java.sql.Date.

Regards

Abhilash

Former Member
0 Kudos

Use "M" (month) instead of "m" (minute).

Armin

Former Member
0 Kudos

Armin

I tried using M instead of m but still I am getting parse expcetion.


 java.text.ParseException: Unparseable date: "09.07.2007" 

Former Member
0 Kudos

When I binded RFC to view controller it was string (not sql date). In above code using simpleDateformatter I converted into date. Next I used String newDateString = dateFormat.format(sqlDate);

for converting date to string again. After conversting into String I binded the value to table.

I used only java.util.Date not java.sql.Date but my naming convention was sqlDate.

former_member182294
Active Contributor
0 Kudos

So from RFC you are receiving the date as String object not date object.

Can you paste your code completely? And also point out where exactly you are getting the exception.

Regards

Abhilash

Former Member
0 Kudos

Hi

You can see my whole code. I am getting expection in try catch field.


IPrivateDetailsView.IReceiptTableElement ele3 =
						wdContext.createReceiptTableElement();

	ele3.setMaterialName(ele.getMattext());
	ele3.setReceiptQuantity(ele.getQuantity());
	ele3.setUnit(ele.getSales_Unit());
try{

//Parsing String to date format
SimpleDateFormat sdf = new SimpleDateFormat("MM.dd.yyyy");
 
 Date sqlDate =sdf.parse(ele.getSched_Date());
 //Getting User Locale
 IUser currentUser = WDClientUser.getCurrentUser().getSAPUser();
Locale userLocale = currentUser.getLocale();
						
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, userLocale);
String newDateString = dateFormat.format(sqlDate);
// binding date string to context 					 ele3.setPlannedReceiptsDate(newDateString);	

}
catch(Exception e)
{
wdThis.wdGetAPI().getComponent().getMessageManager().reportException(
					e.toString(),
					false);}

former_member182294
Active Contributor
0 Kudos

Did you tried this format?

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

If this does not work, I have added few debug statements in the code: Can you tell the output of this?

IPrivateDetailsView.IReceiptTableElement ele3 =

wdContext.createReceiptTableElement();

ele3.setMaterialName(ele.getMattext());

ele3.setReceiptQuantity(ele.getQuantity());

ele3.setUnit(ele.getSales_Unit());

try{

//Parsing String to date format

SimpleDateFormat sdf = new SimpleDateFormat("MM.dd.yyyy");

Date sqlDate =sdf.parse(ele.getSched_Date());

<b>wdThis.wdGetAPI().getComponent().getMessageManager().reportSuccess(" Date converted "+sqlDate);</b>

//Getting User Locale

IUser currentUser = WDClientUser.getCurrentUser().getSAPUser();

Locale userLocale = currentUser.getLocale();

<b>wdThis.wdGetAPI().getComponent().getMessageManager().reportSuccess(" User Current Locale "+userLocale );</b>

DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, userLocale);

<b>wdThis.wdGetAPI().getComponent().getMessageManager().reportSuccess(" Formatting to user locale ");</b>

String newDateString = dateFormat.format(sqlDate);

// binding date string to context ele3.setPlannedReceiptsDate(newDateString);

}

catch(Exception e)

{

wdThis.wdGetAPI().getComponent().getMessageManager().reportException(

e.toString(),

false);}

Regards

Abhilash

Regards

Abhilash

Former Member
0 Kudos

I am not able to see any of these messages. I guess exception is raising at

Date sqlDate =sdf.parse(ele.getSched_Date());

. ele.getSched_Date() is returning String.

former_member182294
Active Contributor
0 Kudos

Can you please confirm if you tried the format "MM/dd/yyyy"? And also please add one more debug statement in the begining of the method:

wdThis.wdGetAPI().getComponent().getMessageManager().reportSuccess(" Source date"+ ele.getSched_Date());

Regards

Abhilash

Former Member
0 Kudos

I tried using this format but still problem is there. I am getting

java.text.ParseException: Unparseable date: "09.07.2007"

former_member182294
Active Contributor
0 Kudos

This is really strange problem. I am able to do the same without any problem.

Final option can you try this:

SimpleDateFormat sdf = new SimpleDateFormat("MM.dd.yyyy");

<b>sdf.setLenient(false);</b>

Date sqlDate =sdf.parse(ele.getSched_Date());

Regards

Abhilash

former_member751941
Active Contributor
0 Kudos

Hi Radhakrishna,

Try this.

String st_Date = ele.getSched_Date().toString();

try {

Date sqlDate = new Date(smpdtFormat.parse(st_Date).getTime());

} catch (ParseException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

//@@begin others

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

//@@end

Regards,

Mithu

Former Member
0 Kudos

Thanks for your your Help Abhilash. I slightly changed code and now its working. I think this is almost same thing that you discussed.You can see code below.

Can you help in working with Number format also? I am getting number format exception. String returned when RFC executed is in 10,000,00. I need to change according to user locale format. I tried doing it in java also. When I remove commas in above string its working fine.


public java.lang.String DisplayingDateFormat( java.lang.String date )
  {
    //@@begin DisplayingDateFormat()
				// Current date Format in BW
					SimpleDateFormat sdf = new SimpleDateFormat("MM.dd.yyyy");
					String ChangedDateFormat=null;
		
try{

//	sdf.setLenient(false);

//Parsing String to Date
	Date sqlDate =sdf.parse(date);
	
//	wdThis.wdGetAPI().getComponent().getMessageManager().reportSuccess(" Source date"+ ele.getSched_Date());
//
//	wdThis.wdGetAPI().getComponent().getMessageManager().reportSuccess(" Date converted "+sqlDate);
//	  Getting User Locale

//Getting Locale Information form Current Session
	Locale userLocale= WDResourceHandler.getCurrentSessionLocale();


	//wdThis.wdGetAPI().getComponent().getMessageManager().reportSuccess(" User Current Locale "+userLocale );
	
//Setting date format ie Short,default etc

	DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, userLocale);
	//wdThis.wdGetAPI().getComponent().getMessageManager().reportSuccess(" Formatting to user locale ");
	
	//Converting Date to String
	String newDateString = dateFormat.format(sqlDate);
	
	//wdThis.wdGetAPI().getComponent().getMessageManager().reportSuccess(" Changed Date Format "+newDateString);
	
	//binding to Table UI element
	
	ChangedDateFormat=newDateString;

}
catch(Exception e)
{
	wdThis.wdGetAPI().getComponent().getMessageManager()
					.reportException(
					e.toString(),
					false);
						   
}

former_member182294
Active Contributor
0 Kudos

Thats cool.. but I did not see much difference in previous code and this code except number style DateFormat.SHORT.

Any ways here is the solution for you number formating:

String input = "10,000,00";

NumberFormat nFormat = NumberFormat.getNumberInstance(Locale.ENGLISH);

//replace locale with dynamic locale

try

{

Number localeNumber = nFormat.parse(input);

int newIntValue = localeNumber.intValue();

String newValue = localeNumber.toString();

}

catch (ParseException e)

{

e.printStackTrace();

}

Regards

Abhilash