[BAEL-8456] - Moved Java Date articles into a new module - 'java-dates'
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.date;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
public class DateWithoutTime {
|
||||
|
||||
public static Date getDateWithoutTimeUsingCalendar() {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(Calendar.HOUR_OF_DAY, 0);
|
||||
calendar.set(Calendar.MINUTE, 0);
|
||||
calendar.set(Calendar.SECOND, 0);
|
||||
calendar.set(Calendar.MILLISECOND, 0);
|
||||
|
||||
return calendar.getTime();
|
||||
}
|
||||
|
||||
public static Date getDateWithoutTimeUsingFormat() throws ParseException {
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
|
||||
return formatter.parse(formatter.format(new Date()));
|
||||
}
|
||||
|
||||
public static LocalDate getLocalDate() {
|
||||
return LocalDate.now();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
import org.apache.commons.lang3.time.DateUtils;
|
||||
|
||||
public class AddHoursToDate {
|
||||
|
||||
public Date addHoursToJavaUtilDate(Date date, int hours) {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(date);
|
||||
calendar.add(Calendar.HOUR_OF_DAY, hours);
|
||||
return calendar.getTime();
|
||||
}
|
||||
|
||||
public Date addHoursToDateUsingInstant(Date date, int hours) {
|
||||
return Date.from(date.toInstant()
|
||||
.plus(Duration.ofHours(hours)));
|
||||
}
|
||||
|
||||
public LocalDateTime addHoursToLocalDateTime(LocalDateTime localDateTime, int hours) {
|
||||
return localDateTime.plusHours(hours);
|
||||
}
|
||||
|
||||
public LocalDateTime subtractHoursToLocalDateTime(LocalDateTime localDateTime, int hours) {
|
||||
return localDateTime.minusHours(hours);
|
||||
}
|
||||
|
||||
public ZonedDateTime addHoursToZonedDateTime(ZonedDateTime zonedDateTime, int hours) {
|
||||
return zonedDateTime.plusHours(hours);
|
||||
}
|
||||
|
||||
public ZonedDateTime subtractHoursToZonedDateTime(ZonedDateTime zonedDateTime, int hours) {
|
||||
return zonedDateTime.minusHours(hours);
|
||||
}
|
||||
|
||||
public Instant addHoursToInstant(Instant instant, int hours) {
|
||||
return instant.plus(hours, ChronoUnit.HOURS);
|
||||
}
|
||||
|
||||
public Instant subtractHoursToInstant(Instant instant, int hours) {
|
||||
return instant.minus(hours, ChronoUnit.HOURS);
|
||||
}
|
||||
|
||||
public Date addHoursWithApacheCommons(Date date, int hours) {
|
||||
return DateUtils.addHours(date, hours);
|
||||
}
|
||||
}
|
||||
2
java-dates/src/main/java/com/baeldung/datetime/README.md
Normal file
2
java-dates/src/main/java/com/baeldung/datetime/README.md
Normal file
@@ -0,0 +1,2 @@
|
||||
### Relevant Articles:
|
||||
- [Introduction to the Java 8 Date/Time API](http://www.baeldung.com/java-8-date-time-intro)
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalTime;
|
||||
|
||||
public class UseDuration {
|
||||
|
||||
public LocalTime modifyDates(LocalTime localTime, Duration duration) {
|
||||
return localTime.plus(duration);
|
||||
}
|
||||
|
||||
public Duration getDifferenceBetweenDates(LocalTime localTime1, LocalTime localTime2) {
|
||||
return Duration.between(localTime1, localTime2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.time.DayOfWeek;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.time.temporal.TemporalAdjusters;
|
||||
|
||||
class UseLocalDate {
|
||||
|
||||
LocalDate getLocalDateUsingFactoryOfMethod(int year, int month, int dayOfMonth) {
|
||||
return LocalDate.of(year, month, dayOfMonth);
|
||||
}
|
||||
|
||||
LocalDate getLocalDateUsingParseMethod(String representation) {
|
||||
return LocalDate.parse(representation);
|
||||
}
|
||||
|
||||
LocalDate getLocalDateFromClock() {
|
||||
LocalDate localDate = LocalDate.now();
|
||||
return localDate;
|
||||
}
|
||||
|
||||
LocalDate getNextDay(LocalDate localDate) {
|
||||
return localDate.plusDays(1);
|
||||
}
|
||||
|
||||
LocalDate getPreviousDay(LocalDate localDate) {
|
||||
return localDate.minus(1, ChronoUnit.DAYS);
|
||||
}
|
||||
|
||||
DayOfWeek getDayOfWeek(LocalDate localDate) {
|
||||
DayOfWeek day = localDate.getDayOfWeek();
|
||||
return day;
|
||||
}
|
||||
|
||||
LocalDate getFirstDayOfMonth() {
|
||||
LocalDate firstDayOfMonth = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
|
||||
return firstDayOfMonth;
|
||||
}
|
||||
|
||||
LocalDateTime getStartOfDay(LocalDate localDate) {
|
||||
LocalDateTime startofDay = localDate.atStartOfDay();
|
||||
return startofDay;
|
||||
}
|
||||
|
||||
LocalDateTime getStartOfDayOfLocalDate(LocalDate localDate) {
|
||||
LocalDateTime startofDay = LocalDateTime.of(localDate, LocalTime.MIDNIGHT);
|
||||
return startofDay;
|
||||
}
|
||||
|
||||
LocalDateTime getStartOfDayAtMinTime(LocalDate localDate) {
|
||||
LocalDateTime startofDay = localDate.atTime(LocalTime.MIN);
|
||||
return startofDay;
|
||||
}
|
||||
|
||||
LocalDateTime getStartOfDayAtMidnightTime(LocalDate localDate) {
|
||||
LocalDateTime startofDay = localDate.atTime(LocalTime.MIDNIGHT);
|
||||
return startofDay;
|
||||
}
|
||||
|
||||
LocalDateTime getEndOfDay(LocalDate localDate) {
|
||||
LocalDateTime endOfDay = localDate.atTime(LocalTime.MAX);
|
||||
return endOfDay;
|
||||
}
|
||||
|
||||
LocalDateTime getEndOfDayFromLocalTime(LocalDate localDate) {
|
||||
LocalDateTime endOfDate = LocalTime.MAX.atDate(localDate);
|
||||
return endOfDate;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.temporal.ChronoField;
|
||||
|
||||
public class UseLocalDateTime {
|
||||
|
||||
public LocalDateTime getLocalDateTimeUsingParseMethod(String representation) {
|
||||
return LocalDateTime.parse(representation);
|
||||
}
|
||||
|
||||
LocalDateTime getEndOfDayFromLocalDateTimeDirectly(LocalDateTime localDateTime) {
|
||||
LocalDateTime endOfDate = localDateTime.with(ChronoField.NANO_OF_DAY, LocalTime.MAX.toNanoOfDay());
|
||||
return endOfDate;
|
||||
}
|
||||
|
||||
LocalDateTime getEndOfDayFromLocalDateTime(LocalDateTime localDateTime) {
|
||||
LocalDateTime endOfDate = localDateTime.toLocalDate()
|
||||
.atTime(LocalTime.MAX);
|
||||
return endOfDate;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.time.LocalTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
public class UseLocalTime {
|
||||
|
||||
LocalTime getLocalTimeUsingFactoryOfMethod(int hour, int min, int seconds) {
|
||||
return LocalTime.of(hour, min, seconds);
|
||||
}
|
||||
|
||||
LocalTime getLocalTimeUsingParseMethod(String timeRepresentation) {
|
||||
return LocalTime.parse(timeRepresentation);
|
||||
}
|
||||
|
||||
private LocalTime getLocalTimeFromClock() {
|
||||
return LocalTime.now();
|
||||
}
|
||||
|
||||
LocalTime addAnHour(LocalTime localTime) {
|
||||
return localTime.plus(1, ChronoUnit.HOURS);
|
||||
}
|
||||
|
||||
int getHourFromLocalTime(LocalTime localTime) {
|
||||
return localTime.getHour();
|
||||
}
|
||||
|
||||
LocalTime getLocalTimeWithMinuteSetToValue(LocalTime localTime, int minute) {
|
||||
return localTime.withMinute(minute);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.Period;
|
||||
|
||||
class UsePeriod {
|
||||
|
||||
LocalDate modifyDates(LocalDate localDate, Period period) {
|
||||
return localDate.plus(period);
|
||||
}
|
||||
|
||||
Period getDifferenceBetweenDates(LocalDate localDate1, LocalDate localDate2) {
|
||||
return Period.between(localDate1, localDate2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
public class UseToInstant {
|
||||
|
||||
public LocalDateTime convertDateToLocalDate(Date date) {
|
||||
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
|
||||
}
|
||||
|
||||
public LocalDateTime convertDateToLocalDate(Calendar calendar) {
|
||||
return LocalDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.temporal.ChronoField;
|
||||
|
||||
class UseZonedDateTime {
|
||||
|
||||
ZonedDateTime getZonedDateTime(LocalDateTime localDateTime, ZoneId zoneId) {
|
||||
return ZonedDateTime.of(localDateTime, zoneId);
|
||||
}
|
||||
|
||||
ZonedDateTime getStartOfDay(LocalDate localDate, ZoneId zone) {
|
||||
ZonedDateTime startofDay = localDate.atStartOfDay()
|
||||
.atZone(zone);
|
||||
return startofDay;
|
||||
}
|
||||
|
||||
ZonedDateTime getStartOfDayShorthand(LocalDate localDate, ZoneId zone) {
|
||||
ZonedDateTime startofDay = localDate.atStartOfDay(zone);
|
||||
return startofDay;
|
||||
}
|
||||
|
||||
ZonedDateTime getStartOfDayFromZonedDateTime(ZonedDateTime zonedDateTime) {
|
||||
ZonedDateTime startofDay = zonedDateTime.toLocalDateTime()
|
||||
.toLocalDate()
|
||||
.atStartOfDay(zonedDateTime.getZone());
|
||||
return startofDay;
|
||||
}
|
||||
|
||||
ZonedDateTime getStartOfDayAtMinTime(ZonedDateTime zonedDateTime) {
|
||||
ZonedDateTime startofDay = zonedDateTime.with(ChronoField.HOUR_OF_DAY, 0);
|
||||
return startofDay;
|
||||
}
|
||||
|
||||
ZonedDateTime getStartOfDayAtMidnightTime(ZonedDateTime zonedDateTime) {
|
||||
ZonedDateTime startofDay = zonedDateTime.with(ChronoField.NANO_OF_DAY, 0);
|
||||
return startofDay;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.java9.datetime;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Class which shows a way to convert java.util.Date into java.time.LocalDate.
|
||||
*
|
||||
* @author abialas
|
||||
*
|
||||
*/
|
||||
public class DateToLocalDateConverter {
|
||||
|
||||
public static LocalDate convertToLocalDateViaInstant(Date dateToConvert) {
|
||||
return dateToConvert.toInstant()
|
||||
.atZone(ZoneId.systemDefault())
|
||||
.toLocalDate();
|
||||
}
|
||||
|
||||
public static LocalDate convertToLocalDateViaSqlDate(Date dateToConvert) {
|
||||
return new java.sql.Date(dateToConvert.getTime()).toLocalDate();
|
||||
}
|
||||
|
||||
public static LocalDate convertToLocalDateViaMilisecond(Date dateToConvert) {
|
||||
return Instant.ofEpochMilli(dateToConvert.getTime())
|
||||
.atZone(ZoneId.systemDefault())
|
||||
.toLocalDate();
|
||||
}
|
||||
|
||||
public static LocalDate convertToLocalDate(Date dateToConvert) {
|
||||
return LocalDate.ofInstant(dateToConvert.toInstant(), ZoneId.systemDefault());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.java9.datetime;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Class which shows a way to convert java.util.Date into java.time.LocalDateTime.
|
||||
*
|
||||
* @author abialas
|
||||
*
|
||||
*/
|
||||
public class DateToLocalDateTimeConverter {
|
||||
|
||||
public static LocalDateTime convertToLocalDateTimeViaInstant(Date dateToConvert) {
|
||||
return dateToConvert.toInstant()
|
||||
.atZone(ZoneId.systemDefault())
|
||||
.toLocalDateTime();
|
||||
}
|
||||
|
||||
public static LocalDateTime convertToLocalDateTimeViaSqlTimestamp(Date dateToConvert) {
|
||||
return new java.sql.Timestamp(dateToConvert.getTime()).toLocalDateTime();
|
||||
}
|
||||
|
||||
public static LocalDateTime convertToLocalDateTimeViaMilisecond(Date dateToConvert) {
|
||||
return Instant.ofEpochMilli(dateToConvert.getTime())
|
||||
.atZone(ZoneId.systemDefault())
|
||||
.toLocalDateTime();
|
||||
}
|
||||
|
||||
public static LocalDateTime convertToLocalDateTime(Date dateToConvert) {
|
||||
return LocalDateTime.ofInstant(dateToConvert.toInstant(), ZoneId.systemDefault());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.java9.datetime;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Class which shows different ways of converting java.time.LocalDateTime into java.util.Date.
|
||||
*
|
||||
* @author abialas
|
||||
*
|
||||
*/
|
||||
public class LocalDateTimeToDateConverter {
|
||||
|
||||
public static Date convertToDateViaSqlTimestamp(LocalDateTime dateToConvert) {
|
||||
return java.sql.Timestamp.valueOf(dateToConvert);
|
||||
}
|
||||
|
||||
public static Date convertToDateViaInstant(LocalDateTime dateToConvert) {
|
||||
return java.util.Date.from(dateToConvert.atZone(ZoneId.systemDefault())
|
||||
.toInstant());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.java9.datetime;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Class which shows different ways of converting java.time.LocalDate into java.util.Date.
|
||||
*
|
||||
* @author abialas
|
||||
*
|
||||
*/
|
||||
public class LocalDateToDateConverter {
|
||||
|
||||
public static Date convertToDateViaSqlDate(LocalDate dateToConvert) {
|
||||
return java.sql.Date.valueOf(dateToConvert);
|
||||
}
|
||||
|
||||
public static Date convertToDateViaInstant(LocalDate dateToConvert) {
|
||||
return java.util.Date.from(dateToConvert.atStartOfDay()
|
||||
.atZone(ZoneId.systemDefault())
|
||||
.toInstant());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.java9.time;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class TimeApi {
|
||||
|
||||
public static List<Date> getDatesBetweenUsingJava7(Date startDate, Date endDate) {
|
||||
List<Date> datesInRange = new ArrayList<Date>();
|
||||
Calendar calendar = new GregorianCalendar();
|
||||
calendar.setTime(startDate);
|
||||
|
||||
Calendar endCalendar = new GregorianCalendar();
|
||||
endCalendar.setTime(endDate);
|
||||
|
||||
while (calendar.before(endCalendar)) {
|
||||
Date result = calendar.getTime();
|
||||
datesInRange.add(result);
|
||||
calendar.add(Calendar.DATE, 1);
|
||||
}
|
||||
return datesInRange;
|
||||
}
|
||||
|
||||
public static List<LocalDate> getDatesBetweenUsingJava8(LocalDate startDate, LocalDate endDate) {
|
||||
long numOfDaysBetween = ChronoUnit.DAYS.between(startDate, endDate);
|
||||
return IntStream.iterate(0, i -> i + 1)
|
||||
.limit(numOfDaysBetween)
|
||||
.mapToObj(i -> startDate.plusDays(i))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static List<LocalDate> getDatesBetweenUsingJava9(LocalDate startDate, LocalDate endDate) {
|
||||
return startDate.datesUntil(endDate).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.regexp.datepattern;
|
||||
|
||||
public interface DateMatcher {
|
||||
|
||||
boolean matches(String date);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.regexp.datepattern;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
class FormattedDateMatcher implements DateMatcher {
|
||||
|
||||
private static final Pattern DATE_PATTERN = Pattern.compile(
|
||||
"^\\d{4}-\\d{2}-\\d{2}$");
|
||||
|
||||
@Override
|
||||
public boolean matches(String date) {
|
||||
return DATE_PATTERN.matcher(date).matches();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.regexp.datepattern;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
class RangedDateMatcher implements DateMatcher {
|
||||
|
||||
private static final Pattern DATE_PATTERN = Pattern.compile(
|
||||
"^((19|2[0-9])[0-9]{2})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$");
|
||||
|
||||
@Override
|
||||
public boolean matches(String date) {
|
||||
return DATE_PATTERN.matcher(date).matches();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.regexp.datepattern.gregorian;
|
||||
|
||||
import com.baeldung.regexp.datepattern.DateMatcher;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class February29thMatcher implements DateMatcher {
|
||||
|
||||
private static final Pattern DATE_PATTERN = Pattern.compile(
|
||||
"^((2000|2400|2800|(19|2[0-9](0[48]|[2468][048]|[13579][26])))-02-29)$");
|
||||
|
||||
@Override
|
||||
public boolean matches(String date) {
|
||||
return DATE_PATTERN.matcher(date).matches();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.regexp.datepattern.gregorian;
|
||||
|
||||
import com.baeldung.regexp.datepattern.DateMatcher;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class FebruaryGeneralMatcher implements DateMatcher {
|
||||
|
||||
private static final Pattern DATE_PATTERN = Pattern.compile(
|
||||
"^(((19|2[0-9])[0-9]{2})-02-(0[1-9]|1[0-9]|2[0-8]))$");
|
||||
|
||||
@Override
|
||||
public boolean matches(String date) {
|
||||
return DATE_PATTERN.matcher(date).matches();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.regexp.datepattern.gregorian;
|
||||
|
||||
import com.baeldung.regexp.datepattern.DateMatcher;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
class GregorianDateMatcher implements DateMatcher {
|
||||
|
||||
private static final Pattern DATE_PATTERN = Pattern.compile(
|
||||
"^((2000|2400|2800|(19|2[0-9](0[48]|[2468][048]|[13579][26])))-02-29)$"
|
||||
+ "|^(((19|2[0-9])[0-9]{2})-02-(0[1-9]|1[0-9]|2[0-8]))$"
|
||||
+ "|^(((19|2[0-9])[0-9]{2})-(0[13578]|10|12)-(0[1-9]|[12][0-9]|3[01]))$"
|
||||
+ "|^(((19|2[0-9])[0-9]{2})-(0[469]|11)-(0[1-9]|[12][0-9]|30))$");
|
||||
|
||||
@Override
|
||||
public boolean matches(String date) {
|
||||
return DATE_PATTERN.matcher(date).matches();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.regexp.datepattern.gregorian;
|
||||
|
||||
import com.baeldung.regexp.datepattern.DateMatcher;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class MonthsOf30DaysMatcher implements DateMatcher {
|
||||
|
||||
private static final Pattern DATE_PATTERN = Pattern.compile(
|
||||
"^(((19|2[0-9])[0-9]{2})-(0[469]|11)-(0[1-9]|[12][0-9]|30))$");
|
||||
|
||||
@Override
|
||||
public boolean matches(String date) {
|
||||
return DATE_PATTERN.matcher(date).matches();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.regexp.datepattern.gregorian;
|
||||
|
||||
import com.baeldung.regexp.datepattern.DateMatcher;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class MonthsOf31DaysMatcher implements DateMatcher {
|
||||
|
||||
private static final Pattern DATE_PATTERN = Pattern.compile(
|
||||
"^(((19|2[0-9])[0-9]{2})-(0[13578]|10|12)-(0[1-9]|[12][0-9]|3[01]))$");
|
||||
|
||||
@Override
|
||||
public boolean matches(String date) {
|
||||
return DATE_PATTERN.matcher(date).matches();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.regexp.datepattern.optmization;
|
||||
|
||||
public class OptimizedMatcher {
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
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) {
|
||||
switch (DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK))) {
|
||||
case FRIDAY:
|
||||
return temporal.plus(3, ChronoUnit.DAYS);
|
||||
case SATURDAY:
|
||||
return temporal.plus(2, ChronoUnit.DAYS);
|
||||
default:
|
||||
return temporal.plus(1, ChronoUnit.DAYS);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user