function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ShamilShamil 

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:

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);

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
Best Answer chosen by Admin (Salesforce Developers) 
ShamilShamil
I actually found a simple solution for this (not sure how I didn't realize it in the first place).
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

venkat1venkat1
for (Opportunity opp:Trigger.new)
{
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;
}
}
ShamilShamil
Venkat,

String s=String.ValueOf(dtt);

returns a date/datetime in format 'yyyy-MM-dd', so it doesn't help.
ShamilShamil
I actually found a simple solution for this (not sure how I didn't realize it in the first place).
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!
This was selected as the best answer
kforce123kforce123

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.

 

 

jinsu kimjinsu kim

thanks it helps me a lot!