Created
March 23, 2015 14:11
-
-
Save blemale/f87370bfac3d965c4ca9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.courtanet.fm.core.editor; | |
import static org.fest.assertions.Assertions.assertThat; | |
import java.text.DateFormat; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class PermisFieldEditorTest { | |
private static final DateFormat FORMAT_ISO = new SimpleDateFormat("yyyyMMdd"); | |
@Test | |
public void test() { | |
Date dateNaissance = getDate("19800229"); | |
Date datePermis = getDate("19980201"); | |
boolean assezAgeADateDuPermis = isAssezAgeADateDuPermis(dateNaissance, datePermis, 18); | |
assertThat(assezAgeADateDuPermis).isTrue(); | |
} | |
public boolean isAssezAgeADateDuPermis(Date naissance, Date datePermis, int ageMinimum) { | |
if (naissance == null || datePermis == null) { | |
return true; | |
} | |
Date borneMin = new Date(naissance.getTime()); | |
borneMin = addMonths(borneMin, ageMinimum * 12); | |
// System.out.println(borneMin); | |
setToFirstDayOfMonth(borneMin); | |
return compareDays(borneMin, datePermis) <= 0; | |
} | |
public Date getDate(String yyyyMMdd) { | |
if (yyyyMMdd == null) { | |
return null; | |
} | |
try { | |
return FORMAT_ISO.parse(yyyyMMdd); | |
} catch (ParseException e) { | |
return null; | |
} | |
} | |
public Date addMonths(Date date, int monthsToAdd) { | |
Date newDate = new Date(date.getTime()); | |
newDate.setMonth(date.getMonth() + monthsToAdd); | |
return newDate; | |
} | |
public void setToFirstDayOfMonth(Date date) { | |
date.setDate(1); | |
} | |
public final int compareDays(Date a, Date b) { | |
if (a == null) | |
return -1; | |
if (b == null) | |
return 1; | |
int compYear = a.getYear() - b.getYear(); | |
if (0 == compYear) { | |
int compMonth = a.getMonth() - b.getMonth(); | |
if (0 == compMonth) { | |
return a.getDate() - b.getDate(); | |
} | |
return compMonth; | |
} | |
return compYear; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment