#BAEL-16646 flatten java-datetime modules
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.date.validation;
|
||||
|
||||
public interface DateValidator {
|
||||
boolean isValid(String dateStr);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.date.validation;
|
||||
|
||||
import org.apache.commons.validator.GenericValidator;
|
||||
|
||||
public class DateValidatorUsingApacheValidator implements DateValidator {
|
||||
|
||||
@Override
|
||||
public boolean isValid(String dateStr) {
|
||||
return GenericValidator.isDate(dateStr, "yyyy-MM-dd", true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.date.validation;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
public class DateValidatorUsingDateFormat implements DateValidator {
|
||||
private String dateFormat;
|
||||
|
||||
public DateValidatorUsingDateFormat(String dateFormat) {
|
||||
this.dateFormat = dateFormat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(String dateStr) {
|
||||
DateFormat sdf = new SimpleDateFormat(this.dateFormat);
|
||||
sdf.setLenient(false);
|
||||
try {
|
||||
sdf.parse(dateStr);
|
||||
} catch (ParseException e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.date.validation;
|
||||
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
|
||||
public class DateValidatorUsingDateTimeFormatter implements DateValidator {
|
||||
private DateTimeFormatter dateFormatter;
|
||||
|
||||
public DateValidatorUsingDateTimeFormatter(DateTimeFormatter dateFormatter) {
|
||||
this.dateFormatter = dateFormatter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(String dateStr) {
|
||||
try {
|
||||
this.dateFormatter.parse(dateStr);
|
||||
} catch (DateTimeParseException e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.date.validation;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
|
||||
public class DateValidatorUsingLocalDate implements DateValidator {
|
||||
private DateTimeFormatter dateFormatter;
|
||||
|
||||
public DateValidatorUsingLocalDate(DateTimeFormatter dateFormatter) {
|
||||
this.dateFormatter = dateFormatter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(String dateStr) {
|
||||
try {
|
||||
LocalDate.parse(dateStr, this.dateFormatter);
|
||||
} catch (DateTimeParseException e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -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,55 @@
|
||||
package com.baeldung.timezonedisplay;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class TimezoneDisplay {
|
||||
|
||||
public enum OffsetBase {
|
||||
GMT, UTC
|
||||
}
|
||||
|
||||
public List<String> getTimeZoneList(OffsetBase base) {
|
||||
Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
|
||||
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
return availableZoneIds
|
||||
.stream()
|
||||
.map(ZoneId::of)
|
||||
.sorted(new ZoneComparator())
|
||||
.map(id -> String.format("(%s%s) %s", base, getOffset(now, id), id.getId()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private String getOffset(LocalDateTime dateTime, ZoneId id) {
|
||||
return dateTime
|
||||
.atZone(id)
|
||||
.getOffset()
|
||||
.getId()
|
||||
.replace("Z", "+00:00");
|
||||
}
|
||||
|
||||
private class ZoneComparator implements Comparator<ZoneId> {
|
||||
|
||||
@Override
|
||||
public int compare(ZoneId zoneId1, ZoneId zoneId2) {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
|
||||
ZoneOffset offset1 = now
|
||||
.atZone(zoneId1)
|
||||
.getOffset();
|
||||
|
||||
ZoneOffset offset2 = now
|
||||
.atZone(zoneId2)
|
||||
.getOffset();
|
||||
|
||||
return offset1.compareTo(offset2);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.timezonedisplay;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TimezoneDisplayApp {
|
||||
|
||||
public static void main(String... args) {
|
||||
TimezoneDisplay display = new TimezoneDisplay();
|
||||
|
||||
System.out.println("Time zones in UTC:");
|
||||
List<String> utc = display.getTimeZoneList(TimezoneDisplay.OffsetBase.UTC);
|
||||
utc.forEach(System.out::println);
|
||||
|
||||
System.out.println("Time zones in GMT:");
|
||||
List<String> gmt = display.getTimeZoneList(TimezoneDisplay.OffsetBase.GMT);
|
||||
gmt.forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.timezonedisplay;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.TimeZone;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class TimezoneDisplayJava7 {
|
||||
|
||||
public enum OffsetBase {
|
||||
GMT, UTC
|
||||
}
|
||||
|
||||
public List<String> getTimeZoneList(TimezoneDisplayJava7.OffsetBase base) {
|
||||
String[] availableZoneIds = TimeZone.getAvailableIDs();
|
||||
List<String> result = new ArrayList<>(availableZoneIds.length);
|
||||
|
||||
for (String zoneId : availableZoneIds) {
|
||||
TimeZone curTimeZone = TimeZone.getTimeZone(zoneId);
|
||||
|
||||
String offset = calculateOffset(curTimeZone.getRawOffset());
|
||||
|
||||
result.add(String.format("(%s%s) %s", base, offset, zoneId));
|
||||
}
|
||||
|
||||
Collections.sort(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private String calculateOffset(int rawOffset) {
|
||||
if (rawOffset == 0) {
|
||||
return "+00:00";
|
||||
}
|
||||
long hours = TimeUnit.MILLISECONDS.toHours(rawOffset);
|
||||
long minutes = TimeUnit.MILLISECONDS.toMinutes(rawOffset);
|
||||
minutes = Math.abs(minutes - TimeUnit.HOURS.toMinutes(hours));
|
||||
|
||||
return String.format("%+03d:%02d", hours, Math.abs(minutes));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.timezonedisplay;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TimezoneDisplayJava7App {
|
||||
|
||||
public static void main(String... args) {
|
||||
TimezoneDisplayJava7 display = new TimezoneDisplayJava7();
|
||||
|
||||
System.out.println("Time zones in UTC:");
|
||||
List<String> utc = display.getTimeZoneList(TimezoneDisplayJava7.OffsetBase.UTC);
|
||||
for (String timeZone : utc) {
|
||||
System.out.println(timeZone);
|
||||
}
|
||||
|
||||
System.out.println("Time zones in GMT:");
|
||||
List<String> gmt = display.getTimeZoneList(TimezoneDisplayJava7.OffsetBase.GMT);
|
||||
for (String timeZone : gmt) {
|
||||
System.out.println(timeZone);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.zonedatetime;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
|
||||
public class OffsetDateTimeExample {
|
||||
|
||||
public OffsetDateTime getCurrentTimeByZoneOffset(String offset) {
|
||||
ZoneOffset zoneOffSet= ZoneOffset.of(offset);
|
||||
OffsetDateTime date = OffsetDateTime.now(zoneOffSet);
|
||||
return date;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.zonedatetime;
|
||||
|
||||
import java.time.OffsetTime;
|
||||
import java.time.ZoneOffset;
|
||||
|
||||
public class OffsetTimeExample {
|
||||
|
||||
public OffsetTime getCurrentTimeByZoneOffset(String offset) {
|
||||
ZoneOffset zoneOffSet = ZoneOffset.of(offset);
|
||||
OffsetTime time = OffsetTime.now(zoneOffSet);
|
||||
return time;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.zonedatetime;
|
||||
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
public class ZoneDateTimeExample {
|
||||
|
||||
public ZonedDateTime getCurrentTimeByZoneId(String region) {
|
||||
ZoneId zone = ZoneId.of(region);
|
||||
ZonedDateTime date = ZonedDateTime.now(zone);
|
||||
return date;
|
||||
}
|
||||
|
||||
public ZonedDateTime convertZonedDateTime(ZonedDateTime sourceDate, String destZone) {
|
||||
|
||||
ZoneId destZoneId = ZoneId.of(destZone);
|
||||
ZonedDateTime destDate = sourceDate.withZoneSameInstant(destZoneId);
|
||||
|
||||
return destDate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package com.baeldung.date;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.apache.commons.lang3.time.DateUtils;
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
public class StringToDateUnitTest {
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void givenDateString_whenConvertedToDate_thenWeGetCorrectLocalDate() {
|
||||
LocalDate expectedLocalDate = LocalDate.of(2018, 05, 05);
|
||||
|
||||
LocalDate date = LocalDate.parse("2018-05-05");
|
||||
|
||||
assertThat(date).isEqualTo(expectedLocalDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateString_whenConvertedToDate_thenWeGetCorrectLocalDateTime() {
|
||||
LocalDateTime expectedLocalDateTime = LocalDateTime.of(2018, 05, 05, 11, 50, 55);
|
||||
|
||||
LocalDateTime dateTime = LocalDateTime.parse("2018-05-05T11:50:55");
|
||||
|
||||
assertThat(dateTime).isEqualTo(expectedLocalDateTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateString_whenConvertedToDate_thenWeGetDateTimeParseException() {
|
||||
thrown.expect(DateTimeParseException.class);
|
||||
thrown.expectMessage("Text '2018-05-05' could not be parsed at index 10");
|
||||
|
||||
LocalDateTime.parse("2018-05-05");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateString_whenConvertedToDate_thenWeGetCorrectZonedDateTime() {
|
||||
LocalDateTime localDateTime = LocalDateTime.of(2015, 05, 05, 10, 15, 30);
|
||||
ZonedDateTime expectedZonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("Europe/Paris"));
|
||||
|
||||
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2015-05-05T10:15:30+01:00[Europe/Paris]");
|
||||
|
||||
assertThat(zonedDateTime).isEqualTo(expectedZonedDateTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateString_whenConvertedToDateUsingFormatter_thenWeGetCorrectLocalDate() {
|
||||
LocalDate expectedLocalDate = LocalDate.of(1959, 7, 9);
|
||||
|
||||
String dateInString = "19590709";
|
||||
LocalDate date = LocalDate.parse(dateInString, DateTimeFormatter.BASIC_ISO_DATE);
|
||||
|
||||
assertThat(date).isEqualTo(expectedLocalDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateString_whenConvertedToDateUsingCustomFormatter_thenWeGetCorrectLocalDate() {
|
||||
LocalDate expectedLocalDate = LocalDate.of(1980, 05, 05);
|
||||
|
||||
String dateInString = "Mon, 05 May 1980";
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE, d MMM yyyy", Locale.ENGLISH);
|
||||
LocalDate dateTime = LocalDate.parse(dateInString, formatter);
|
||||
|
||||
assertThat(dateTime).isEqualTo(expectedLocalDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateString_whenConvertedToDate_thenWeGetCorrectDate() throws ParseException {
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
|
||||
|
||||
String dateInString = "7-Jun-2013";
|
||||
Date date = formatter.parse(dateInString);
|
||||
|
||||
assertDateIsCorrect(date);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateString_whenConvertedToDate_thenWeGetParseException() throws ParseException {
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
|
||||
|
||||
thrown.expect(ParseException.class);
|
||||
thrown.expectMessage("Unparseable date: \"07/06/2013\"");
|
||||
|
||||
String dateInString = "07/06/2013";
|
||||
formatter.parse(dateInString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateString_whenConvertedToDate_thenWeGetCorrectJodaDateTime() {
|
||||
org.joda.time.format.DateTimeFormatter formatter = org.joda.time.format.DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
|
||||
|
||||
String dateInString = "07/06/2013 10:11:59";
|
||||
DateTime dateTime = DateTime.parse(dateInString, formatter);
|
||||
|
||||
assertEquals("Day of Month should be 7: ", 7, dateTime.getDayOfMonth());
|
||||
assertEquals("Month should be: ", 6, dateTime.getMonthOfYear());
|
||||
assertEquals("Year should be: ", 2013, dateTime.getYear());
|
||||
|
||||
assertEquals("Hour of day should be: ", 10, dateTime.getHourOfDay());
|
||||
assertEquals("Minutes of hour should be: ", 11, dateTime.getMinuteOfHour());
|
||||
assertEquals("Seconds of minute should be: ", 59, dateTime.getSecondOfMinute());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateString_whenConvertedToDate_thenWeGetCorrectDateTime() throws ParseException {
|
||||
String dateInString = "07/06-2013";
|
||||
Date date = DateUtils.parseDate(dateInString, new String[] { "yyyy-MM-dd HH:mm:ss", "dd/MM-yyyy" });
|
||||
|
||||
assertDateIsCorrect(date);
|
||||
}
|
||||
|
||||
private void assertDateIsCorrect(Date date) {
|
||||
Calendar calendar = new GregorianCalendar(Locale.ENGLISH);
|
||||
calendar.setTime(date);
|
||||
|
||||
assertEquals("Day of Month should be 7: ", 7, calendar.get(Calendar.DAY_OF_MONTH));
|
||||
assertEquals("Month should be: ", 5, calendar.get(Calendar.MONTH));
|
||||
assertEquals("Year should be: ", 2013, calendar.get(Calendar.YEAR));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.date.validation;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.apache.commons.validator.GenericValidator;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DateValidatorUsingApacheValidatorUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenValidDatePassed_ThenTrue() {
|
||||
assertTrue(GenericValidator.isDate("2019-02-28", "yyyy-MM-dd", true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInvalidDatePassed_ThenFalse() {
|
||||
assertFalse(GenericValidator.isDate("2019-02-29", "yyyy-MM-dd", true));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.date.validation;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class DateValidatorUsingDateFormatUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenValidator_whenValidDatePassed_ThenTrue() {
|
||||
DateValidator validator = new DateValidatorUsingDateFormat("MM/dd/yyyy");
|
||||
|
||||
assertTrue(validator.isValid("02/28/2019"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidator_whenInvalidDatePassed_ThenFalse() {
|
||||
DateValidator validator = new DateValidatorUsingDateFormat("MM/dd/yyyy");
|
||||
|
||||
assertFalse(validator.isValid("02/30/2019"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.date.validation;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.ResolverStyle;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class DateValidatorUsingDateTimeFormatterUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenValidator_whenValidDatePassed_ThenTrue() {
|
||||
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd", Locale.US)
|
||||
.withResolverStyle(ResolverStyle.STRICT);
|
||||
DateValidator validator = new DateValidatorUsingDateTimeFormatter(dateFormatter);
|
||||
|
||||
assertTrue(validator.isValid("2019-02-28"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidator_whenInValidDatePassed_ThenFalse() {
|
||||
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd", Locale.US)
|
||||
.withResolverStyle(ResolverStyle.STRICT);
|
||||
DateValidator validator = new DateValidatorUsingDateTimeFormatter(dateFormatter);
|
||||
|
||||
assertFalse(validator.isValid("2019-02-30"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.date.validation;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class DateValidatorUsingLocalDateUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenValidator_whenValidDatePassed_ThenTrue() {
|
||||
DateTimeFormatter dateFormatter = DateTimeFormatter.BASIC_ISO_DATE;
|
||||
DateValidator validator = new DateValidatorUsingLocalDate(dateFormatter);
|
||||
|
||||
assertTrue(validator.isValid("20190228"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidator_whenInValidDatePassed_ThenFalse() {
|
||||
DateTimeFormatter dateFormatter = DateTimeFormatter.BASIC_ISO_DATE;
|
||||
DateValidator validator = new DateValidatorUsingLocalDate(dateFormatter);
|
||||
|
||||
assertFalse(validator.isValid("20190230"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.time.format.FormatStyle;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class DateTimeFormatterUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDefaultUsLocaleAndDateTimeAndPattern_whenFormatWithDifferentLocales_thenGettingLocalizedDateTimes() {
|
||||
Locale.setDefault(Locale.US);
|
||||
LocalDateTime localDateTime = LocalDateTime.of(2018, 1, 1, 10, 15, 50, 500);
|
||||
String pattern = "dd-MMMM-yyyy HH:mm:ss.SSS";
|
||||
|
||||
DateTimeFormatter defaultTimeFormatter = DateTimeFormatter.ofPattern(pattern);
|
||||
DateTimeFormatter plTimeFormatter = DateTimeFormatter.ofPattern(pattern, new Locale("pl", "PL"));
|
||||
DateTimeFormatter deTimeFormatter = DateTimeFormatter.ofPattern(pattern).withLocale(Locale.GERMANY);
|
||||
|
||||
Assert.assertEquals("01-January-2018 10:15:50.000", defaultTimeFormatter.format(localDateTime));
|
||||
Assert.assertEquals("01-stycznia-2018 10:15:50.000", plTimeFormatter.format(localDateTime));
|
||||
Assert.assertEquals("01-Januar-2018 10:15:50.000", deTimeFormatter.format(localDateTime));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateTimeAndTimeZone_whenFormatWithDifferentLocales_thenGettingLocalizedZonedDateTimes() {
|
||||
Locale.setDefault(Locale.US);
|
||||
LocalDateTime localDateTime = LocalDateTime.of(2018, 1, 1, 10, 15, 50, 500);
|
||||
ZoneId losAngelesTimeZone = TimeZone.getTimeZone("America/Los_Angeles").toZoneId();
|
||||
|
||||
DateTimeFormatter localizedFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
|
||||
DateTimeFormatter frLocalizedFormatter =
|
||||
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withLocale(Locale.FRANCE);
|
||||
String formattedDateTime = localizedFormatter.format(ZonedDateTime.of(localDateTime, losAngelesTimeZone));
|
||||
String frFormattedDateTime = frLocalizedFormatter.format(ZonedDateTime.of(localDateTime, losAngelesTimeZone));
|
||||
|
||||
Assert.assertEquals("Monday, January 1, 2018 10:15:50 AM PST", formattedDateTime);
|
||||
Assert.assertEquals("lundi 1 janvier 2018 10 h 15 PST", frFormattedDateTime);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPrintFormattedDate() {
|
||||
String europeanDatePattern = "dd.MM.yyyy";
|
||||
DateTimeFormatter europeanDateFormatter = DateTimeFormatter.ofPattern(europeanDatePattern);
|
||||
LocalDate summerDay = LocalDate.of(2016, 7, 31);
|
||||
Assert.assertEquals("31.07.2016", europeanDateFormatter.format(summerDay));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPrintFormattedTime24() {
|
||||
String timeColonPattern = "HH:mm:ss";
|
||||
DateTimeFormatter timeColonFormatter = DateTimeFormatter.ofPattern(timeColonPattern);
|
||||
LocalTime colonTime = LocalTime.of(17, 35, 50);
|
||||
Assert.assertEquals("17:35:50", timeColonFormatter.format(colonTime));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPrintFormattedTimeWithMillis() {
|
||||
String timeColonPattern = "HH:mm:ss SSS";
|
||||
DateTimeFormatter timeColonFormatter = DateTimeFormatter.ofPattern(timeColonPattern);
|
||||
LocalTime colonTime = LocalTime.of(17, 35, 50).plus(329, ChronoUnit.MILLIS);
|
||||
Assert.assertEquals("17:35:50 329", timeColonFormatter.format(colonTime));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPrintFormattedTimePM() {
|
||||
String timeColonPattern = "hh:mm:ss a";
|
||||
DateTimeFormatter timeColonFormatter = DateTimeFormatter.ofPattern(timeColonPattern);
|
||||
LocalTime colonTime = LocalTime.of(17, 35, 50);
|
||||
Assert.assertEquals("05:35:50 PM", timeColonFormatter.format(colonTime));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPrintFormattedUTCRelatedZonedDateTime() {
|
||||
String newYorkDateTimePattern = "dd.MM.yyyy HH:mm z";
|
||||
DateTimeFormatter newYorkDateFormatter = DateTimeFormatter.ofPattern(newYorkDateTimePattern);
|
||||
LocalDateTime summerDay = LocalDateTime.of(2016, 7, 31, 14, 15);
|
||||
Assert.assertEquals("31.07.2016 14:15 UTC-04:00", newYorkDateFormatter.format(ZonedDateTime.of(summerDay, ZoneId.of("UTC-4"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPrintFormattedNewYorkZonedDateTime() {
|
||||
String newYorkDateTimePattern = "dd.MM.yyyy HH:mm z";
|
||||
DateTimeFormatter newYorkDateFormatter = DateTimeFormatter.ofPattern(newYorkDateTimePattern);
|
||||
LocalDateTime summerDay = LocalDateTime.of(2016, 7, 31, 14, 15);
|
||||
Assert.assertEquals("31.07.2016 14:15 EDT", newYorkDateFormatter.format(ZonedDateTime.of(summerDay, ZoneId.of("America/New_York"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPrintStyledDate() {
|
||||
LocalDate anotherSummerDay = LocalDate.of(2016, 8, 23);
|
||||
Assert.assertEquals("Tuesday, August 23, 2016", DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).format(anotherSummerDay));
|
||||
Assert.assertEquals("August 23, 2016", DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).format(anotherSummerDay));
|
||||
Assert.assertEquals("Aug 23, 2016", DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).format(anotherSummerDay));
|
||||
Assert.assertEquals("8/23/16", DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(anotherSummerDay));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPrintStyledDateTime() {
|
||||
LocalDateTime anotherSummerDay = LocalDateTime.of(2016, 8, 23, 13, 12, 45);
|
||||
Assert.assertEquals("Tuesday, August 23, 2016 1:12:45 PM EET", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay));
|
||||
Assert.assertEquals("August 23, 2016 1:12:45 PM EET", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay));
|
||||
Assert.assertEquals("Aug 23, 2016 1:12:45 PM", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay));
|
||||
Assert.assertEquals("8/23/16 1:12 PM", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPrintFormattedDateTimeWithPredefined() {
|
||||
Assert.assertEquals("2018-03-09", DateTimeFormatter.ISO_LOCAL_DATE.format(LocalDate.of(2018, 3, 9)));
|
||||
Assert.assertEquals("2018-03-09-03:00", DateTimeFormatter.ISO_OFFSET_DATE.format(LocalDate.of(2018, 3, 9).atStartOfDay(ZoneId.of("UTC-3"))));
|
||||
Assert.assertEquals("Fri, 9 Mar 2018 00:00:00 -0300", DateTimeFormatter.RFC_1123_DATE_TIME.format(LocalDate.of(2018, 3, 9).atStartOfDay(ZoneId.of("UTC-3"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldParseDateTime() {
|
||||
Assert.assertEquals(LocalDate.of(2018, 3, 12), LocalDate.from(DateTimeFormatter.ISO_LOCAL_DATE.parse("2018-03-09")).plusDays(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldParseFormatStyleFull() {
|
||||
ZonedDateTime dateTime = ZonedDateTime.from(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).parse("Tuesday, August 23, 2016 1:12:45 PM EET"));
|
||||
Assert.assertEquals(ZonedDateTime.of(LocalDateTime.of(2016, 8, 23, 22, 12, 45), ZoneId.of("Europe/Bucharest")), dateTime.plusHours(9));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldParseDateWithCustomFormatter() {
|
||||
DateTimeFormatter europeanDateFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
|
||||
Assert.assertFalse(LocalDate.from(europeanDateFormatter.parse("15.08.2014")).isLeapYear());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldParseTimeWithCustomFormatter() {
|
||||
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss a");
|
||||
Assert.assertTrue(LocalTime.from(timeFormatter.parse("12:25:30 AM")).isBefore(LocalTime.NOON));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldParseZonedDateTimeWithCustomFormatter() {
|
||||
DateTimeFormatter zonedFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm z");
|
||||
Assert.assertEquals(7200, ZonedDateTime.from(zonedFormatter.parse("31.07.2016 14:15 GMT+02:00")).getOffset().getTotalSeconds());
|
||||
}
|
||||
|
||||
@Test(expected = DateTimeParseException.class)
|
||||
public void shouldExpectAnExceptionIfDateTimeStringNotMatchPattern() {
|
||||
DateTimeFormatter zonedFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm z");
|
||||
ZonedDateTime.from(zonedFormatter.parse("31.07.2016 14:15"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
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"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
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"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
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"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.simpledateformat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
|
||||
public class SimpleDateFormatUnitTest {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(SimpleDateFormatUnitTest.class.getName());
|
||||
|
||||
@Test
|
||||
public void givenSpecificDate_whenFormatted_thenCheckFormatCorrect() throws Exception {
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
|
||||
assertEquals("24-05-1977", formatter.format(new Date(233345223232L)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSpecificDate_whenFormattedUsingDateFormat_thenCheckFormatCorrect() throws Exception {
|
||||
DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
|
||||
assertEquals("5/24/77", formatter.format(new Date(233345223232L)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringDate_whenParsed_thenCheckDateCorrect() throws Exception{
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
|
||||
formatter.setTimeZone(TimeZone.getTimeZone("Europe/London"));
|
||||
Date myDate = new Date(233276400000L);
|
||||
Date parsedDate = formatter.parse("24-05-1977");
|
||||
assertEquals(myDate.getTime(), parsedDate.getTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFranceLocale_whenFormatted_thenCheckFormatCorrect() throws Exception{
|
||||
SimpleDateFormat franceDateFormatter = new SimpleDateFormat("EEEEE dd-MMMMMMM-yyyy", Locale.FRANCE);
|
||||
Date myWednesday = new Date(1539341312904L);
|
||||
assertTrue(franceDateFormatter.format(myWednesday).startsWith("vendredi"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given2TimeZones_whenFormatted_thenCheckTimeDifference() throws Exception {
|
||||
Date now = new Date();
|
||||
|
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE dd-MMM-yy HH:mm:ssZ");
|
||||
|
||||
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Europe/London"));
|
||||
logger.info(simpleDateFormat.format(now));
|
||||
//change the date format
|
||||
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
|
||||
logger.info(simpleDateFormat.format(now));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.timestamp;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
public class StringToTimestampConverterUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDatePattern_whenParsing_thenTimestampIsCorrect() {
|
||||
String pattern = "MMM dd, yyyy HH:mm:ss.SSSSSSSS";
|
||||
String timestampAsString = "Nov 12, 2018 13:02:56.12345678";
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
|
||||
LocalDateTime localDateTime = LocalDateTime.from(formatter.parse(timestampAsString));
|
||||
|
||||
Timestamp timestamp = Timestamp.valueOf(localDateTime);
|
||||
Assert.assertEquals("2018-11-12 13:02:56.12345678", timestamp.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.timestamp;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
public class TimestampToStringConverterTest {
|
||||
|
||||
@Test
|
||||
public void givenDatePattern_whenFormatting_thenResultingStringIsCorrect() {
|
||||
Timestamp timestamp = Timestamp.valueOf("2018-12-12 01:02:03.123456789");
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
|
||||
|
||||
String timestampAsString = formatter.format(timestamp.toLocalDateTime());
|
||||
Assert.assertEquals("2018-12-12T01:02:03.123456789", timestampAsString);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.zonedatetime;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class OffsetDateTimeExampleUnitTest {
|
||||
|
||||
OffsetDateTimeExample offsetDateTimeExample = new OffsetDateTimeExample();
|
||||
|
||||
@Test
|
||||
public void givenZoneOffset_whenGetCurrentTime_thenResultHasZone() {
|
||||
String offset = "+02:00";
|
||||
OffsetDateTime time = offsetDateTimeExample.getCurrentTimeByZoneOffset(offset);
|
||||
|
||||
assertTrue(time.getOffset()
|
||||
.equals(ZoneOffset.of(offset)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.zonedatetime;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.time.OffsetTime;
|
||||
import java.time.ZoneOffset;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class OffsetTimeExampleUnitTest {
|
||||
|
||||
OffsetTimeExample offsetTimeExample = new OffsetTimeExample();
|
||||
|
||||
@Test
|
||||
public void givenZoneOffset_whenGetCurrentTime_thenResultHasZone() {
|
||||
String offset = "+02:00";
|
||||
OffsetTime time = offsetTimeExample.getCurrentTimeByZoneOffset(offset);
|
||||
|
||||
assertTrue(time.getOffset()
|
||||
.equals(ZoneOffset.of(offset)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.zonedatetime;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ZoneDateTimeExampleUnitTest {
|
||||
|
||||
ZoneDateTimeExample zoneDateTimeExample = new ZoneDateTimeExample();
|
||||
|
||||
@Test
|
||||
public void givenZone_whenGetCurrentTime_thenResultHasZone() {
|
||||
String zone = "Europe/Berlin";
|
||||
ZonedDateTime time = zoneDateTimeExample.getCurrentTimeByZoneId(zone);
|
||||
|
||||
assertTrue(time.getZone()
|
||||
.equals(ZoneId.of(zone)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenZones_whenConvertDateByZone_thenGetConstantDiff() {
|
||||
String sourceZone = "Europe/Berlin";
|
||||
String destZone = "Asia/Tokyo";
|
||||
ZonedDateTime sourceDate = zoneDateTimeExample.getCurrentTimeByZoneId(sourceZone);
|
||||
ZonedDateTime destDate = zoneDateTimeExample.convertZonedDateTime(sourceDate, destZone);
|
||||
|
||||
assertTrue(sourceDate.toInstant()
|
||||
.compareTo(destDate.toInstant()) == 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.zonedatetime;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ZonedDateTimeUnitTest {
|
||||
|
||||
private static final Logger log = Logger.getLogger(ZonedDateTimeUnitTest.class.getName());
|
||||
|
||||
@Test
|
||||
public void givenZonedDateTime_whenConvertToString_thenOk() {
|
||||
|
||||
ZonedDateTime zonedDateTimeNow = ZonedDateTime.now(ZoneId.of("UTC"));
|
||||
ZonedDateTime zonedDateTimeOf = ZonedDateTime.of(2018, 01, 01, 0, 0, 0, 0, ZoneId.of("UTC"));
|
||||
|
||||
LocalDateTime localDateTime = LocalDateTime.now();
|
||||
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("UTC"));
|
||||
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy - HH:mm:ss Z");
|
||||
String formattedString = zonedDateTime.format(formatter);
|
||||
|
||||
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM/dd/yyyy - HH:mm:ss z");
|
||||
String formattedString2 = zonedDateTime.format(formatter2);
|
||||
|
||||
log.info(formattedString);
|
||||
log.info(formattedString2);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenParseZonedDateTime_thenOk() {
|
||||
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2011-12-03T10:15:30+01:00");
|
||||
|
||||
log.info(zonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenParseZonedDateTimeWithoutZone_thenException() {
|
||||
assertThrows(DateTimeParseException.class, () -> ZonedDateTime.parse("2011-12-03T10:15:30", DateTimeFormatter.ISO_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_whenParseLocalDateTimeAtZone_thenOk() {
|
||||
ZoneId timeZone = ZoneId.systemDefault();
|
||||
ZonedDateTime zonedDateTime = LocalDateTime.parse("2011-12-03T10:15:30", DateTimeFormatter.ISO_DATE_TIME).atZone(timeZone);
|
||||
|
||||
log.info(zonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user