Switch from Postgres to H2

This commit is contained in:
Ali Dehghani
2019-10-28 22:12:19 +03:30
parent db85c8f275
commit c4da87f511
20523 changed files with 1643929 additions and 0 deletions
@@ -0,0 +1,32 @@
package com.baeldung.date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.util.Date;
import org.joda.time.Years;
public class AgeCalculator {
public int calculateAge(LocalDate birthDate, LocalDate currentDate) {
// validate inputs ...
return Period.between(birthDate, currentDate)
.getYears();
}
public int calculateAgeWithJodaTime(org.joda.time.LocalDate birthDate, org.joda.time.LocalDate currentDate) {
// validate inputs ...
Years age = Years.yearsBetween(birthDate, currentDate);
return age.getYears();
}
public int calculateAgeWithJava7(Date birthDate, Date currentDate) {
// validate inputs ...
DateFormat formatter = new SimpleDateFormat("yyyyMMdd");
int d1 = Integer.parseInt(formatter.format(birthDate));
int d2 = Integer.parseInt(formatter.format(currentDate));
int age = (d2 - d1) / 10000;
return age;
}
}
@@ -0,0 +1,30 @@
package com.baeldung.date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
public class DateWithoutTime {
public static Date getDateWithoutTimeUsingCalendar() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
public static Date getDateWithoutTimeUsingFormat() throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
return formatter.parse(formatter.format(new Date()));
}
public static LocalDate getLocalDate() {
return LocalDate.now();
}
}
@@ -0,0 +1,35 @@
package com.baeldung.date.comparison;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import static java.time.temporal.ChronoUnit.*;
public class DateTimeComparisonUtils {
public static boolean isSameDay(LocalDateTime timestamp, LocalDate localDateToCompare) {
return timestamp.toLocalDate().isEqual(localDateToCompare);
}
public static boolean isSameDay(LocalDateTime timestamp, LocalDateTime timestampToCompare) {
return timestamp.truncatedTo(DAYS).isEqual(timestampToCompare.truncatedTo(DAYS));
}
public static boolean isSameHour(LocalDateTime timestamp, LocalDateTime timestampToCompare) {
return timestamp.truncatedTo(HOURS).isEqual(timestampToCompare.truncatedTo(HOURS));
}
public static boolean isSameMinute(LocalDateTime timestamp, LocalDateTime timestampToCompare) {
return timestamp.truncatedTo(MINUTES).isEqual(timestampToCompare.truncatedTo(MINUTES));
}
public static boolean isSameHour(ZonedDateTime zonedTimestamp, ZonedDateTime zonedTimestampToCompare) {
return zonedTimestamp.truncatedTo(HOURS).isEqual(zonedTimestampToCompare.truncatedTo(HOURS));
}
public static boolean isSameHour(ZonedDateTime zonedDateTime, LocalDateTime localDateTime, ZoneId zoneId) {
return isSameHour(zonedDateTime, localDateTime.atZone(zoneId));
}
}
@@ -0,0 +1,17 @@
package com.baeldung.date.comparison;
import org.apache.commons.lang3.time.DateUtils;
import java.util.Calendar;
import java.util.Date;
public class LegacyDateComparisonUtils {
public static boolean isSameDay(Date date, Date dateToCompare) {
return DateUtils.isSameDay(date, dateToCompare);
}
public static boolean isSameHour(Date date, Date dateToCompare) {
return DateUtils.truncatedEquals(date, dateToCompare, Calendar.HOUR);
}
}
@@ -0,0 +1,54 @@
package com.baeldung.datetime;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang3.time.DateUtils;
public class AddHoursToDate {
public Date addHoursToJavaUtilDate(Date date, int hours) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR_OF_DAY, hours);
return calendar.getTime();
}
public Date addHoursToDateUsingInstant(Date date, int hours) {
return Date.from(date.toInstant()
.plus(Duration.ofHours(hours)));
}
public LocalDateTime addHoursToLocalDateTime(LocalDateTime localDateTime, int hours) {
return localDateTime.plusHours(hours);
}
public LocalDateTime subtractHoursToLocalDateTime(LocalDateTime localDateTime, int hours) {
return localDateTime.minusHours(hours);
}
public ZonedDateTime addHoursToZonedDateTime(ZonedDateTime zonedDateTime, int hours) {
return zonedDateTime.plusHours(hours);
}
public ZonedDateTime subtractHoursToZonedDateTime(ZonedDateTime zonedDateTime, int hours) {
return zonedDateTime.minusHours(hours);
}
public Instant addHoursToInstant(Instant instant, int hours) {
return instant.plus(hours, ChronoUnit.HOURS);
}
public Instant subtractHoursToInstant(Instant instant, int hours) {
return instant.minus(hours, ChronoUnit.HOURS);
}
public Date addHoursWithApacheCommons(Date date, int hours) {
return DateUtils.addHours(date, hours);
}
}
@@ -0,0 +1,28 @@
package com.baeldung.datetime;
import java.util.Calendar;
import java.util.Date;
public class DateExtractYearMonthDayIntegerValues {
int getYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.YEAR);
}
int getMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MONTH);
}
int getDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_MONTH);
}
}
@@ -0,0 +1,18 @@
package com.baeldung.datetime;
import java.time.LocalDate;
public class LocalDateExtractYearMonthDayIntegerValues {
int getYear(LocalDate localDate) {
return localDate.getYear();
}
int getMonth(LocalDate localDate) {
return localDate.getMonthValue();
}
int getDay(LocalDate localDate) {
return localDate.getDayOfMonth();
}
}
@@ -0,0 +1,18 @@
package com.baeldung.datetime;
import java.time.LocalDateTime;
public class LocalDateTimeExtractYearMonthDayIntegerValues {
int getYear(LocalDateTime localDateTime) {
return localDateTime.getYear();
}
int getMonth(LocalDateTime localDateTime) {
return localDateTime.getMonthValue();
}
int getDay(LocalDateTime localDateTime) {
return localDateTime.getDayOfMonth();
}
}
@@ -0,0 +1,18 @@
package com.baeldung.datetime;
import java.time.OffsetDateTime;
public class OffsetDateTimeExtractYearMonthDayIntegerValues {
int getYear(OffsetDateTime offsetDateTime) {
return offsetDateTime.getYear();
}
int getMonth(OffsetDateTime offsetDateTime) {
return offsetDateTime.getMonthValue();
}
int getDay(OffsetDateTime offsetDateTime) {
return offsetDateTime.getDayOfMonth();
}
}
@@ -0,0 +1 @@
### Relevant Articles:
@@ -0,0 +1,21 @@
package com.baeldung.datetime;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;
public class UseDateTimeFormatter {
public String formatAsIsoDate(LocalDateTime localDateTime) {
return localDateTime.format(DateTimeFormatter.ISO_DATE);
}
public String formatCustom(LocalDateTime localDateTime, String pattern) {
return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
}
public String formatWithStyleAndLocale(LocalDateTime localDateTime, FormatStyle formatStyle, Locale locale) {
return localDateTime.format(DateTimeFormatter.ofLocalizedDateTime(formatStyle)
.withLocale(locale));
}
}
@@ -0,0 +1,15 @@
package com.baeldung.datetime;
import java.time.Duration;
import java.time.LocalTime;
public class UseDuration {
public LocalTime modifyDates(LocalTime localTime, Duration duration) {
return localTime.plus(duration);
}
public Duration getDifferenceBetweenDates(LocalTime localTime1, LocalTime localTime2) {
return Duration.between(localTime1, localTime2);
}
}
@@ -0,0 +1,78 @@
package com.baeldung.datetime;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
class UseLocalDate {
LocalDate getLocalDateUsingFactoryOfMethod(int year, int month, int dayOfMonth) {
return LocalDate.of(year, month, dayOfMonth);
}
LocalDate getLocalDateUsingParseMethod(String representation) {
return LocalDate.parse(representation);
}
LocalDate getLocalDateFromClock() {
LocalDate localDate = LocalDate.now();
return localDate;
}
LocalDate getNextDay(LocalDate localDate) {
return localDate.plusDays(1);
}
LocalDate getPreviousDay(LocalDate localDate) {
return localDate.minus(1, ChronoUnit.DAYS);
}
DayOfWeek getDayOfWeek(LocalDate localDate) {
DayOfWeek day = localDate.getDayOfWeek();
return day;
}
LocalDate getFirstDayOfMonth() {
LocalDate firstDayOfMonth = LocalDate.now()
.with(TemporalAdjusters.firstDayOfMonth());
return firstDayOfMonth;
}
boolean isLeapYear(LocalDate localDate) {
return localDate.isLeapYear();
}
LocalDateTime getStartOfDay(LocalDate localDate) {
LocalDateTime startofDay = localDate.atStartOfDay();
return startofDay;
}
LocalDateTime getStartOfDayOfLocalDate(LocalDate localDate) {
LocalDateTime startofDay = LocalDateTime.of(localDate, LocalTime.MIDNIGHT);
return startofDay;
}
LocalDateTime getStartOfDayAtMinTime(LocalDate localDate) {
LocalDateTime startofDay = localDate.atTime(LocalTime.MIN);
return startofDay;
}
LocalDateTime getStartOfDayAtMidnightTime(LocalDate localDate) {
LocalDateTime startofDay = localDate.atTime(LocalTime.MIDNIGHT);
return startofDay;
}
LocalDateTime getEndOfDay(LocalDate localDate) {
LocalDateTime endOfDay = localDate.atTime(LocalTime.MAX);
return endOfDay;
}
LocalDateTime getEndOfDayFromLocalTime(LocalDate localDate) {
LocalDateTime endOfDate = LocalTime.MAX.atDate(localDate);
return endOfDate;
}
}
@@ -0,0 +1,28 @@
package com.baeldung.datetime;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoField;
public class UseLocalDateTime {
public LocalDateTime getLocalDateTimeUsingParseMethod(String representation) {
return LocalDateTime.parse(representation);
}
LocalDateTime getEndOfDayFromLocalDateTimeDirectly(LocalDateTime localDateTime) {
LocalDateTime endOfDate = localDateTime.with(ChronoField.NANO_OF_DAY, LocalTime.MAX.toNanoOfDay());
return endOfDate;
}
LocalDateTime getEndOfDayFromLocalDateTime(LocalDateTime localDateTime) {
LocalDateTime endOfDate = localDateTime.toLocalDate()
.atTime(LocalTime.MAX);
return endOfDate;
}
LocalDateTime ofEpochSecond(int epochSecond, ZoneOffset zoneOffset) {
return LocalDateTime.ofEpochSecond(epochSecond, 0, zoneOffset);
}
}
@@ -0,0 +1,35 @@
package com.baeldung.datetime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
public class UseLocalTime {
LocalTime getLocalTimeUsingFactoryOfMethod(int hour, int min, int seconds) {
return LocalTime.of(hour, min, seconds);
}
LocalTime getLocalTimeUsingFactoryOfMethod(int hour, int min) {
return LocalTime.of(hour, min);
}
LocalTime getLocalTimeUsingParseMethod(String timeRepresentation) {
return LocalTime.parse(timeRepresentation);
}
private LocalTime getLocalTimeFromClock() {
return LocalTime.now();
}
LocalTime addAnHour(LocalTime localTime) {
return localTime.plus(1, ChronoUnit.HOURS);
}
int getHourFromLocalTime(LocalTime localTime) {
return localTime.getHour();
}
LocalTime getLocalTimeWithMinuteSetToValue(LocalTime localTime, int minute) {
return localTime.withMinute(minute);
}
}
@@ -0,0 +1,11 @@
package com.baeldung.datetime;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
public class UseOffsetDateTime {
public OffsetDateTime offsetOfLocalDateTimeAndOffset(LocalDateTime localDateTime, ZoneOffset offset) {
return OffsetDateTime.of(localDateTime, offset);
}
}
@@ -0,0 +1,15 @@
package com.baeldung.datetime;
import java.time.LocalDate;
import java.time.Period;
class UsePeriod {
LocalDate modifyDates(LocalDate localDate, Period period) {
return localDate.plus(period);
}
Period getDifferenceBetweenDates(LocalDate localDate1, LocalDate localDate2) {
return Period.between(localDate1, localDate2);
}
}
@@ -0,0 +1,17 @@
package com.baeldung.datetime;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
public class UseToInstant {
public LocalDateTime convertDateToLocalDate(Date date) {
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
}
public LocalDateTime convertDateToLocalDate(Calendar calendar) {
return LocalDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault());
}
}
@@ -0,0 +1,46 @@
package com.baeldung.datetime;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
class UseZonedDateTime {
ZonedDateTime getZonedDateTime(LocalDateTime localDateTime, ZoneId zoneId) {
return ZonedDateTime.of(localDateTime, zoneId);
}
ZonedDateTime getZonedDateTimeUsingParseMethod(String parsableString) {
return ZonedDateTime.parse(parsableString);
}
ZonedDateTime getStartOfDay(LocalDate localDate, ZoneId zone) {
ZonedDateTime startOfDay = localDate.atStartOfDay()
.atZone(zone);
return startOfDay;
}
ZonedDateTime getStartOfDayShorthand(LocalDate localDate, ZoneId zone) {
ZonedDateTime startOfDay = localDate.atStartOfDay(zone);
return startOfDay;
}
ZonedDateTime getStartOfDayFromZonedDateTime(ZonedDateTime zonedDateTime) {
ZonedDateTime startOfDay = zonedDateTime.toLocalDateTime()
.toLocalDate()
.atStartOfDay(zonedDateTime.getZone());
return startOfDay;
}
ZonedDateTime getStartOfDayAtMinTime(ZonedDateTime zonedDateTime) {
ZonedDateTime startOfDay = zonedDateTime.with(ChronoField.HOUR_OF_DAY, 0);
return startOfDay;
}
ZonedDateTime getStartOfDayAtMidnightTime(ZonedDateTime zonedDateTime) {
ZonedDateTime startOfDay = zonedDateTime.with(ChronoField.NANO_OF_DAY, 0);
return startOfDay;
}
}
@@ -0,0 +1,18 @@
package com.baeldung.datetime;
import java.time.ZonedDateTime;
public class ZonedDateTimeExtractYearMonthDayIntegerValues {
int getYear(ZonedDateTime zonedDateTime) {
return zonedDateTime.getYear();
}
int getMonth(ZonedDateTime zonedDateTime) {
return zonedDateTime.getMonthValue();
}
int getDay(ZonedDateTime zonedDateTime) {
return zonedDateTime.getDayOfMonth();
}
}
@@ -0,0 +1,63 @@
package com.baeldung.datetime.modify;
import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.DateTime;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Logger;
public class DateIncrementer {
private static final Logger log = Logger.getLogger(DateIncrementer.class.getName());
private static final int INCREMENT_BY_IN_DAYS = 1;
public static String addOneDay(String date) {
return LocalDate
.parse(date)
.plusDays(INCREMENT_BY_IN_DAYS)
.toString();
}
public static String addOneDayJodaTime(String date) {
DateTime dateTime = new DateTime(date);
return dateTime
.plusDays(INCREMENT_BY_IN_DAYS)
.toString("yyyy-MM-dd");
}
public static String addOneDayCalendar(String date) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(date));
c.add(Calendar.DATE, 1);
return sdf.format(c.getTime());
}
public static String addOneDayApacheCommons(String date) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date incrementedDate = DateUtils.addDays(sdf.parse(date), 1);
return sdf.format(incrementedDate);
}
public static void main(String[] args) throws ParseException {
String date = LocalDate
.now()
.toString();
log.info("Current date = " + date);
String incrementedDateJava8 = DateIncrementer.addOneDay(date);
log.info("Date incremented by one day using (Java 8): " + incrementedDateJava8);
String incrementedDateJodaTime = DateIncrementer.addOneDayJodaTime(date);
log.info("Date incremented by one day using (Joda-Time): " + incrementedDateJodaTime);
String incrementedDateCalendar = addOneDayCalendar(date);
log.info("Date incremented by one day using (java.util.Calendar): " + incrementedDateCalendar);
String incrementedDateApacheCommons = addOneDayApacheCommons(date);
log.info("Date incremented by one day using (Apache Commons DateUtils): " + incrementedDateApacheCommons);
}
}
@@ -0,0 +1,69 @@
package com.baeldung.gregorian.calendar;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.TimeZone;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
public class GregorianCalendarExample {
public Date setMonth(GregorianCalendar calendar, int amount) {
calendar.set(Calendar.MONTH, amount);
return calendar.getTime();
}
public Date rollAdd(GregorianCalendar calendar, int amount) {
calendar.roll(GregorianCalendar.MONTH, amount);
return calendar.getTime();
}
public boolean isLeapYearExample(int year) {
GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
return cal.isLeapYear(year);
}
public Date subtractDays(GregorianCalendar calendar, int daysToSubtract) {
GregorianCalendar cal = calendar;
cal.add(Calendar.DATE, -daysToSubtract);
return cal.getTime();
}
public Date addDays(GregorianCalendar calendar, int daysToAdd) {
GregorianCalendar cal = calendar;
cal.add(Calendar.DATE, daysToAdd);
return cal.getTime();
}
public XMLGregorianCalendar toXMLGregorianCalendar(GregorianCalendar calendar) throws DatatypeConfigurationException {
DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
return datatypeFactory.newXMLGregorianCalendar(calendar);
}
public Date toDate(XMLGregorianCalendar calendar) {
return calendar.toGregorianCalendar()
.getTime();
}
public int compareDates(GregorianCalendar firstDate, GregorianCalendar secondDate) {
return firstDate.compareTo(secondDate);
}
public String formatDate(GregorianCalendar calendar) {
return calendar.toZonedDateTime()
.format(DateTimeFormatter.ofPattern("d MMM uuuu"));
}
}
@@ -0,0 +1,43 @@
package com.baeldung.java9.time;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class TimeApi {
public static List<Date> getDatesBetweenUsingJava7(Date startDate, Date endDate) {
List<Date> datesInRange = new ArrayList<Date>();
Calendar calendar = new GregorianCalendar();
calendar.setTime(startDate);
Calendar endCalendar = new GregorianCalendar();
endCalendar.setTime(endDate);
while (calendar.before(endCalendar)) {
Date result = calendar.getTime();
datesInRange.add(result);
calendar.add(Calendar.DATE, 1);
}
return datesInRange;
}
public static List<LocalDate> getDatesBetweenUsingJava8(LocalDate startDate, LocalDate endDate) {
long numOfDaysBetween = ChronoUnit.DAYS.between(startDate, endDate);
return IntStream.iterate(0, i -> i + 1)
.limit(numOfDaysBetween)
.mapToObj(i -> startDate.plusDays(i))
.collect(Collectors.toList());
}
public static List<LocalDate> getDatesBetweenUsingJava9(LocalDate startDate, LocalDate endDate) {
return startDate.datesUntil(endDate).collect(Collectors.toList());
}
}
@@ -0,0 +1,22 @@
package com.baeldung.temporaladjuster;
import java.time.DayOfWeek;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjuster;
public class CustomTemporalAdjuster implements TemporalAdjuster {
@Override
public Temporal adjustInto(Temporal temporal) {
switch (DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK))) {
case FRIDAY:
return temporal.plus(3, ChronoUnit.DAYS);
case SATURDAY:
return temporal.plus(2, ChronoUnit.DAYS);
default:
return temporal.plus(1, ChronoUnit.DAYS);
}
}
}