+ Start a Discussion
lakslaks 

Problem with Date retrieved from List

Hi..am facing a problem with Date formatting.

 

In the below code, 

Am inserting dates into the list in the format Date.newInstance(2011, 01, 01), but while iterating list and retrieving the date, it comes out in a different format.


So my further calculations are failing. Could anybody please advice on the same.

When I run the code I get "Internal Salesforce Error".

 

Problem is because the dates I am comparing are in different formats:

l_dtClosedDate is assigned value: Fri Jan 01 00:00:00 GMT 2010
l_dtMaxCompletedDate is assigned value: 1970-01-01 00:00:00

 

I want l_dtClosedDate  also in the same format as l_dtMaxCompletedDate.
Actually that is the format in which it is created when I do Date.newInstance(2011, 01, 01) and insert it into the list.

 

I am not able to do any conversion with the l_dtClosedDate variable fetched from the List. All functions fail when used with that variable.

 

1. Can anybody advise as to how to convert the l_dtClosedDate variable to this format - 1970-01-01 00:00:00
2. Or how to convert a date in this format - 1970-01-01 00:00:00 to Sat Jan 01 00:00:00 GMT 2011.

 

public class Maxx
{
	public static void getMaxDate()
	{
		List<Date> l_ltClosedDateList = new List<Date>();
		Date l_dtMaxCompletedDate = Date.newInstance(1970, 1, 1);
		Date l_dtClosedDate;
		String Date1;
		
		//Data
		l_ltClosedDateList.add(Date.newInstance(2010, 1, 1));
		l_ltClosedDateList.add(Date.newInstance(2010, 1, 11));
		l_ltClosedDateList.add(Date.newInstance(2010, 1, 12));
		
		Iterator<Date> l_itClosedDateIter = l_ltClosedDateList.iterator();
		while(l_itClosedDateIter.hasNext())
		{
			System.debug('Entered while loop');
			l_dtClosedDate = l_itClosedDateIter.next();
			
			System.debug('Closed date is assigned value: '+l_dtClosedDate);			
			System.debug('Max completed date is assigned value: '+l_dtMaxCompletedDate);

			if( l_dtClosedDate > l_dtMaxCompletedDate )
			{
				System.debug('Entered if loop');
				l_dtMaxCompletedDate = l_dtClosedDate;
			}
		}
		System.debug('Max Completed Date: '+l_dtMaxCompletedDate);
	}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Its not the fact that the dates are stored in a list, its the iterator that is giving you the problem.  If you change the code to:

 

 

 

public class Maxx
{
	public static void getMaxDate()
	{
		List<Date> l_ltClosedDateList = new List<Date>();
		Date l_dtMaxCompletedDate = Date.newInstance(1960, 1, 2);
		
		//Data
		l_ltClosedDateList.add(Date.newInstance(2010, 1, 1));
		System.debug('List element 0 is assigned value: '+l_ltClosedDateList[0]);
		l_ltClosedDateList.add(Date.newInstance(2010, 1, 11));
		l_ltClosedDateList.add(Date.newInstance(2010, 1, 12));
		
        for (Date l_dtClosedDate : l_ltClosedDateList)
		{
			System.debug('Entered while loop');
			
			System.debug('Closed date is assigned value: '+l_dtClosedDate);			
			System.debug('Max completed date is assigned value: '+l_dtMaxCompletedDate);

			if( l_dtClosedDate > l_dtMaxCompletedDate )
			{
				System.debug('Entered if loop');
				l_dtMaxCompletedDate = l_dtClosedDate;
			}
		} 
		System.debug('Max Completed Date: '+l_dtMaxCompletedDate);
	}
}

 

then it works as expected.  

 

All Answers

bob_buzzardbob_buzzard

Its not the fact that the dates are stored in a list, its the iterator that is giving you the problem.  If you change the code to:

 

 

 

public class Maxx
{
	public static void getMaxDate()
	{
		List<Date> l_ltClosedDateList = new List<Date>();
		Date l_dtMaxCompletedDate = Date.newInstance(1960, 1, 2);
		
		//Data
		l_ltClosedDateList.add(Date.newInstance(2010, 1, 1));
		System.debug('List element 0 is assigned value: '+l_ltClosedDateList[0]);
		l_ltClosedDateList.add(Date.newInstance(2010, 1, 11));
		l_ltClosedDateList.add(Date.newInstance(2010, 1, 12));
		
        for (Date l_dtClosedDate : l_ltClosedDateList)
		{
			System.debug('Entered while loop');
			
			System.debug('Closed date is assigned value: '+l_dtClosedDate);			
			System.debug('Max completed date is assigned value: '+l_dtMaxCompletedDate);

			if( l_dtClosedDate > l_dtMaxCompletedDate )
			{
				System.debug('Entered if loop');
				l_dtMaxCompletedDate = l_dtClosedDate;
			}
		} 
		System.debug('Max Completed Date: '+l_dtMaxCompletedDate);
	}
}

 

then it works as expected.  

 

This was selected as the best answer
lakslaks

Thanks a lot Bob :-)

 

But does that mean that we cannot use iterator in such scenarios ?

Is there any particular cases where iterators cant be used in Apex etc.

I find that a bit odd since was using iterators a lot in java.

 

 

bob_buzzardbob_buzzard

I've not been able to find very much information at all with regard to Iterators.  I've been doing some more experimenting and it isn't just dates that this is limited to - a List of Integers exhibits the same behaviour.  It seems to be when you attempt to execute a method on the object retrieved from the Iterator.  The object itself checks out okay via the instanceof keyword, but there must be something inconsistent about it.

 

As its an internal system error, this implies Salesforce is raising an unhandled error condition, so I'd be inclined to raise a case with support about this.