on 07-06-2009 5:33 AM
Hi ,
i have a table with data and in one column i have dates.I have 2 dates in row in dates column(yyyy-mm--dd) format .How to change these two dates in dd/mm/yyyy and some rows i have only single date.
_____________
2009-11-09
2008-12-01
____________
With Regards,
suresh
Hi Suresh,
Create a simple data type of type java.sql.date and give the format in it as "dd/MM/yyyy". Please revert back incase you need any further information on this.
Thanks and Regards,
Pravesh
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Suresh,
Try the first approach which I have told you. If that does not work try this code once again.
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String s=df.format(new Date(2009,11,9)); // I am passing the date in the format function in Year-Month-Date format.
This should work, its working on my system. If this does not work please revert back.
Also please note that you are confusing with my name. Its Pravesh not Praveen.
Thanks and Regards
Pravesh
Hi Suresh,
If this column is showing date in "yyyy-mm-dd" format and of type String data type then use the following code to change to your required format.
Pass your required format to dateFormat.
public static String convertStringDateToRequiredFormat(String strDate,String dateFormat)
{
/**
* The expected input date is yyyy-MM-dd MM/dd/yyyy.
* If input is not in expected format, strDate will be returned back.
*/
try{
String dtArray[] = strDate.split("-");
int month = Integer.parseInt(dtArray[1]);
int day = Integer.parseInt(dtArray[2]);
int year = Integer.parseInt(dtArray[0]);
Calendar cl = Calendar.getInstance();
cl.set(Calendar.MONTH,--month);
cl.set(Calendar.DAY_OF_MONTH,day);
cl.set(Calendar.YEAR,year);
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
String dt = sdf.format(cl.getTime());
return dt;
}catch(Exception e){
/**
* In case of any exception just returning given string
*/
return strDate;
}
}
Regards,
Jaya.
Hi Suresh,
Its quite strange that its not working. It should ideally work.. OK instead of date format try to use this method.
1) Create one method String getFormatedDate(String Date). Go to methods tab of the view to create this method. Give the method name as getFormatedDate with return type String. Add the parameter to this method of type String with name Date.
2) Write this code where you are getting the dates in format (yyyy-mm-dd).
Stirng dt = care.getDates();
String date = getFormatedDate(dt); // This will be your required date in format dd/mm/yyyy
3) In the implementation write this code:
public String getFormatedDate(String date){
String year = date.split("-")[0];
String month = date.split("-")[1];
String day = date.split("-")[2];
String finalDate = day+"/" +month+"/"+ year;
return finalDate ;
}
I am sure this will work atlease. However this is very unusual way of getting the formatted date but I really dont know why the dateFormatter is not working for you..
Please revert back incase you could not solve this issue using this approach. And thanks for correcting the name
Thanks and Regards,
Pravesh
Hi Suresh,
Create one more value attribute under the node (Which you bind to a table). Chagne the property of calculated attribute to true. Two methods (getter and setter) will be generated for this.
Copy paste the below code in the newly generated getter method.
try{
String strDate = element.getXXXDate();
String dateFormat = "dd/MM/yyyy";
// element is one of the input parameter of your getter method which represents the current element of the node.
// I am assuming that the column name which is holding the date in "yyyy-mm-dd" is XXXDate. Change the code to suit your attribute.
String dtArray[] = strDate.split("-");
int month = Integer.parseInt(dtArray[1]);
int day = Integer.parseInt(dtArray[2]);
int year = Integer.parseInt(dtArray[0]);
Calendar cl = Calendar.getInstance();
cl.set(Calendar.MONTH,--month);
cl.set(Calendar.DAY_OF_MONTH,day);
cl.set(Calendar.YEAR,year);
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
String dt = sdf.format(cl.getTime());
return dt;
}catch(Exception e){
/**
* In case of any exception just returning given string
*/
return strDate;
}
Now, instead of binding the old column to the table, bind the new column (Calculated attribute) to the table. It will show the date in the required format.
Hi Suresh,
This can be possible only if the value passed as date to this functionis null or empty. Please add a null value and a empty value check.
Try this code:
public String getFormatedDate(String date){
String finalDate = "";
if(date!=null && !date.equalIgnoreCase("")){
String year = date.split("-")[0];
String month = date.split("-")[1];
String day = date.split("-")[2];
finalDate = day+"/" +month+"/"+ year;
}
return finalDate ;
}
Please note that if null OR a empty string is passed to this function then this function will just return "". (An empty string)
I hope this solves the issue.
Thanks and Regards,
Pravesh
HI Pravesh,
Thank you. Its working fine but when i have 2 dates in one row at that time the dates are not showing perfectly.Actually my issue is that only when i have single date i am able to change the date format through this code
NDD = care.getNextDD().trim().toString();
NDD = NDD.substring(8)"/"NDD.substring(5,7)"/"NDD.substring(0,4);
But in my rows sometime it will have 2 or 3 dates in dates column.I need to change the entire column in this format not only if i have only 1 date.
Regards,
Suresh
Hi Suresh,
Uff!!! Finally.. you got the solution.. I would say that just create a function and everytime you are fetching the date just pass it to this function. If you are getting multiple dates then you have to write your logic in such a way that it could be passed to this function seperately and the function can return you the exact value.
There is no other way out. If your question is answered I guess probably you can give the points to the suggestion or close the thread.
If you have any further issues please revert back.
Thanks and Regards,
Pravesh
Hi Suresh,
This is purely a JAVA code and your technique of how you could distinguish between the multiple dates coming from the RFC element. probably I could just give you a hint.
Let us assume that you are getting the multiples dates seperated by a comma (","). Then the logic should be like:
String NDD = care.getNextDD().trim().toString();
String dates[] = NDD.split(",");
String tempDate= "";
String finalDate = "";
if(date.length()>0){
tempDate = getFormatedDate(dates[0]);
finalDate = finalDate + tempDate;
}
then you could pass each string to the method where you will get the output as the string.
Also you function getFormatedDate should look like this:
public String getFormatedDate(String date){
String finalDate = "";
if(date!=null && !date.equalsIgnoreCase("")){
finalDate = date.substring(8)+"/"date.substring(5,7)"/"+date.substring(0,4);
}
return finalDate ;
}
I hope this could help you to solve the issue. However, you need to decide upon the exact logic which you need to implement in this case.
Thanks and Regards,
Pravesh
User | Count |
---|---|
84 | |
10 | |
10 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.