Ad
Code
Diff
  • package com.mystuff.juststuff;
    
    import java.text.*;
    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();
    
    
        /*
          end date is chronologically before start date. flip them.
          NOTE: lines 23-30 should be a pre-condition to the countDatePalindromes method,
          so they should be deleted.
        */
    		if (startDate.getTime() > endDate.getTime()) {
    			startCal.setTime(endDate);
    			endCal.setTime(startDate);
    		}else {
    			startCal.setTime(startDate);
    			endCal.setTime(endDate);
    		}
    
    		String regularDate = null;
    		String palDate = null;
    		while ( (startCal.before(endCal)) || (startCal.equals(endCal)) ) {
    			regularDate = sdf.format(startCal.getTime());
    			palDate = palindromify(startCal.getTime());			
          
          // a date palindrome was found
    			if (regularDate.equals(palDate))
    				palSet.add(startCal.getTime());
            
    			startCal.add(Calendar.DAY_OF_MONTH, 1);
    		}	
    		return palSet;
    	}
    	
    	private String palindromify(final Date arg) {
    		StringBuffer palBuff = new StringBuffer(8);
        palBuff = sdf.format(arg, palBuff, new FieldPosition(0));
    		return palBuff.reverse().toString();
    	}
    
    }
    
    • package com.mystuff.juststuff;
    • import java.text.SimpleDateFormat;
    • import java.text.*;
    • import java.util.Calendar;
    • import java.util.Date;
    • import java.util.GregorianCalendar;
    • import java.util.TreeSet;
    • public class Palindrome
    • {
    • public class Palindrome {
    • SimpleDateFormat sdf;
    • public Palindrome()
    • {
    • public Palindrome() {
    • this.sdf = new SimpleDateFormat("MMddyyyy");
    • sdf.setLenient(false);
    • }
    • public TreeSet<Date> countDatePalindromes(Date startDate, Date endDate)
    • {
    • 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()));
    • /*
    • end date is chronologically before start date. flip them.
    • NOTE: lines 23-30 should be a pre-condition to the countDatePalindromes method,
    • so they should be deleted.
    • */
    • if (startDate.getTime() > endDate.getTime()) {
    • startCal.setTime(endDate);
    • endCal.setTime(startDate);
    • }
    • else
    • {
    • // start date is chronologically prior to end date. just set the calendars as such.
    • }else {
    • 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)) )
    • {
    • while ( (startCal.before(endCal)) || (startCal.equals(endCal)) ) {
    • regularDate = sdf.format(startCal.getTime());
    • palDate = palindromify(startCal.getTime());
    • palDate = palindromify(startCal.getTime());
    • // a date palindrome was found
    • 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);
    • private String palindromify(final Date arg) {
    • 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();
    • palBuff = sdf.format(arg, palBuff, new FieldPosition(0));
    • return palBuff.reverse().toString();
    • }
    • }