[BAEL-8456] - Moved Java Date articles into a new module - 'java-dates'
This commit is contained in:
@@ -47,7 +47,6 @@
|
||||
- [How to Get a Name of a Method Being Executed?](http://www.baeldung.com/java-name-of-executing-method)
|
||||
- [Dynamic Proxies in Java](http://www.baeldung.com/java-dynamic-proxies)
|
||||
- [How to Copy an Array in Java](http://www.baeldung.com/java-array-copy)
|
||||
- [Period and Duration in Java](http://www.baeldung.com/java-period-duration)
|
||||
- [Converting a Stack Trace to a String in Java](http://www.baeldung.com/java-stacktrace-to-string)
|
||||
- [Java Double Brace Initialization](http://www.baeldung.com/java-double-brace-initialization)
|
||||
- [The StackOverflowError in Java](http://www.baeldung.com/java-stack-overflow-error)
|
||||
@@ -75,7 +74,6 @@
|
||||
- [A Guide to Java Initialization](http://www.baeldung.com/java-initialization)
|
||||
- [Implementing a Binary Tree in Java](http://www.baeldung.com/java-binary-tree)
|
||||
- [A Guide to ThreadLocalRandom in Java](http://www.baeldung.com/java-thread-local-random)
|
||||
- [RegEx for matching Date Pattern in Java](http://www.baeldung.com/java-date-regular-expressions)
|
||||
- [Nested Classes in Java](http://www.baeldung.com/java-nested-classes)
|
||||
- [A Guide to Java Loops](http://www.baeldung.com/java-loops)
|
||||
- [Varargs in Java](http://www.baeldung.com/java-varargs)
|
||||
@@ -96,7 +94,6 @@
|
||||
- [A Practical Guide to DecimalFormat](http://www.baeldung.com/java-decimalformat)
|
||||
- [How to Detect the OS Using Java](http://www.baeldung.com/java-detect-os)
|
||||
- [ASCII Art in Java](http://www.baeldung.com/ascii-art-in-java)
|
||||
- [Handling Daylight Savings Time in Java](http://www.baeldung.com/java-daylight-savings)
|
||||
- [Inheritance and Composition (Is-a vs Has-a relationship) in Java](http://www.baeldung.com/java-inheritance-composition)
|
||||
- [Finding Max/Min of a List or Collection](http://www.baeldung.com/java-collection-min-max)
|
||||
- [The "final" Keyword in Java](http://www.baeldung.com/java-final)
|
||||
@@ -136,7 +133,6 @@
|
||||
- [Jagged Arrays In Java](http://www.baeldung.com/java-jagged-arrays)
|
||||
- [Importance of Main Manifest Attribute in a Self-Executing JAR](http://www.baeldung.com/java-jar-executable-manifest-main-class)
|
||||
- [Extracting Year, Month and Day from Date in Java](http://www.baeldung.com/java-year-month-day)
|
||||
- [Get Date Without Time in Java](http://www.baeldung.com/java-date-without-time)
|
||||
- [How to Get the File Extension of a File in Java](http://www.baeldung.com/java-file-extension)
|
||||
- [Immutable Objects in Java](http://www.baeldung.com/java-immutable-object)
|
||||
- [Console I/O in Java](http://www.baeldung.com/java-console-input-output)
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.baeldung.regexp.datepattern;
|
||||
|
||||
public interface DateMatcher {
|
||||
|
||||
boolean matches(String date);
|
||||
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package com.baeldung.regexp.datepattern.optmization;
|
||||
|
||||
public class OptimizedMatcher {
|
||||
|
||||
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
package com.baeldung.date;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
public class DateWithoutTimeUnitTest {
|
||||
|
||||
private static final long MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;
|
||||
|
||||
@Test
|
||||
public void whenGettingDateWithoutTimeUsingCalendar_thenReturnDateWithoutTime() {
|
||||
Date dateWithoutTime = DateWithoutTime.getDateWithoutTimeUsingCalendar();
|
||||
|
||||
// first check the time is set to 0
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(dateWithoutTime);
|
||||
|
||||
assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY));
|
||||
assertEquals(0, calendar.get(Calendar.MINUTE));
|
||||
assertEquals(0, calendar.get(Calendar.SECOND));
|
||||
assertEquals(0, calendar.get(Calendar.MILLISECOND));
|
||||
|
||||
// we get the day of the date
|
||||
int day = calendar.get(Calendar.DAY_OF_MONTH);
|
||||
|
||||
// if we add the mills of one day minus 1 we should get the same day
|
||||
calendar.setTimeInMillis(dateWithoutTime.getTime() + MILLISECONDS_PER_DAY - 1);
|
||||
assertEquals(day, calendar.get(Calendar.DAY_OF_MONTH));
|
||||
|
||||
// if we add one full day in millis we should get a different day
|
||||
calendar.setTimeInMillis(dateWithoutTime.getTime() + MILLISECONDS_PER_DAY);
|
||||
assertNotEquals(day, calendar.get(Calendar.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGettingDateWithoutTimeUsingFormat_thenReturnDateWithoutTime() throws ParseException {
|
||||
Date dateWithoutTime = DateWithoutTime.getDateWithoutTimeUsingFormat();
|
||||
|
||||
// first check the time is set to 0
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(dateWithoutTime);
|
||||
|
||||
assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY));
|
||||
assertEquals(0, calendar.get(Calendar.MINUTE));
|
||||
assertEquals(0, calendar.get(Calendar.SECOND));
|
||||
assertEquals(0, calendar.get(Calendar.MILLISECOND));
|
||||
|
||||
// we get the day of the date
|
||||
int day = calendar.get(Calendar.DAY_OF_MONTH);
|
||||
|
||||
// if we add the mills of one day minus 1 we should get the same day
|
||||
calendar.setTimeInMillis(dateWithoutTime.getTime() + MILLISECONDS_PER_DAY - 1);
|
||||
assertEquals(day, calendar.get(Calendar.DAY_OF_MONTH));
|
||||
|
||||
// if we add one full day in millis we should get a different day
|
||||
calendar.setTimeInMillis(dateWithoutTime.getTime() + MILLISECONDS_PER_DAY);
|
||||
assertNotEquals(day, calendar.get(Calendar.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGettingLocalDate_thenReturnDateWithoutTime() {
|
||||
// get the local date
|
||||
LocalDate localDate = DateWithoutTime.getLocalDate();
|
||||
|
||||
// get the millis of our LocalDate
|
||||
long millisLocalDate = localDate
|
||||
.atStartOfDay()
|
||||
.toInstant(OffsetDateTime
|
||||
.now()
|
||||
.getOffset())
|
||||
.toEpochMilli();
|
||||
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
// if we add the millis of one day minus 1 we should get the same day
|
||||
calendar.setTimeInMillis(millisLocalDate + MILLISECONDS_PER_DAY - 1);
|
||||
assertEquals(localDate.getDayOfMonth(), calendar.get(Calendar.DAY_OF_MONTH));
|
||||
|
||||
// if we add one full day in millis we should get a different day
|
||||
calendar.setTimeInMillis(millisLocalDate + MILLISECONDS_PER_DAY);
|
||||
assertNotEquals(localDate.getDayOfMonth(), calendar.get(Calendar.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
package com.baeldung.dst;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DaylightSavingTimeExamplesUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenItalianTimeZone_WhenDSTHappens_ThenCorrectlyShiftTimeZone() throws ParseException {
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Rome"));
|
||||
|
||||
TimeZone tz = TimeZone.getTimeZone("Europe/Rome");
|
||||
Calendar cal = Calendar.getInstance(tz, Locale.ITALIAN);
|
||||
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.ITALIAN);
|
||||
Date dateBeforeDST = df.parse("2018-03-25 01:55");
|
||||
prettyPrint(cal.getTimeZone());
|
||||
|
||||
cal.setTime(dateBeforeDST);
|
||||
System.out.println("Before DST (00:55 UTC - 01:55 GMT+1) = " + dateBeforeDST);
|
||||
|
||||
System.out.println("With this Calendar " + (cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / (60 * 1000) + " minutes must be added to UTC (GMT TimeZone) to get a correct date for this TimeZone\n");
|
||||
assertThat(cal.get(Calendar.ZONE_OFFSET)).isEqualTo(3600000);
|
||||
assertThat(cal.get(Calendar.DST_OFFSET)).isEqualTo(0);
|
||||
|
||||
cal.add(Calendar.MINUTE, 10);
|
||||
|
||||
Date dateAfterDST = cal.getTime();
|
||||
|
||||
System.out.println(" After DST (01:05 UTC - 03:05 GMT+2) = " + dateAfterDST);
|
||||
System.out.println("With this Calendar " + (cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / (60 * 1000) + " minutes must be added to UTC (GMT TimeZone) to get a correct date for this TimeZone\n");
|
||||
assertThat(cal.get(Calendar.DST_OFFSET)).isEqualTo(3600000);
|
||||
assertThat(dateAfterDST).isEqualTo(df.parse("2018-03-25 03:05"));
|
||||
|
||||
Long deltaBetweenDatesInMillis = dateAfterDST.getTime() - dateBeforeDST.getTime();
|
||||
Long tenMinutesInMillis = (1000L * 60 * 10);
|
||||
assertThat(deltaBetweenDatesInMillis).isEqualTo(tenMinutesInMillis);
|
||||
}
|
||||
|
||||
private void prettyPrint(TimeZone tz) {
|
||||
|
||||
//@formatter:off
|
||||
System.out.println(String.format(
|
||||
" Zone ID = %s (%s)\n"
|
||||
+ " RawOffset = %s minutes\n"
|
||||
+ " DST = %s minutes\n"
|
||||
+ " -----------------------------------------",
|
||||
tz.getID(), tz.getDisplayName(), tz.getRawOffset()/60000, tz.getDSTSavings()/60000));
|
||||
//@formatter:on
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void whenIterating_ThenPrintAllTimeZones() {
|
||||
for (String id : TimeZone.getAvailableIDs()) {
|
||||
TimeZone tz = TimeZone.getTimeZone(id);
|
||||
prettyPrint(tz);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
package com.baeldung.dst;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class DaylightSavingTimeJavaTimeExamplesUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenItalianTimeZone_WhenDSTHappens_ThenCorrectlyShiftTimeZone() throws ParseException {
|
||||
ZoneId italianZoneId = ZoneId.of("Europe/Rome");
|
||||
|
||||
LocalDateTime localDateTimeBeforeDST = LocalDateTime.of(2018, 3, 25, 1, 55);
|
||||
System.out.println(localDateTimeBeforeDST);
|
||||
assertThat(localDateTimeBeforeDST.toString()).isEqualTo("2018-03-25T01:55");
|
||||
|
||||
ZonedDateTime zonedDateTimeBeforeDST = localDateTimeBeforeDST.atZone(italianZoneId);
|
||||
prettyPrint(zonedDateTimeBeforeDST);
|
||||
assertThat(zonedDateTimeBeforeDST.toString()).isEqualTo("2018-03-25T01:55+01:00[Europe/Rome]");
|
||||
|
||||
ZonedDateTime zonedDateTimeAfterDST = zonedDateTimeBeforeDST.plus(10, ChronoUnit.MINUTES);
|
||||
prettyPrint(zonedDateTimeAfterDST);
|
||||
assertThat(zonedDateTimeAfterDST.toString()).isEqualTo("2018-03-25T03:05+02:00[Europe/Rome]");
|
||||
|
||||
Long deltaBetweenDatesInMinutes = ChronoUnit.MINUTES.between(zonedDateTimeBeforeDST, zonedDateTimeAfterDST);
|
||||
assertThat(deltaBetweenDatesInMinutes).isEqualTo(10);
|
||||
|
||||
}
|
||||
|
||||
private void prettyPrint(ZonedDateTime zdt) {
|
||||
//@formatter:off
|
||||
System.out.println(String.format(
|
||||
" ZonedDateTime = %s\n"
|
||||
+ " Zone ID = %s (%s)\n"
|
||||
+ " RawOffset = %s minutes\n"
|
||||
+ " -----------------------------------------",
|
||||
zdt, zdt.getZone(), zdt.getZone().getId(), zdt.getOffset().getTotalSeconds()/60));
|
||||
//@formatter:on
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCounting_ThenPrintDifferencesBetweenAPIs() {
|
||||
System.out.println("Total java.time.ZoneId count : " + ZoneId.getAvailableZoneIds()
|
||||
.size());
|
||||
System.out.println("Total java.util.TimeZone Id count : " + TimeZone.getAvailableIDs().length);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.baeldung.regexp.datepattern;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FormattedDateMatcherUnitTest {
|
||||
|
||||
private DateMatcher matcher = new FormattedDateMatcher();
|
||||
|
||||
@Test
|
||||
public void whenUsingFormattedDateMatcher_thenFormatConstraintsSatisfied() {
|
||||
Assert.assertTrue(matcher.matches("2017-12-31"));
|
||||
Assert.assertTrue(matcher.matches("2018-01-01"));
|
||||
Assert.assertTrue(matcher.matches("0000-00-00"));
|
||||
Assert.assertTrue(matcher.matches("1029-99-72"));
|
||||
|
||||
Assert.assertFalse(matcher.matches("2018-01"));
|
||||
Assert.assertFalse(matcher.matches("2018-01-01-01"));
|
||||
Assert.assertFalse(matcher.matches("2018-01-XX"));
|
||||
Assert.assertFalse(matcher.matches(" 2018-01-01"));
|
||||
Assert.assertFalse(matcher.matches("2018-01-01 "));
|
||||
Assert.assertFalse(matcher.matches("2018/01/01"));
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package com.baeldung.regexp.datepattern;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class RangedDateMatcherUnitTest {
|
||||
|
||||
private DateMatcher matcher = new RangedDateMatcher();
|
||||
|
||||
@Test
|
||||
public void whenUsingRangedDateMatcher_thenFormatConstraintsSatisfied() {
|
||||
Assert.assertFalse(matcher.matches("2018-01"));
|
||||
Assert.assertFalse(matcher.matches("2018-01-01-01"));
|
||||
Assert.assertFalse(matcher.matches("2018-01-XX"));
|
||||
Assert.assertFalse(matcher.matches(" 2018-01-01"));
|
||||
Assert.assertFalse(matcher.matches("2018-01-01 "));
|
||||
Assert.assertFalse(matcher.matches("2018/01/01"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingRangedDateMatcher_thenRangeConstraintsSatisfied() {
|
||||
Assert.assertTrue(matcher.matches("1900-01-01"));
|
||||
Assert.assertTrue(matcher.matches("2018-02-31"));
|
||||
Assert.assertTrue(matcher.matches("2999-12-31"));
|
||||
|
||||
Assert.assertFalse(matcher.matches("1899-12-31"));
|
||||
Assert.assertFalse(matcher.matches("2018-05-35"));
|
||||
Assert.assertFalse(matcher.matches("2018-13-05"));
|
||||
Assert.assertFalse(matcher.matches("3000-01-01"));
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.regexp.datepattern.gregorian;
|
||||
|
||||
import com.baeldung.regexp.datepattern.DateMatcher;
|
||||
import com.baeldung.regexp.datepattern.gregorian.testhelper.GregorianDateTestHelper;
|
||||
import org.junit.Test;
|
||||
|
||||
public class February29thMatcherUnitTest {
|
||||
|
||||
private DateMatcher matcher = new February29thMatcher();
|
||||
|
||||
private GregorianDateTestHelper testHelper = new GregorianDateTestHelper(matcher);
|
||||
|
||||
@Test
|
||||
public void whenYearIsLeap_thenYearHasFebruary29th() {
|
||||
testHelper.assertFebruary29th();
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.regexp.datepattern.gregorian;
|
||||
|
||||
import com.baeldung.regexp.datepattern.DateMatcher;
|
||||
import com.baeldung.regexp.datepattern.gregorian.testhelper.GregorianDateTestHelper;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FebruaryGeneralMatcherUnitTest {
|
||||
|
||||
private DateMatcher matcher = new FebruaryGeneralMatcher();
|
||||
|
||||
private GregorianDateTestHelper testHelper = new GregorianDateTestHelper(matcher);
|
||||
|
||||
@Test
|
||||
public void whenMonthIsFebruary_thenMonthContainsUpTo28Days() {
|
||||
testHelper.assertFebruaryGeneralDates();
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package com.baeldung.regexp.datepattern.gregorian;
|
||||
|
||||
import com.baeldung.regexp.datepattern.DateMatcher;
|
||||
import com.baeldung.regexp.datepattern.gregorian.testhelper.GregorianDateTestHelper;
|
||||
import org.junit.Test;
|
||||
|
||||
public class GregorianDateMatcherUnitTest {
|
||||
|
||||
private DateMatcher matcher = new GregorianDateMatcher();
|
||||
|
||||
private GregorianDateTestHelper testHelper = new GregorianDateTestHelper(matcher);
|
||||
|
||||
@Test
|
||||
public void whenUsingGregorianDateMatcher_thenFormatConstraintsSatisfied() {
|
||||
testHelper.assertFormat();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingGregorianDateMatcher_thenRangeConstraintsSatisfied() {
|
||||
testHelper.assertRange();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenYearIsLeap_thenFebruaryHas29Days() {
|
||||
testHelper.assertFebruary29th();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMonthIsFebruary_thenMonthContainsUpTo28Days() {
|
||||
testHelper.assertFebruaryGeneralDates();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMonthIsShort_thenMonthContainsUpTo30Days() {
|
||||
testHelper.assertMonthsOf30Days();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMonthIsLong_thenMonthContainsUpTo31Days() {
|
||||
testHelper.assertMonthsOf31Dates();
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.regexp.datepattern.gregorian;
|
||||
|
||||
import com.baeldung.regexp.datepattern.DateMatcher;
|
||||
import com.baeldung.regexp.datepattern.gregorian.testhelper.GregorianDateTestHelper;
|
||||
import org.junit.Test;
|
||||
|
||||
public class MonthsOf30DaysMatcherUnitTest {
|
||||
|
||||
private DateMatcher matcher = new MonthsOf30DaysMatcher();
|
||||
|
||||
private GregorianDateTestHelper testHelper = new GregorianDateTestHelper(matcher);
|
||||
|
||||
@Test
|
||||
public void whenMonthIsShort_thenMonthContainsUpTo30Days() {
|
||||
testHelper.assertMonthsOf30Days();
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.baeldung.regexp.datepattern.gregorian;
|
||||
|
||||
import com.baeldung.regexp.datepattern.DateMatcher;
|
||||
import com.baeldung.regexp.datepattern.gregorian.testhelper.GregorianDateTestHelper;
|
||||
import org.junit.Test;
|
||||
|
||||
public class MonthsOf31DaysMatcherUnitTest {
|
||||
|
||||
private DateMatcher matcher = new MonthsOf31DaysMatcher();
|
||||
|
||||
private GregorianDateTestHelper testHelper = new GregorianDateTestHelper(matcher);
|
||||
|
||||
@Test
|
||||
public void whenMonthIsLong_thenMonthContainsUpTo31Days() {
|
||||
testHelper.assertMonthsOf31Dates();
|
||||
}
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
package com.baeldung.regexp.datepattern.gregorian.testhelper;
|
||||
|
||||
import com.baeldung.regexp.datepattern.DateMatcher;
|
||||
import org.junit.Assert;
|
||||
|
||||
public class GregorianDateTestHelper {
|
||||
|
||||
private final DateMatcher matcher;
|
||||
|
||||
public GregorianDateTestHelper(DateMatcher matcher) {
|
||||
this.matcher = matcher;
|
||||
}
|
||||
|
||||
public void assertFormat() {
|
||||
Assert.assertTrue(matcher.matches("2017-12-31"));
|
||||
Assert.assertTrue(matcher.matches("2018-01-01"));
|
||||
|
||||
Assert.assertFalse(matcher.matches("2018-02"));
|
||||
Assert.assertFalse(matcher.matches("2018-02-01-01"));
|
||||
Assert.assertFalse(matcher.matches("2018-02-XX"));
|
||||
Assert.assertFalse(matcher.matches(" 2018-02-01"));
|
||||
Assert.assertFalse(matcher.matches("2018-02-01 "));
|
||||
Assert.assertFalse(matcher.matches("2020/02/28"));
|
||||
Assert.assertFalse(matcher.matches("2020.02.29"));
|
||||
}
|
||||
|
||||
public void assertRange() {
|
||||
Assert.assertTrue(matcher.matches("1900-01-01"));
|
||||
Assert.assertTrue(matcher.matches("2205-05-25"));
|
||||
Assert.assertTrue(matcher.matches("2999-12-31"));
|
||||
|
||||
Assert.assertFalse(matcher.matches("1899-12-31"));
|
||||
Assert.assertFalse(matcher.matches("2018-05-35"));
|
||||
Assert.assertFalse(matcher.matches("2018-13-05"));
|
||||
Assert.assertFalse(matcher.matches("3000-01-01"));
|
||||
Assert.assertFalse(matcher.matches("3200-02-29"));
|
||||
}
|
||||
|
||||
public void assertFebruary29th() {
|
||||
Assert.assertTrue(matcher.matches("2000-02-29"));
|
||||
Assert.assertTrue(matcher.matches("2400-02-29"));
|
||||
Assert.assertTrue(matcher.matches("2800-02-29"));
|
||||
Assert.assertTrue(matcher.matches("2020-02-29"));
|
||||
Assert.assertTrue(matcher.matches("2024-02-29"));
|
||||
Assert.assertTrue(matcher.matches("2028-02-29"));
|
||||
|
||||
Assert.assertFalse(matcher.matches("2017-02-29"));
|
||||
Assert.assertFalse(matcher.matches("2018-02-29"));
|
||||
Assert.assertFalse(matcher.matches("2019-02-29"));
|
||||
Assert.assertFalse(matcher.matches("2100-02-29"));
|
||||
Assert.assertFalse(matcher.matches("2200-02-29"));
|
||||
Assert.assertFalse(matcher.matches("2300-02-29"));
|
||||
}
|
||||
|
||||
public void assertFebruaryGeneralDates() {
|
||||
Assert.assertTrue(matcher.matches("2018-02-01"));
|
||||
Assert.assertTrue(matcher.matches("2019-02-13"));
|
||||
Assert.assertTrue(matcher.matches("2020-02-25"));
|
||||
|
||||
Assert.assertFalse(matcher.matches("2000-02-30"));
|
||||
Assert.assertFalse(matcher.matches("2400-02-62"));
|
||||
Assert.assertFalse(matcher.matches("2420-02-94"));
|
||||
}
|
||||
|
||||
public void assertMonthsOf30Days() {
|
||||
Assert.assertTrue(matcher.matches("2018-04-30"));
|
||||
Assert.assertTrue(matcher.matches("2019-06-30"));
|
||||
Assert.assertTrue(matcher.matches("2020-09-30"));
|
||||
Assert.assertTrue(matcher.matches("2021-11-30"));
|
||||
|
||||
Assert.assertTrue(matcher.matches("2022-04-02"));
|
||||
Assert.assertTrue(matcher.matches("2023-06-14"));
|
||||
Assert.assertTrue(matcher.matches("2024-09-26"));
|
||||
|
||||
Assert.assertFalse(matcher.matches("2018-04-31"));
|
||||
Assert.assertFalse(matcher.matches("2019-06-31"));
|
||||
Assert.assertFalse(matcher.matches("2020-09-31"));
|
||||
Assert.assertFalse(matcher.matches("2021-11-31"));
|
||||
|
||||
Assert.assertFalse(matcher.matches("2022-04-32"));
|
||||
Assert.assertFalse(matcher.matches("2023-06-64"));
|
||||
Assert.assertFalse(matcher.matches("2024-09-96"));
|
||||
}
|
||||
|
||||
public void assertMonthsOf31Dates() {
|
||||
Assert.assertTrue(matcher.matches("2018-01-31"));
|
||||
Assert.assertTrue(matcher.matches("2019-03-31"));
|
||||
Assert.assertTrue(matcher.matches("2020-05-31"));
|
||||
Assert.assertTrue(matcher.matches("2021-07-31"));
|
||||
Assert.assertTrue(matcher.matches("2022-08-31"));
|
||||
Assert.assertTrue(matcher.matches("2023-10-31"));
|
||||
Assert.assertTrue(matcher.matches("2024-12-31"));
|
||||
|
||||
Assert.assertTrue(matcher.matches("2025-01-03"));
|
||||
Assert.assertTrue(matcher.matches("2026-03-15"));
|
||||
Assert.assertTrue(matcher.matches("2027-05-27"));
|
||||
|
||||
Assert.assertFalse(matcher.matches("2018-01-32"));
|
||||
Assert.assertFalse(matcher.matches("2019-03-64"));
|
||||
Assert.assertFalse(matcher.matches("2020-05-96"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user