Given a start date and end date, find a list of dates with the format of month, day year, that is the same forward as it is backward. For example, the date October 2, 2001 is a palindrome because in MMDDYYYY format, this date is 10022001 and in YYYYDDMM format the date is 10022001. Your task is to find all the palindrome dates for any given start and end date.
The method that calculates this should take two Date types, one for the start date and one for the end date. The method should return a Set of Date types that is sorted chronologically and has no duplicates.
package com.mystuff.juststuff;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TreeSet;
public class Palindrome
{
SimpleDateFormat sdf;
public Palindrome()
{
this.sdf = new SimpleDateFormat("MMddyyyy");
sdf.setLenient(false);
}
public TreeSet<Date> countDatePalindromes(Date startDate, Date endDate)
{
TreeSet<Date> palSet = new TreeSet<Date>();
GregorianCalendar startCal = new GregorianCalendar();
GregorianCalendar endCal = new GregorianCalendar();
if (startDate.getTime() > endDate.getTime())
{
// end date is chronologically before start date. flip them.
// System.out.println("end is before start. must flip. start: " + sdf.format(startDate.getTime()) + " end: " + sdf.format(endDate.getTime()));
startCal.setTime(endDate);
endCal.setTime(startDate);
}
else
{
// start date is chronologically prior to end date. just set the calendars as such.
startCal.setTime(startDate);
endCal.setTime(endDate);
}
// System.out.println("start should be before end. start: " + sdf.format(startCal.getTime()) + " end: " + sdf.format(endCal.getTime()));
String regularDate = null;
String palDate = null;
while ( (startCal.before(endCal)) || (startCal.equals(endCal)) )
{
regularDate = sdf.format(startCal.getTime());
palDate = palindromify(startCal.getTime());
if (regularDate.equals(palDate))
{
// a date palindrome was found
// System.out.println("Found one! regular: " + regularDate + " palindrome: " + palDate);
palSet.add(startCal.getTime());
}
startCal.add(Calendar.DAY_OF_MONTH, 1);
}
// System.out.println("I found " + palSet.size() + " palindromes");
return palSet;
}
private String palindromify(final Date arg)
{
SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy");
sdf.setLenient(false);
StringBuffer palBuff = new StringBuffer(8);
String pal = sdf.format(arg);
for (int i = pal.length(); i >= 1; i--)
{
palBuff.append(pal.charAt(i - 1));
}
return palBuff.toString();
}
}
package com.mystuff.juststuff;
import static org.junit.Assert.assertTrue;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TreeSet;
import org.junit.Test;
public class PalindromeTest
{
@Test
public void testCountDatePalindromesEndDateBeforeStartDate()
{
GregorianCalendar startCalTest = new GregorianCalendar(2001, 11, 1);
GregorianCalendar endCalTest = new GregorianCalendar(2001, 0, 31);
Date startDate = new Date(startCalTest.getTimeInMillis());
Date endDate = new Date(endCalTest.getTimeInMillis());
Palindrome drome = new Palindrome();
TreeSet<Date> palSet = drome.countDatePalindromes(startDate, endDate);
assertTrue("Set size should be greater than zero", palSet.size() > 0);
}
@Test
public void testCountDatePalindromesStartDateBeforeEndDate()
{
GregorianCalendar startCalTest = new GregorianCalendar(2001, 0, 1);
GregorianCalendar endCalTest = new GregorianCalendar(2001, 11, 31);
Date startDate = new Date(startCalTest.getTimeInMillis());
Date endDate = new Date(endCalTest.getTimeInMillis());
Palindrome drome = new Palindrome();
TreeSet<Date> palSet = drome.countDatePalindromes(startDate, endDate);
assertTrue("Set size should be greater than zero", palSet.size() > 0);
}
@Test
public void testCountDatePalindromesStartDateEqualsEndDateNoPalindromes()
{
GregorianCalendar startCalTest = new GregorianCalendar(2001, 11, 31); // Nov 31, 2001 is not a palindrome date
GregorianCalendar endCalTest = new GregorianCalendar(2001, 11, 31);
Date startDate = new Date(startCalTest.getTimeInMillis());
Date endDate = new Date(endCalTest.getTimeInMillis());
Palindrome drome = new Palindrome();
TreeSet<Date> palSet = drome.countDatePalindromes(startDate, endDate);
assertTrue("Set size should be zero", palSet.size() == 0);
}
@Test
public void testCountDatePalindromesStartDateEqualsEndDateOnePalindrome()
{
GregorianCalendar startCalTest = new GregorianCalendar(2001, 9, 2); // Oct 2, 2001 is a palindrome date
GregorianCalendar endCalTest = new GregorianCalendar(2001, 9, 2);
Date startDate = new Date(startCalTest.getTimeInMillis());
Date endDate = new Date(endCalTest.getTimeInMillis());
Palindrome drome = new Palindrome();
TreeSet<Date> palSet = drome.countDatePalindromes(startDate, endDate);
assertTrue("Set size should be one", palSet.size() == 1);
}
@Test
public void testCountDatePalindromesHugeDateRange()
{
GregorianCalendar startCalTest = new GregorianCalendar(1900, 0, 1);
GregorianCalendar endCalTest = new GregorianCalendar(2001, 11, 31);
Date startDate = new Date(startCalTest.getTimeInMillis());
Date endDate = new Date(endCalTest.getTimeInMillis());
Palindrome drome = new Palindrome();
TreeSet<Date> palSet = drome.countDatePalindromes(startDate, endDate);
assertTrue("Set size should be greater than zero", palSet.size() > 0);
}
}