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
Zach Gavin DelacroixZach Gavin Delacroix 

How to convert list of DateTime

Hi Experts!

I'm trying to convert list of Account's created date to a specific timeZone. Like PST.

I'm able to convert a single record like below.
 
public class accountTimeZone {
    public account acc{get;set;}
    public String LocalDate{get;set;}
   
    public accountTimeZone(){
        acc = [select createdDate from Account where id = '0019000001JCS1x'];
        LocalDate = acc.createdDate.format('MMMM dd, yyyy hh:mm a','America/New_York');
        System.debug(LocalDate);
    }
}

But how will I go about converting a list of Create Dates like.
 
public class accountTimeZone {

public list<account> AccountDate {get;set;}
public list<DateTime> convertedDate{get;set;}

public accountTimeZone(){

        AccountDate = [select createdDate from account limit 5];
        
    }
}

I'd like to convert these dates then call them in my visualforce page apex table.

Thank you in advance!

-Zach
Best Answer chosen by Zach Gavin Delacroix
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Zach,

We forgot to assign the memory for the list. 

Just add one more line, it should work fine for you -
 
public class accountTimeZone {

public list<String> convertedDate{get;set;}

public accountTimeZone(){

       //Assigning memory
       convertedDate = new List<String>();

        for(Account AccRec :  [select createdDate from account limit 5]){
                String dt = accRec.createdDate.format('MMMM dd, yyyy hh:mm a','America/New_York');
				convertedDate.add(dt);
        } 
        System.debug(convertedDate);      
    }
}
Let me know, if it helps you.


Thanks,
Sumit Kumar Singh

All Answers

Sumit Kumar Singh 9Sumit Kumar Singh 9
Hi Zach, 
You can try this -
public class accountTimeZone {

public list<account> AccountDate {get;set;}
public list<DateTime> convertedDate{get;set;}

public accountTimeZone(){

       // AccountDate = [select createdDate from account limit 5];
        for(Account AccRec :  [select createdDate from account limit 5]){
                DateTime dt = accRec.createdDate.format('MMMM dd, yyyy hh:mm a','America/New_York');
				convertedDate.add(dt);
        }       
    }
}

Pls, let me know, if it helps you.


Thanks,
Sumit Kumar Singh
Dushyant SonwarDushyant Sonwar

Hi Zach,

Salesforce DB saves DateTime field in GMT Format.
You can use Timezone class in salesforce to convert your dateTIme field according to your Timezone.

You can check out this link for more info:
http://salesforce.stackexchange.com/questions/8538/convert-time-from-a-different-timezone-other-than-local-to-gmt

Hope this helps.

Zach Gavin DelacroixZach Gavin Delacroix
Hi Sumit,

Thanks for the Response, I've tried the code before I submitted this question but I was getting an Error.

Error: accountTimeZone Compile Error: Illegal assignment from String to Datetime at line 19 column 22

Since the value that we are passing to the dt variable is Text. However, when I change the variable and List to String like below..
 
public class accountTimeZone {

public list<String> convertedDate{get;set;}

public accountTimeZone(){
        for(Account AccRec :  [select createdDate from account limit 5]){
                String dt = accRec.createdDate.format('MMMM dd, yyyy hh:mm a','America/New_York');
				convertedDate.add(dt);
        }       
    }
}

I will get Error: System.NullPointerException: Attempt to de-reference a null object


Thank you.

-Zach
Zach Gavin DelacroixZach Gavin Delacroix
@Dushyant Sonwar

Thank you for the Suggestion. I will read about the TimeZone Class. :)
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Zach,

We forgot to assign the memory for the list. 

Just add one more line, it should work fine for you -
 
public class accountTimeZone {

public list<String> convertedDate{get;set;}

public accountTimeZone(){

       //Assigning memory
       convertedDate = new List<String>();

        for(Account AccRec :  [select createdDate from account limit 5]){
                String dt = accRec.createdDate.format('MMMM dd, yyyy hh:mm a','America/New_York');
				convertedDate.add(dt);
        } 
        System.debug(convertedDate);      
    }
}
Let me know, if it helps you.


Thanks,
Sumit Kumar Singh
This was selected as the best answer
Zach Gavin DelacroixZach Gavin Delacroix

Thanks Sumit! This worked! You did it again :)