1

I am using the below code to format the LastmodifiedDate for records from the Note table

public List<wNA> rowList {get; set;}  

public class wNA implements Comparable {
    // see http://salesforce.stackexchange.com/questions/106275/how-to-sort-a-custom-list-of-notes-and-attachments-by-lastmodifieddate
    public Integer compareTo(Object o) {            
        return -(LastModifiedDate.getTime()-((wNA)o).LastModifiedDate.getTime()).intValue();            
    }

    public string Type {get; set;}
    public string Id {get; set;}
    public string fileName {get; set;}
    public string createdBy {get; set;}
    public datetime LastModifiedDate {get; set;}
    public string contentType {get; set;}
    public string LastModifiedDtStr {get; set;}
}       

List<Note> lstNotes= [SELECT Id, Title, ParentId, LastModifiedDate, createdbyid, body FROM Note where ParentId =:recordid];

    for (Integer iDx=0; idx<lstNotes.size(); idx++){
        wNA tr = new wNA();

        String strLastModified = lstNotes[iDx].LastModifiedDate.format('dd mmm yyyy HH:MM:ss');

        tr.Type = 'Note';
        tr.Id = lstNotes[iDx].Id;
        tr.fileName = lstNotes[iDx].Title;
        tr.createdBy = [SELECT Id, Name from User where Id =:lstNotes[iDx].createdbyid].Name; //lstNotes[iDx].createdbyid;
        tr.LastModifiedDate = lstNotes[iDx].LastModifiedDate; // strLastModified; //  LastModifiedDtStr          
        tr.LastModifiedDtStr=strLastModified;
        rowList.add(tr);
    }    

In the Note table the LastModifiedDate field seems to be like this:

2016-01-19T15:05:05.000+0000

and when I display it in my table in VF it looks like this (very strange, it's a datetime field being put in a datetime string):

Thu Jan 21 18:41:04 GMT 2016 

and I would like to display it like this:

Thu Jan 21 2016 18:41:04  

so how could I change the line String strLastModified=lstAttachments[iDx].LastModifiedDate.format('dd mmm yyyy HH:MM:ss'); to format the date correctly?

0

2 Answers 2

2

You can use Apex DateTime format method:-

Contact con = [SELECT LastModifiedDate FROM Contact LIMIT 1];
String strLastModified=con.LastModifiedDate.format('E MMM dd yyyy HH:MM:ss');
System.debug('  @@@@  '+strLastModified);
2

If you don't want to format date in controller side then format your date in VF page using <apex:outputText>

<apex:outputText value="{0, date, E MMM dd yyyy HH:MM:ss}">
    <apex:param value="{!tr.LastModifiedDate}" /> 
</apex:outputText>

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .