+ Start a Discussion

changing the date format in the apex class

 Hi Experts,
 I need to change the format of Installation_Date__c into YYYY-MM-DD,here is the code where Installation_Date__c field is used , could anyone help me please
   for(TWM_Meter__c meter : [select id, Meter_Location__c, Meter_Box_Type__c, Exact_location_of_Meter__c, Meter_Grid_Ref_No__c,
                                            Serial_No__r.Name, In_Line_or_Concentric__c, Meter_Make__c, Meter_Type__c, Meter_Size__c, Flow_Rate__c, No_of_Dials__c,
                                            Manufactured_year__c, Last_Reading__c,  Installation_Date__c, LCE_Fitted__c, LCE_Reader_Serial_No__c,
                                            LCE_Location__c, LCE_Grid_Ref__c, New_Radio_ID__c,  Pulse__c, Property_Outer_Postcode__c,Property_Inner_Postcode__c,
                                            Northings__c, Eastings__c, Exchange_Radio_ID__c, Task__r.Job__c, 
                                            Meter_Location_Description__c, Task__r.name, // CH04 
                                            Tech_Type__c,DMA__c,MIU_fitted__c,New_MIU_Reader_Serial_Number__c,MIU_location__c,MIU_Grid_ref__c from TWM_Meter__c where  Task__r.Job__c in : mapJobTasks.keySet()
                                            AND ((Installation_Date__c >=:twmJobInst.Required_Start_Date__c //CH09
                                            AND Installation_Date__c <=:twmJobInst.Required_Completion_Date__c)//CH09
                                            OR (Installation_Date__c = null))//CH09
                                            order by Task__r.name]){ //CH04 Added order by     // CH06.New 
  strItemRow += formatDate(meter.Installation_Date__c) + '|';
You can use the Date.format method to translate it into the locale for the current user
String formattedDate = meter.Installation_Date__c.format();

Otherwise you'll have to do generate the formatted date yourself
public static String formatDate(Date d) {
    return d.year() + '-' + d.month() + '-' + d.day();

Additionally you could convert it to a Datetime and use it's formatting method
Datetime dt = new Datetime(meter.Installation_Date__c);
String formattedDate = dt.format('YYYY-MM-DD');

Jesus AlvaradoJesus Alvarado
Hey that's a nice tip! But do you have an idea how to do that, in a visualforcepage? with a apex reapeat element
There's no way to do it directly in a visualforce page.  You'd have to to make a wrapper class for your data and use that in your apex:repeat. For example

In your controller build up a list of wrapped objects
public class MyController {
    public class MyWrapper {
        public Case c {get; set}

        public String getFormattedCreatedDate() {
            return c.CreatedDate.format('YYYY-MM-DD');

        public MyWrapper() {}

        public MyWrapper(Case c) {
            this.c = c;

    public List<MyWrapper> wrappedCases {get; private set;}

    public MyController {
        this.wrappedCases = new List<MyWrapper>();

        for (Case c : [
            select CaseNumber,
            from Case
            order by CreatedDate desc
            limit 10
        ]) {
            wrappedCases.add(new MyWrapper(c));

and then in your page, repeat over those
<apex:repeat value="{!wrappedCases}" var="w">
    <apex:outputText value="{!w.c.CaseNumber}" /> - 
    <apex:outputText value="{!w.formattedCreatedDate}" />

NOTE: This code has not been tested and my contain typographical or logical errors.  Use at your own risk.
Jesus AlvaradoJesus Alvarado
Very quick answer and helpfully ! Thank you @pcon, My  scenario is a little different, I got a json object transform to a MAP, then I get the values in the visulaforce and print them there, But I can controll the API, maybe it would be better if I formatt the date from the API Server that in the Apex Code. BTW very good example! I will  take a look if some other scenario I could need it!!
Jesus AlvaradoJesus Alvarado
Actually Also with your before answer I imagine a scenario where I can loop my json object and transform the datetime to string. This is other solution but for performance I will try first change the API Server :D
Mohseen Begum 11Mohseen Begum 11
<apex:outputText value="{0,date, MM/dd/yyyy}"> <apex:param value="{!NOW()}" /> </apex:outputText>