BAEL-994 - TemporalAdjuster in Java (#2232)

* Evaluation article: Different Types of Bean Injection in Spring

* added tests & changed configuration to Java-based config

* removed xml config files

* rename unit tests

* BAEL-972 - Apache Commons Text

* remove code from evaluation article

* remove code from evaluation article

* BAEL-972 - Apache Commons Text - added another example

* BAEL-972 - Apache Commons Text - just indentation

* BAEL-994 - TemporalAdjuster in Java

* BAEL-994 - TemporalAdjuster in Java

* BAEL-994 - TemporalAdjuster in Java

* BAEL-994 - TemporalAdjuster in Java

* BAEL-994 - TemporalAdjuster in Java - fix problems
This commit is contained in:
Ahmed Tawila 2017-07-09 10:15:22 +03:00 committed by Grzegorz Piwowarek
parent f876c6d02e
commit 13eec7e57c
3 changed files with 15 additions and 60 deletions

View File

@ -1,31 +0,0 @@
package com.baeldung.temporaladjuster;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class TemporalAdjusterUtil {
public static Calendar nextDayOfWeek(int dayOfWeek) {
Calendar date = Calendar.getInstance();
int difference = dayOfWeek - date.get(Calendar.DAY_OF_WEEK);
if (!(difference > 0)) {
difference += 7;
}
date.add(Calendar.DAY_OF_MONTH, difference);
return date;
}
@SuppressWarnings("static-access")
public static String getNextWorkingDay() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setCalendar(calendar);
if (calendar.DAY_OF_WEEK == Calendar.FRIDAY)
calendar.add(Calendar.DATE, 3);
if (calendar.DAY_OF_WEEK == Calendar.SATURDAY)
calendar.add(Calendar.DATE, 2);
else
calendar.add(Calendar.DATE, 1);
return format.format(calendar.getTime());
}
}

View File

@ -1,56 +1,49 @@
package com.baeldung.temporaladjusters;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import org.junit.Assert;
import org.junit.Test;
import com.baeldung.temporaladjuster.CustomTemporalAdjuster;
import com.baeldung.temporaladjuster.TemporalAdjusterUtil;
public class CustomTemporalAdjusterTest {
@Test
public void whenAdjustAndImplementInterface_thenNextWorkingDay() {
LocalDate localDate = LocalDate.now();
LocalDate localDate = LocalDate.of(2017, 07, 8);
CustomTemporalAdjuster temporalAdjuster = new CustomTemporalAdjuster();
LocalDate nextWorkingDay = localDate.with(temporalAdjuster);
Assert.assertEquals(TemporalAdjusterUtil.getNextWorkingDay(), nextWorkingDay.toString());
Assert.assertEquals("2017-07-10", nextWorkingDay.toString());
}
@Test
public void whenAdjust_thenNextWorkingDay() {
LocalDate localDate = LocalDate.now();
LocalDate localDate = LocalDate.of(2017, 07, 8);
TemporalAdjuster temporalAdjuster = NEXT_WORKING_DAY;
LocalDate date = localDate.with(temporalAdjuster);
Assert.assertEquals(TemporalAdjusterUtil.getNextWorkingDay(), date.toString());
Assert.assertEquals("2017-07-10", date.toString());
}
@Test
public void whenAdjust_thenFourteenDaysFromToday() {
LocalDate localDate = LocalDate.now();
public void whenAdjust_thenFourteenDaysAfterDate() {
LocalDate localDate = LocalDate.of(2017, 07, 8);
TemporalAdjuster temporalAdjuster = (t) -> t.plus(Period.ofDays(14));
LocalDate result = localDate.with(temporalAdjuster);
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setCalendar(calendar);
calendar.add(Calendar.DATE, 14);
String fourteenDaysFromToday = format.format(calendar.getTime());
String fourteenDaysAfterDate = "2017-07-22";
Assert.assertEquals(fourteenDaysFromToday, result.toString());
Assert.assertEquals(fourteenDaysAfterDate, result.toString());
}
static TemporalAdjuster NEXT_WORKING_DAY = TemporalAdjusters.ofDateAdjuster(today -> {
DayOfWeek dayOfWeek = today.getDayOfWeek();
static TemporalAdjuster NEXT_WORKING_DAY = TemporalAdjusters.ofDateAdjuster(date -> {
DayOfWeek dayOfWeek = date.getDayOfWeek();
int daysToAdd;
if (dayOfWeek == DayOfWeek.FRIDAY)
daysToAdd = 3;
@ -58,6 +51,6 @@ public class CustomTemporalAdjusterTest {
daysToAdd = 2;
else
daysToAdd = 1;
return today.plusDays(daysToAdd);
return date.plusDays(daysToAdd);
});
}

View File

@ -1,29 +1,22 @@
package com.baeldung.temporaladjusters;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import org.junit.Assert;
import org.junit.Test;
import com.baeldung.temporaladjuster.TemporalAdjusterUtil;
public class TemporalAdjustersTest {
@Test
public void whenAdjust_thenNextSunday() {
LocalDate localDate = LocalDate.now();
LocalDate localDate = LocalDate.of(2017, 07, 8);
LocalDate nextSunday = localDate.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
Calendar calendar = TemporalAdjusterUtil.nextDayOfWeek(Calendar.SUNDAY);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setCalendar(calendar);
String formattedDate = format.format(calendar.getTime());
String expected = "2017-07-09";
Assert.assertEquals(formattedDate, nextSunday.toString());
Assert.assertEquals(expected, nextSunday.toString());
}
}