You need to sign in to do that
Don't have an account?
Shamil
Date Format in Apex and Front End (Visualforce/S-control) - How to make it generic?
Hello,
I have a Controller, or in other words simply an Apex method, that has a method that returns a PageReference object with some querystring parameters:
A date format in Apex is 'yyyy-MM-dd', so I manually convert it to 'MM/dd/yyyy' in order to prepopulate a 'close date' field (opp9) on Opportunity in edit mode:
The problem is that 'MM/dd/yyyy' is a US standard of a date, so this prepopulation would fail for a date format, say, 'dd/MM/yyyy'.
How can this code be generic enough to handle this problem?
P.S. I tried to use the UserInfo.getLocale() method - but it doesnt' seem to be useful.
Thanks for your future replies!
Shamil
I have a Controller, or in other words simply an Apex method, that has a method that returns a PageReference object with some querystring parameters:
Code:
public PageReference save() { String optyId = ApexPages.currentPage().getParameters().get('id'); PageReference opptyPage = new PageReference('/' + optyId + '/e'); String contractEndDateString = String.valueOf(getOpportunity().Contract_End_Date__c.month()) + '/' +String.valueOf(getOpportunity().Contract_End_Date__c.day()) + '/' + String.valueOf(getOpportunity().Contract_End_Date__c.year()); opptyPage.getParameters().put('opp9', contractEndDateString); opptyPage.setRedirect(true); return opptyPage; }
A date format in Apex is 'yyyy-MM-dd', so I manually convert it to 'MM/dd/yyyy' in order to prepopulate a 'close date' field (opp9) on Opportunity in edit mode:
Code:
String contractEndDateString = String.valueOf(getOpportunity().Contract_End_Date__c.month()) + '/' +String.valueOf(getOpportunity().Contract_End_Date__c.day()) + '/' + String.valueOf(getOpportunity().Contract_End_Date__c.year());
opptyPage.getParameters().put('opp9', contractEndDateString);
P.S. I tried to use the UserInfo.getLocale() method - but it doesnt' seem to be useful.
Thanks for your future replies!
Shamil
So the solution is:
String contractEndDateString = getOpportunity().Contract_End_Date__c.format();
The contractEndDateString will have a format of a specific locale used in a particular org.
As simple as that!
All Answers
{
if(opp.Create_Date__c!=null)
{
Date dt=opp.Create_Date__c;
//String dt=opp.Create_Date__c;
try
{
Datetime dtt=Datetime.ValueOf(dt);
String s=String.ValueOf(dtt);
opp.Create_Date__c=Date.ValueOf(s);
}
catch (Exception e){
opp.Create_Date__c=dt;
}
}
String s=String.ValueOf(dtt);
returns a date/datetime in format 'yyyy-MM-dd', so it doesn't help.
So the solution is:
String contractEndDateString = getOpportunity().Contract_End_Date__c.format();
The contractEndDateString will have a format of a specific locale used in a particular org.
As simple as that!
great idea Shamil - thanks , even if you want to format a Double or Date held in a controller property, you can assign the value to a bogus sObject field of the appropriate type, call the format() method and bind the String result to the visualforce page.
thanks it helps me a lot!