BAEL-994 - TemporalAdjuster in Java (#2227)

* 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
This commit is contained in:
Ahmed Tawila 2017-07-08 08:59:59 +03:00 committed by Grzegorz Piwowarek
parent fdd26c7c52
commit 647455b029
5 changed files with 149 additions and 3 deletions

View File

@ -0,0 +1,23 @@
package com.baeldung.temporaladjuster;
import java.time.DayOfWeek;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjuster;
public class CustomTemporalAdjuster implements TemporalAdjuster {
@Override
public Temporal adjustInto(Temporal temporal) {
DayOfWeek dayOfWeek = DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK));
int daysToAdd;
if (dayOfWeek == DayOfWeek.FRIDAY)
daysToAdd = 3;
else if (dayOfWeek == DayOfWeek.SATURDAY)
daysToAdd = 2;
else
daysToAdd = 1;
return temporal.plus(daysToAdd, ChronoUnit.DAYS);
}
}

View File

@ -0,0 +1,31 @@
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

@ -0,0 +1,63 @@
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();
CustomTemporalAdjuster temporalAdjuster = new CustomTemporalAdjuster();
LocalDate nextWorkingDay = localDate.with(temporalAdjuster);
Assert.assertEquals(TemporalAdjusterUtil.getNextWorkingDay(), nextWorkingDay.toString());
}
@Test
public void whenAdjust_thenNextWorkingDay() {
LocalDate localDate = LocalDate.now();
TemporalAdjuster temporalAdjuster = NEXT_WORKING_DAY;
LocalDate date = localDate.with(temporalAdjuster);
Assert.assertEquals(TemporalAdjusterUtil.getNextWorkingDay(), date.toString());
}
@Test
public void whenAdjust_thenFourteenDaysFromToday() {
LocalDate localDate = LocalDate.now();
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());
Assert.assertEquals(fourteenDaysFromToday, result.toString());
}
static TemporalAdjuster NEXT_WORKING_DAY = TemporalAdjusters.ofDateAdjuster(today -> {
DayOfWeek dayOfWeek = today.getDayOfWeek();
int daysToAdd;
if (dayOfWeek == DayOfWeek.FRIDAY)
daysToAdd = 3;
else if (dayOfWeek == DayOfWeek.SATURDAY)
daysToAdd = 2;
else
daysToAdd = 1;
return today.plusDays(daysToAdd);
});
}

View File

@ -0,0 +1,29 @@
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 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());
Assert.assertEquals(formattedDate, nextSunday.toString());
}
}

View File

@ -5,12 +5,12 @@ import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
public class StrBuilderTest { public class StrBuilderTest {
@Test @Test
public void whenReplaced_thenCorrect() { public void whenReplaced_thenCorrect() {
StrBuilder strBuilder = new StrBuilder("example StrBuilder!"); StrBuilder strBuilder = new StrBuilder("example StrBuilder!");
strBuilder.replaceAll("example", "new"); strBuilder.replaceAll("example", "new");
Assert.assertEquals(new StrBuilder("new StrBuilder!"), strBuilder); Assert.assertEquals(new StrBuilder("new StrBuilder!"), strBuilder);
} }
@ -21,4 +21,4 @@ public class StrBuilderTest {
Assert.assertEquals(new StrBuilder(""), strBuilder); Assert.assertEquals(new StrBuilder(""), strBuilder);
} }
} }