Ad
Code
Diff
  • package com.mystuff.juststuff;
    
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    import java.time.format.DateTimeParseException;
    import java.util.Arrays;
    import java.util.Set;
    import java.util.SortedSet;
    import java.util.TreeSet;
    import java.util.function.Predicate;
    import java.util.stream.Collectors;
    import java.util.stream.IntStream;
    
    public class Palindrome {
    
      private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMddyyyy");
    
      public Set<LocalDate> countDatePalindromes(final LocalDate startDate, final LocalDate endDate) {
        final SortedSet<LocalDate> sortedDates = new TreeSet<>(Arrays.asList(startDate, endDate));
        return IntStream.rangeClosed(sortedDates.first().getYear(), sortedDates.last().getYear())
            .filter(Palindrome::isPalindromePossible)
            .mapToObj(Palindrome::createPalindrome)
            .filter(isDateInRange(sortedDates.first(), sortedDates.last()))
            .collect(Collectors.toCollection(TreeSet::new));
      }
      
      private static boolean isPalindromePossible(int year) {
          int monthFirstDigit = year % 10;
          return monthFirstDigit == 0 || monthFirstDigit == 1; 
      }
    
      private static LocalDate createPalindrome(final int year) {
        final String yearStr = String.valueOf(year);
        final String datePalindrome = new StringBuilder(yearStr).reverse().append(yearStr).toString();
        try {
          return LocalDate.parse(datePalindrome, formatter);
        } catch (final DateTimeParseException e) {}
        return null;
      }
    
      private static Predicate<LocalDate> isDateInRange(final LocalDate startDate, final LocalDate endDate) {
        return (date) -> !(date == null || date.isBefore(startDate) || date.isAfter(endDate));
      }
    }
    • package com.mystuff.juststuff;
    • import java.time.LocalDate;
    • import java.time.format.DateTimeFormatter;
    • import java.time.format.DateTimeParseException;
    • import java.util.Arrays;
    • import java.util.Set;
    • import java.util.SortedSet;
    • import java.util.TreeSet;
    • import java.util.function.Predicate;
    • import java.util.stream.Collectors;
    • import java.util.stream.IntStream;
    • public class Palindrome {
    • private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMddyyyy");
    • public Set<LocalDate> countDatePalindromes(final LocalDate startDate, final LocalDate endDate) {
    • final SortedSet<LocalDate> sortedDates = new TreeSet<>(Arrays.asList(startDate, endDate));
    • return IntStream.rangeClosed(sortedDates.first().getYear(), sortedDates.last().getYear())
    • .filter(Palindrome::isPalindromePossible)
    • .mapToObj(Palindrome::createPalindrome)
    • .filter(isDateInRange(sortedDates.first(), sortedDates.last()))
    • .collect(Collectors.toCollection(TreeSet::new));
    • }
    • private static boolean isPalindromePossible(int year) {
    • int monthFirstDigit = year % 10;
    • return monthFirstDigit == 0 || monthFirstDigit == 1;
    • }
    • private static LocalDate createPalindrome(final int year) {
    • final String yearStr = String.valueOf(year);
    • final String datePalindrome = new StringBuilder(yearStr).reverse().append(yearStr).toString();
    • try {
    • return LocalDate.parse(datePalindrome, formatter);
    • } catch (final DateTimeParseException e) {}
    • return null;
    • }
    • private static Predicate<LocalDate> isDateInRange(final LocalDate startDate, final LocalDate endDate) {
    • return (date) -> !(date == null || date.isBefore(startDate) || date.isAfter(endDate));
    • }
    • }
Code
Diff
  • package com.mystuff.juststuff;
    
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    import java.time.format.DateTimeParseException;
    import java.util.Set;
    import java.util.TreeSet;
    import java.util.stream.Collectors;
    import java.util.stream.IntStream;
    
    public class Palindrome {
    
    	private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMddyyyy");
    
    	public Set<LocalDate> countDatePalindromes(final LocalDate startDate, final LocalDate endDate) {
    		return IntStream.range(startDate.getYear(), endDate.getYear() + 1).mapToObj(Palindrome::createPalindrome)
    				.filter(date -> isValidDate(date, startDate, endDate)).collect(Collectors.toCollection(TreeSet::new));
    	}
    
    	private static LocalDate createPalindrome(int year) {
    		String yearStr = String.valueOf(year);
    		String datePalindrome = new StringBuilder().append(yearStr).reverse().append(yearStr).toString();
    		try {
    			return LocalDate.parse(datePalindrome, formatter);
    		} catch (DateTimeParseException e) {
    			return null;
    		}
    
    	}
    
    	private static boolean isValidDate(final LocalDate date, LocalDate startDate, LocalDate endDate) {
    		if (date == null)
    			return false;
    		if (startDate.isEqual(date) || endDate.isEqual(date))
    			return true;
    		return startDate.isBefore(date) && endDate.isAfter(date) ||
    				endDate.isBefore(date) && startDate.isAfter(date);
    	}
    }
    • package com.mystuff.juststuff;
    • import java.time.LocalDate;
    • import java.time.format.DateTimeFormatter;
    • import java.time.format.DateTimeParseException;
    • import java.util.Set;
    • import java.util.SortedSet;
    • import java.util.TreeSet;
    • import java.util.stream.Collectors;
    • import java.util.stream.Stream;
    • import java.time.LocalDate;
    • import java.time.format.DateTimeFormatter;
    • import java.time.temporal.ChronoUnit;
    • import java.util.stream.IntStream;
    • public class Palindrome {
    • private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMddyyyy");
    • private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMddyyyy");
    • public Set<LocalDate> countDatePalindromes(final LocalDate startDate, final LocalDate endDate) {
    • final SortedSet<LocalDate> sortedDates = Stream.of(startDate, endDate).collect(Collectors.toCollection(TreeSet::new));
    • return Stream.iterate(sortedDates.first(), date -> date.plusDays(1))
    • .limit(ChronoUnit.DAYS.between(sortedDates.first(), sortedDates.last().plusDays(1)))
    • .filter(Palindrome::isPalindrome)
    • .collect(Collectors.toCollection(TreeSet::new));
    • }
    • private static boolean isPalindrome(final LocalDate date) {
    • final String fwd = date.format(formatter);
    • return fwd.equals(new StringBuilder(fwd).reverse().toString());
    • }
    • public Set<LocalDate> countDatePalindromes(final LocalDate startDate, final LocalDate endDate) {
    • return IntStream.range(startDate.getYear(), endDate.getYear() + 1).mapToObj(Palindrome::createPalindrome)
    • .filter(date -> isValidDate(date, startDate, endDate)).collect(Collectors.toCollection(TreeSet::new));
    • }
    • private static LocalDate createPalindrome(int year) {
    • String yearStr = String.valueOf(year);
    • String datePalindrome = new StringBuilder().append(yearStr).reverse().append(yearStr).toString();
    • try {
    • return LocalDate.parse(datePalindrome, formatter);
    • } catch (DateTimeParseException e) {
    • return null;
    • }
    • }
    • private static boolean isValidDate(final LocalDate date, LocalDate startDate, LocalDate endDate) {
    • if (date == null)
    • return false;
    • if (startDate.isEqual(date) || endDate.isEqual(date))
    • return true;
    • return startDate.isBefore(date) && endDate.isAfter(date) ||
    • endDate.isBefore(date) && startDate.isAfter(date);
    • }
    • }