Split the first 2 modules: java-dates-conversion and java-dates-string.
This commit is contained in:
@@ -1,39 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.datetolocaldate;
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.datetolocaldate;
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.datetolocaldate;
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.datetolocaldate;
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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,55 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
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));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.zoneddatetime;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.zoneddatetime;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.baeldung.zoneddatetime;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ConvertInstantToTimestampUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenInstant_whenConvertedToTimestamp_thenGetTimestampWithSamePointOnTimeline() {
|
||||
Instant instant = Instant.now();
|
||||
Timestamp timestamp = Timestamp.from(instant); // same point on the time-line as Instant
|
||||
assertThat(instant.toEpochMilli()).isEqualTo(timestamp.getTime());
|
||||
|
||||
instant = timestamp.toInstant();
|
||||
assertThat(instant.toEpochMilli()).isEqualTo(timestamp.getTime());
|
||||
|
||||
DateFormat df = DateFormat.getDateTimeInstance();
|
||||
df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS'Z'");
|
||||
df.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
assertThat(instant.toString()).isEqualTo(df.format(timestamp).toString());
|
||||
}
|
||||
}
|
||||
@@ -1,158 +0,0 @@
|
||||
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"));
|
||||
}
|
||||
}
|
||||
@@ -1,89 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.datetolocaldate;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.temporal.ChronoField;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.datetolocaldate.DateToLocalDateConverter;
|
||||
|
||||
/**
|
||||
* JUnits for {@link DateToLocalDateConverter} class.
|
||||
*
|
||||
* @author abialas
|
||||
*
|
||||
*/
|
||||
public class DateToLocalDateConverterUnitTest {
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010WhenConvertViaInstant() {
|
||||
// given
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2010, 10, 10);
|
||||
Date dateToConvert = calendar.getTime();
|
||||
|
||||
// when
|
||||
LocalDate localDate = DateToLocalDateConverter.convertToLocalDateViaInstant(dateToConvert);
|
||||
|
||||
// then
|
||||
assertEquals(2010, localDate.get(ChronoField.YEAR));
|
||||
assertEquals(11, localDate.get(ChronoField.MONTH_OF_YEAR));
|
||||
assertEquals(10, localDate.get(ChronoField.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010WhenConvertViaMiliseconds() {
|
||||
// given
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2010, 10, 10);
|
||||
Date dateToConvert = calendar.getTime();
|
||||
|
||||
// when
|
||||
LocalDate localDate = DateToLocalDateConverter.convertToLocalDateViaMilisecond(dateToConvert);
|
||||
|
||||
// then
|
||||
assertEquals(2010, localDate.get(ChronoField.YEAR));
|
||||
assertEquals(11, localDate.get(ChronoField.MONTH_OF_YEAR));
|
||||
assertEquals(10, localDate.get(ChronoField.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010WhenConvertViaSqlDate() {
|
||||
// given
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2010, 10, 10);
|
||||
Date dateToConvert = calendar.getTime();
|
||||
|
||||
// when
|
||||
LocalDate localDate = DateToLocalDateConverter.convertToLocalDateViaSqlDate(dateToConvert);
|
||||
|
||||
// then
|
||||
assertEquals(2010, localDate.get(ChronoField.YEAR));
|
||||
assertEquals(11, localDate.get(ChronoField.MONTH_OF_YEAR));
|
||||
assertEquals(10, localDate.get(ChronoField.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010WhenConvertToLocalDate() {
|
||||
// given
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2010, 10, 10);
|
||||
Date dateToConvert = calendar.getTime();
|
||||
|
||||
// when
|
||||
LocalDate localDateTime = DateToLocalDateConverter.convertToLocalDate(dateToConvert);
|
||||
|
||||
// then
|
||||
assertEquals(2010, localDateTime.get(ChronoField.YEAR));
|
||||
assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR));
|
||||
assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.datetolocaldate;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.temporal.ChronoField;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.datetolocaldate.DateToLocalDateTimeConverter;
|
||||
|
||||
/**
|
||||
* JUnits for {@link DateToLocalDateTimeConverter} class.
|
||||
*
|
||||
* @author abialas
|
||||
*
|
||||
*/
|
||||
public class DateToLocalDateTimeConverterUnitTest {
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010time8hour20minWhenConvertViaInstant() {
|
||||
// given
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2010, 10, 10, 8, 20);
|
||||
Date dateToConvert = calendar.getTime();
|
||||
|
||||
// when
|
||||
LocalDateTime localDateTime = DateToLocalDateTimeConverter.convertToLocalDateTimeViaInstant(dateToConvert);
|
||||
|
||||
// then
|
||||
assertEquals(2010, localDateTime.get(ChronoField.YEAR));
|
||||
assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR));
|
||||
assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH));
|
||||
assertEquals(8, localDateTime.get(ChronoField.HOUR_OF_DAY));
|
||||
assertEquals(20, localDateTime.get(ChronoField.MINUTE_OF_HOUR));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010time8hour20minWhenConvertViaMiliseconds() {
|
||||
// given
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2010, 10, 10, 8, 20);
|
||||
Date dateToConvert = calendar.getTime();
|
||||
|
||||
// when
|
||||
LocalDateTime localDateTime = DateToLocalDateTimeConverter.convertToLocalDateTimeViaMilisecond(dateToConvert);
|
||||
|
||||
// then
|
||||
assertEquals(2010, localDateTime.get(ChronoField.YEAR));
|
||||
assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR));
|
||||
assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH));
|
||||
assertEquals(8, localDateTime.get(ChronoField.HOUR_OF_DAY));
|
||||
assertEquals(20, localDateTime.get(ChronoField.MINUTE_OF_HOUR));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010time8hour20minWhenConvertViaSqlTimestamp() {
|
||||
// given
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2010, 10, 10, 8, 20);
|
||||
Date dateToConvert = calendar.getTime();
|
||||
|
||||
// when
|
||||
LocalDateTime localDateTime = DateToLocalDateTimeConverter.convertToLocalDateTimeViaSqlTimestamp(dateToConvert);
|
||||
|
||||
// then
|
||||
assertEquals(2010, localDateTime.get(ChronoField.YEAR));
|
||||
assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR));
|
||||
assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH));
|
||||
assertEquals(8, localDateTime.get(ChronoField.HOUR_OF_DAY));
|
||||
assertEquals(20, localDateTime.get(ChronoField.MINUTE_OF_HOUR));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010time8hour20minWhenConvertToLocalDateTime() {
|
||||
// given
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2010, 10, 10, 8, 20);
|
||||
Date dateToConvert = calendar.getTime();
|
||||
|
||||
// when
|
||||
LocalDateTime localDateTime = DateToLocalDateTimeConverter.convertToLocalDateTime(dateToConvert);
|
||||
|
||||
// then
|
||||
assertEquals(2010, localDateTime.get(ChronoField.YEAR));
|
||||
assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR));
|
||||
assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH));
|
||||
assertEquals(8, localDateTime.get(ChronoField.HOUR_OF_DAY));
|
||||
assertEquals(20, localDateTime.get(ChronoField.MINUTE_OF_HOUR));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.datetolocaldate;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.datetolocaldate.LocalDateTimeToDateConverter;
|
||||
|
||||
/**
|
||||
*
|
||||
* JUnits for {@link LocalDateTimeToDateConverter} class.
|
||||
*
|
||||
* @author abialas
|
||||
*
|
||||
*/
|
||||
public class LocalDateTimeToDateConverterUnitTest {
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010time8hour20minWhenConvertViaInstant() {
|
||||
// given
|
||||
LocalDateTime dateToConvert = LocalDateTime.of(2010, 11, 10, 8, 20);
|
||||
|
||||
// when
|
||||
Date date = LocalDateTimeToDateConverter.convertToDateViaInstant(dateToConvert);
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(date);
|
||||
|
||||
// then
|
||||
assertEquals(2010, calendar.get(Calendar.YEAR));
|
||||
assertEquals(10, calendar.get(Calendar.MONTH));
|
||||
assertEquals(10, calendar.get(Calendar.DAY_OF_MONTH));
|
||||
assertEquals(8, calendar.get(Calendar.HOUR));
|
||||
assertEquals(20, calendar.get(Calendar.MINUTE));
|
||||
assertEquals(0, calendar.get(Calendar.SECOND));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010WhenConvertViaSqlTimestamp() {
|
||||
// given
|
||||
LocalDateTime dateToConvert = LocalDateTime.of(2010, 11, 10, 8, 20);
|
||||
|
||||
// when
|
||||
Date date = LocalDateTimeToDateConverter.convertToDateViaSqlTimestamp(dateToConvert);
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(date);
|
||||
|
||||
// then
|
||||
assertEquals(2010, calendar.get(Calendar.YEAR));
|
||||
assertEquals(10, calendar.get(Calendar.MONTH));
|
||||
assertEquals(10, calendar.get(Calendar.DAY_OF_MONTH));
|
||||
assertEquals(8, calendar.get(Calendar.HOUR));
|
||||
assertEquals(20, calendar.get(Calendar.MINUTE));
|
||||
assertEquals(0, calendar.get(Calendar.SECOND));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.datetolocaldate;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.datetolocaldate.LocalDateToDateConverter;
|
||||
|
||||
/**
|
||||
*
|
||||
* JUnits for {@link LocalDateToDateConverter} class.
|
||||
*
|
||||
* @author abialas
|
||||
*
|
||||
*/
|
||||
public class LocalDateToDateConverterUnitTest {
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010WhenConvertViaInstant() {
|
||||
// given
|
||||
LocalDate dateToConvert = LocalDate.of(2010, 11, 10);
|
||||
|
||||
// when
|
||||
Date date = LocalDateToDateConverter.convertToDateViaInstant(dateToConvert);
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(date);
|
||||
|
||||
// then
|
||||
assertEquals(2010, calendar.get(Calendar.YEAR));
|
||||
assertEquals(10, calendar.get(Calendar.MONTH));
|
||||
assertEquals(10, calendar.get(Calendar.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010WhenConvertViaSqlDate() {
|
||||
// given
|
||||
LocalDate dateToConvert = LocalDate.of(2010, 11, 10);
|
||||
|
||||
// when
|
||||
Date date = LocalDateToDateConverter.convertToDateViaSqlDate(dateToConvert);
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(date);
|
||||
|
||||
// then
|
||||
assertEquals(2010, calendar.get(Calendar.YEAR));
|
||||
assertEquals(10, calendar.get(Calendar.MONTH));
|
||||
assertEquals(10, calendar.get(Calendar.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.baeldung.zoneddatetime;
|
||||
|
||||
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)));
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.baeldung.zoneddatetime;
|
||||
|
||||
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)));
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package com.baeldung.zoneddatetime;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package com.baeldung.zoneddatetime;
|
||||
|
||||
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