Search in sources :

Example 26 with Period

use of java.time.Period in project cals-api by ca-cwds.

the class PersonMapper method fillAge.

@AfterMapping
default void fillAge(@MappingTarget PersonDTO personDTO) {
    LocalDate dateOfBirth = personDTO.getDateOfBirth();
    if (dateOfBirth != null) {
        Period period = Period.between(dateOfBirth, LocalDate.now());
        personDTO.setAge(period.getYears());
    }
}
Also used : Period(java.time.Period) LocalDate(java.time.LocalDate) AfterMapping(org.mapstruct.AfterMapping)

Example 27 with Period

use of java.time.Period in project portfolio by buchen.

the class ReportingPeriodDialog method presetFromTemplate.

private void presetFromTemplate() {
    if (template instanceof ReportingPeriod.LastX)
        radioLast.setSelection(true);
    else if (template instanceof ReportingPeriod.LastXDays)
        radioLastDays.setSelection(true);
    else if (template instanceof ReportingPeriod.LastXTradingDays)
        radioLastTradingDays.setSelection(true);
    else if (template instanceof ReportingPeriod.FromXtoY)
        radioFromXtoY.setSelection(true);
    else if (template instanceof ReportingPeriod.SinceX)
        radioSinceX.setSelection(true);
    else if (template instanceof ReportingPeriod.YearX)
        radioYearX.setSelection(true);
    else
        throw new IllegalArgumentException();
    dateFrom.setSelection(template.getStartDate());
    dateSince.setSelection(template.getStartDate());
    dateTo.setSelection(template.getEndDate());
    Period p = Period.between(template.getStartDate(), template.getEndDate());
    years.setSelection(p.getYears());
    months.setSelection(p.getMonths());
    days.setSelection(Dates.daysBetween(template.getStartDate(), template.getEndDate()));
    tradingDays.setSelection(Dates.tradingDaysBetween(template.getStartDate(), template.getEndDate()));
    year.setSelection(template.getEndDate().getYear());
}
Also used : ReportingPeriod(name.abuchen.portfolio.snapshot.ReportingPeriod) Period(java.time.Period) ReportingPeriod(name.abuchen.portfolio.snapshot.ReportingPeriod)

Example 28 with Period

use of java.time.Period in project portfolio by buchen.

the class TimelineChart method paintTimeGrid.

private void paintTimeGrid(PaintEvent e) {
    IAxis xAxis = getAxisSet().getXAxis(0);
    Range range = xAxis.getRange();
    ZoneId zoneId = ZoneId.systemDefault();
    LocalDate start = Instant.ofEpochMilli((long) range.lower).atZone(zoneId).toLocalDate();
    LocalDate end = Instant.ofEpochMilli((long) range.upper).atZone(zoneId).toLocalDate();
    LocalDate cursor = start.getDayOfMonth() == 1 ? start : start.plusMonths(1).withDayOfMonth(1);
    Period period;
    DateTimeFormatter format;
    long days = ChronoUnit.DAYS.between(start, end);
    if (days < 250) {
        period = Period.ofMonths(1);
        // $NON-NLS-1$
        format = DateTimeFormatter.ofPattern("MMMM yyyy");
    } else if (days < 800) {
        period = Period.ofMonths(3);
        // $NON-NLS-1$
        format = DateTimeFormatter.ofPattern("QQQ yyyy");
        cursor = cursor.plusMonths((12 - cursor.getMonthValue() + 1) % 3);
    } else if (days < 1200) {
        period = Period.ofMonths(6);
        // $NON-NLS-1$
        format = DateTimeFormatter.ofPattern("QQQ yyyy");
        cursor = cursor.plusMonths((12 - cursor.getMonthValue() + 1) % 6);
    } else {
        period = Period.ofYears(days > 5000 ? 2 : 1);
        // $NON-NLS-1$
        format = DateTimeFormatter.ofPattern("yyyy");
        if (cursor.getMonthValue() > 1)
            cursor = cursor.plusYears(1).withDayOfYear(1);
    }
    while (cursor.isBefore(end)) {
        int y = xAxis.getPixelCoordinate((double) cursor.atStartOfDay(zoneId).toInstant().toEpochMilli());
        e.gc.drawLine(y, 0, y, e.height);
        e.gc.drawText(format.format(cursor), y + 5, 5);
        cursor = cursor.plus(period);
    }
}
Also used : ZoneId(java.time.ZoneId) Period(java.time.Period) Range(org.swtchart.Range) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) IAxis(org.swtchart.IAxis) Point(org.eclipse.swt.graphics.Point)

Example 29 with Period

use of java.time.Period in project Shadbot by Shadorc.

the class FormatUtils method formatLongDuration.

public static String formatLongDuration(Instant instant) {
    Period period = Period.between(TimeUtils.toLocalDate(instant).toLocalDate(), LocalDate.now());
    String str = period.getUnits().stream().filter(unit -> period.get(unit) != 0).map(unit -> String.format("%d %s", period.get(unit), unit.toString().toLowerCase())).collect(Collectors.joining(", "));
    return str.isEmpty() ? FormatUtils.formatShortDuration(instant.toEpochMilli()) : str;
}
Also used : Period(java.time.Period) IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) Instant(java.time.Instant) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) NumberFormat(java.text.NumberFormat) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Stream(java.util.stream.Stream) Lists(com.google.common.collect.Lists) DurationFormatUtils(org.apache.commons.lang3.time.DurationFormatUtils) Locale(java.util.Locale) EmbedField(sx.blah.discord.handle.impl.obj.Embed.EmbedField) LocalDate(java.time.LocalDate) AudioTrackInfo(com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo) Period(java.time.Period)

Example 30 with Period

use of java.time.Period in project sqlg by pietermartin.

the class CockroachdbDialect method appendSqlValue.

private void appendSqlValue(StringBuilder sql, Object value, PropertyType propertyType) {
    switch(propertyType) {
        case BOOLEAN:
            if (value != null) {
                sql.append(value);
            } else {
                sql.append("null");
            }
            break;
        case BYTE:
            if (value != null) {
                sql.append(value);
            } else {
                sql.append("null");
            }
            break;
        case SHORT:
            if (value != null) {
                sql.append(value);
            } else {
                sql.append("null");
            }
            break;
        case INTEGER:
            if (value != null) {
                sql.append(value);
            } else {
                sql.append("null");
            }
            break;
        case LONG:
            if (value != null) {
                sql.append(value);
            } else {
                sql.append("null");
            }
            break;
        case FLOAT:
            if (value != null) {
                sql.append(value);
            } else {
                sql.append("null");
            }
            break;
        case DOUBLE:
            if (value != null) {
                sql.append(value);
            } else {
                sql.append("null");
            }
            break;
        case STRING:
            if (value != null) {
                sql.append("'");
                sql.append(value.toString().replace("'", "''"));
                sql.append("'");
            } else {
                sql.append("null");
            }
            break;
        case LOCALDATETIME:
            if (value != null) {
                sql.append("'");
                sql.append(value.toString());
                sql.append("'::TIMESTAMP");
            } else {
                sql.append("null");
            }
            break;
        case LOCALDATE:
            if (value != null) {
                sql.append("'");
                sql.append(value.toString());
                sql.append("'::DATE");
            } else {
                sql.append("null");
            }
            break;
        case LOCALTIME:
            if (value != null) {
                sql.append("'");
                sql.append(shiftDST((LocalTime) value).toString());
                sql.append("'::TIME");
            } else {
                sql.append("null");
            }
            break;
        case ZONEDDATETIME:
            if (value != null) {
                ZonedDateTime zonedDateTime = (ZonedDateTime) value;
                LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
                TimeZone timeZone = TimeZone.getTimeZone(zonedDateTime.getZone());
                sql.append("'");
                sql.append(localDateTime.toString());
                sql.append("'::TIMESTAMP");
                sql.append(",'");
                sql.append(timeZone.getID());
                sql.append("'");
            } else {
                sql.append("null,null");
            }
            break;
        case DURATION:
            if (value != null) {
                Duration duration = (Duration) value;
                sql.append("'");
                sql.append(duration.getSeconds());
                sql.append("'::BIGINT");
                sql.append(",'");
                sql.append(duration.getNano());
                sql.append("'::INTEGER");
            } else {
                sql.append("null,null");
            }
            break;
        case PERIOD:
            if (value != null) {
                Period period = (Period) value;
                sql.append("'");
                sql.append(period.getYears());
                sql.append("'::INTEGER");
                sql.append(",'");
                sql.append(period.getMonths());
                sql.append("'::INTEGER");
                sql.append(",'");
                sql.append(period.getDays());
                sql.append("'::INTEGER");
            } else {
                sql.append("null,null,null");
            }
            break;
        case JSON:
            if (value != null) {
                sql.append("'");
                sql.append(value.toString().replace("'", "''"));
                sql.append("'::JSONB");
            } else {
                sql.append("null");
            }
            break;
        case boolean_ARRAY:
            if (value != null) {
                sql.append("'{");
                boolean[] booleanArray = (boolean[]) value;
                int countBooleanArray = 1;
                for (Boolean b : booleanArray) {
                    sql.append(b);
                    if (countBooleanArray++ < booleanArray.length) {
                        sql.append(",");
                    }
                }
                sql.append("}'");
            } else {
                sql.append("null");
            }
            break;
        case BOOLEAN_ARRAY:
            if (value != null) {
                sql.append("'{");
                Boolean[] BooleanArray = (Boolean[]) value;
                int countBOOLEANArray = 1;
                for (Boolean b : BooleanArray) {
                    sql.append(b);
                    if (countBOOLEANArray++ < BooleanArray.length) {
                        sql.append(",");
                    }
                }
                sql.append("}'");
            } else {
                sql.append("null");
            }
            break;
        case byte_ARRAY:
            if (value != null) {
                sql.append("'");
                sql.append(PGbytea.toPGString((byte[]) value).replace("'", "''"));
                sql.append("'");
            } else {
                sql.append("null");
            }
            break;
        case BYTE_ARRAY:
            if (value != null) {
                sql.append("'");
                sql.append(PGbytea.toPGString((byte[]) SqlgUtil.convertByteArrayToPrimitiveArray((Byte[]) value)).replace("'", "''"));
                sql.append("'");
            } else {
                sql.append("null");
            }
            break;
        case short_ARRAY:
            if (value != null) {
                sql.append("'{");
                short[] sortArray = (short[]) value;
                int countShortArray = 1;
                for (Short s : sortArray) {
                    sql.append(s);
                    if (countShortArray++ < sortArray.length) {
                        sql.append(",");
                    }
                }
                sql.append("}'");
            } else {
                sql.append("null");
            }
            break;
        case SHORT_ARRAY:
            if (value != null) {
                sql.append("'{");
                Short[] shortObjectArray = (Short[]) value;
                for (int i = 0; i < shortObjectArray.length; i++) {
                    Short s = shortObjectArray[i];
                    sql.append(s);
                    if (i < shortObjectArray.length - 1) {
                        sql.append(",");
                    }
                }
                sql.append("}'");
            } else {
                sql.append("null");
            }
            break;
        case int_ARRAY:
            if (value != null) {
                sql.append("'{");
                int[] intArray = (int[]) value;
                int countIntArray = 1;
                for (Integer i : intArray) {
                    sql.append(i);
                    if (countIntArray++ < intArray.length) {
                        sql.append(",");
                    }
                }
                sql.append("}'");
            } else {
                sql.append("null");
            }
            break;
        case INTEGER_ARRAY:
            if (value != null) {
                sql.append("'{");
                Integer[] integerArray = (Integer[]) value;
                int countIntegerArray = 1;
                for (Integer i : integerArray) {
                    sql.append(i);
                    if (countIntegerArray++ < integerArray.length) {
                        sql.append(",");
                    }
                }
                sql.append("}'");
            } else {
                sql.append("null");
            }
            break;
        case LONG_ARRAY:
            if (value != null) {
                sql.append("'{");
                Long[] longArray = (Long[]) value;
                int countLongArray = 1;
                for (Long l : longArray) {
                    sql.append(l);
                    if (countLongArray++ < longArray.length) {
                        sql.append(",");
                    }
                }
                sql.append("}'");
            } else {
                sql.append("null");
            }
            break;
        case long_ARRAY:
            if (value != null) {
                sql.append("'{");
                long[] longPrimitiveArray = (long[]) value;
                int countLongPrimitiveArray = 1;
                for (Long l : longPrimitiveArray) {
                    sql.append(l);
                    if (countLongPrimitiveArray++ < longPrimitiveArray.length) {
                        sql.append(",");
                    }
                }
                sql.append("}'");
            } else {
                sql.append("null");
            }
            break;
        case FLOAT_ARRAY:
            if (value != null) {
                sql.append("'{");
                Float[] floatArray = (Float[]) value;
                int countFloatArray = 1;
                for (Float f : floatArray) {
                    sql.append(f);
                    if (countFloatArray++ < floatArray.length) {
                        sql.append(",");
                    }
                }
                sql.append("}'");
            } else {
                sql.append("null");
            }
            break;
        case float_ARRAY:
            if (value != null) {
                sql.append("'{");
                float[] floatPrimitiveArray = (float[]) value;
                int countFloatPrimitiveArray = 1;
                for (Float f : floatPrimitiveArray) {
                    sql.append(f);
                    if (countFloatPrimitiveArray++ < floatPrimitiveArray.length) {
                        sql.append(",");
                    }
                }
                sql.append("}'");
            } else {
                sql.append("null");
            }
            break;
        case DOUBLE_ARRAY:
            if (value != null) {
                sql.append("'{");
                Double[] doubleArray = (Double[]) value;
                int countDoubleArray = 1;
                for (Double d : doubleArray) {
                    sql.append(d);
                    if (countDoubleArray++ < doubleArray.length) {
                        sql.append(",");
                    }
                }
                sql.append("}'");
            } else {
                sql.append("null");
            }
            break;
        case double_ARRAY:
            if (value != null) {
                sql.append("'{");
                double[] doublePrimitiveArray = (double[]) value;
                int countDoublePrimitiveArray = 1;
                for (Double d : doublePrimitiveArray) {
                    sql.append(d);
                    if (countDoublePrimitiveArray++ < doublePrimitiveArray.length) {
                        sql.append(",");
                    }
                }
                sql.append("}'");
            } else {
                sql.append("null");
            }
            break;
        case STRING_ARRAY:
            if (value != null) {
                sql.append("'{");
                String[] stringArray = (String[]) value;
                int countStringArray = 1;
                for (String s : stringArray) {
                    sql.append("\"");
                    sql.append(s);
                    sql.append("\"");
                    if (countStringArray++ < stringArray.length) {
                        sql.append(",");
                    }
                }
                sql.append("}'");
            } else {
                sql.append("null");
            }
            break;
        case LOCALDATETIME_ARRAY:
            if (value != null) {
                sql.append("ARRAY[");
                LocalDateTime[] localDateTimeArray = (LocalDateTime[]) value;
                int countStringArray = 1;
                for (LocalDateTime s : localDateTimeArray) {
                    sql.append("'");
                    sql.append(s.toString());
                    sql.append("'::TIMESTAMP");
                    if (countStringArray++ < localDateTimeArray.length) {
                        sql.append(",");
                    }
                }
                sql.append("]");
            } else {
                sql.append("null");
            }
            break;
        case LOCALDATE_ARRAY:
            if (value != null) {
                sql.append("ARRAY[");
                LocalDate[] localDateArray = (LocalDate[]) value;
                int countStringArray = 1;
                for (LocalDate s : localDateArray) {
                    sql.append("'");
                    sql.append(s.toString());
                    sql.append("'::DATE");
                    if (countStringArray++ < localDateArray.length) {
                        sql.append(",");
                    }
                }
                sql.append("]");
            } else {
                sql.append("null");
            }
            break;
        case LOCALTIME_ARRAY:
            if (value != null) {
                sql.append("ARRAY[");
                LocalTime[] localTimeArray = (LocalTime[]) value;
                int countStringArray = 1;
                for (LocalTime s : localTimeArray) {
                    sql.append("'");
                    sql.append(shiftDST(s).toLocalTime().toString());
                    sql.append("'::TIME");
                    if (countStringArray++ < localTimeArray.length) {
                        sql.append(",");
                    }
                }
                sql.append("]");
            } else {
                sql.append("null");
            }
            break;
        case ZONEDDATETIME_ARRAY:
            if (value != null) {
                sql.append("ARRAY[");
                ZonedDateTime[] localZonedDateTimeArray = (ZonedDateTime[]) value;
                int countStringArray = 1;
                for (ZonedDateTime zonedDateTime : localZonedDateTimeArray) {
                    LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
                    TimeZone timeZone = TimeZone.getTimeZone(zonedDateTime.getZone());
                    sql.append("'");
                    sql.append(localDateTime.toString());
                    sql.append("'::TIMESTAMP");
                    if (countStringArray++ < localZonedDateTimeArray.length) {
                        sql.append(",");
                    }
                }
                sql.append("],");
                sql.append("ARRAY[");
                countStringArray = 1;
                for (ZonedDateTime zonedDateTime : localZonedDateTimeArray) {
                    LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
                    TimeZone timeZone = TimeZone.getTimeZone(zonedDateTime.getZone());
                    sql.append("'");
                    sql.append(timeZone.getID());
                    sql.append("'");
                    if (countStringArray++ < localZonedDateTimeArray.length) {
                        sql.append(",");
                    }
                }
                sql.append("]");
            } else {
                sql.append("null,null");
            }
            break;
        case DURATION_ARRAY:
            if (value != null) {
                sql.append("ARRAY[");
                Duration[] durationArray = (Duration[]) value;
                int countStringArray = 1;
                for (Duration duration : durationArray) {
                    sql.append("'");
                    sql.append(duration.getSeconds());
                    sql.append("'::BIGINT");
                    if (countStringArray++ < durationArray.length) {
                        sql.append(",");
                    }
                }
                sql.append("],");
                sql.append("ARRAY[");
                countStringArray = 1;
                for (Duration duration : durationArray) {
                    sql.append("'");
                    sql.append(duration.getNano());
                    sql.append("'::INTEGER");
                    if (countStringArray++ < durationArray.length) {
                        sql.append(",");
                    }
                }
                sql.append("]");
            } else {
                sql.append("null,null");
            }
            break;
        case PERIOD_ARRAY:
            if (value != null) {
                sql.append("ARRAY[");
                Period[] periodArray = (Period[]) value;
                int countStringArray = 1;
                for (Period period : periodArray) {
                    sql.append("'");
                    sql.append(period.getYears());
                    sql.append("'::INTEGER");
                    if (countStringArray++ < periodArray.length) {
                        sql.append(",");
                    }
                }
                sql.append("],");
                sql.append("ARRAY[");
                countStringArray = 1;
                for (Period period : periodArray) {
                    sql.append("'");
                    sql.append(period.getMonths());
                    sql.append("'::INTEGER");
                    if (countStringArray++ < periodArray.length) {
                        sql.append(",");
                    }
                }
                sql.append("],");
                sql.append("ARRAY[");
                countStringArray = 1;
                for (Period period : periodArray) {
                    sql.append("'");
                    sql.append(period.getDays());
                    sql.append("'::INTEGER");
                    if (countStringArray++ < periodArray.length) {
                        sql.append(",");
                    }
                }
                sql.append("]");
            } else {
                sql.append("null,null,null");
            }
            break;
        case POINT:
            throw new IllegalStateException("JSON Arrays are not supported.");
        case LINESTRING:
            throw new IllegalStateException("JSON Arrays are not supported.");
        case POLYGON:
            throw new IllegalStateException("JSON Arrays are not supported.");
        case GEOGRAPHY_POINT:
            throw new IllegalStateException("JSON Arrays are not supported.");
        case GEOGRAPHY_POLYGON:
            throw new IllegalStateException("JSON Arrays are not supported.");
        case JSON_ARRAY:
            throw new IllegalStateException("JSON Arrays are not supported.");
        default:
            throw new IllegalStateException("Unknown propertyType " + propertyType.name());
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) LocalDate(java.time.LocalDate) ZonedDateTime(java.time.ZonedDateTime) LocalTime(java.time.LocalTime) Period(java.time.Period) Duration(java.time.Duration) TimeZone(java.util.TimeZone)

Aggregations

Period (java.time.Period)108 Test (org.junit.Test)38 Test (org.testng.annotations.Test)27 LocalDate (java.time.LocalDate)20 Duration (java.time.Duration)9 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)8 LocalDateTime (java.time.LocalDateTime)7 ZonedDateTime (java.time.ZonedDateTime)7 Date (java.util.Date)6 LocalTime (java.time.LocalTime)5 ZoneId (java.time.ZoneId)5 DateTimeFormatter (java.time.format.DateTimeFormatter)5 List (java.util.List)5 Test (org.junit.jupiter.api.Test)5 Instant (java.time.Instant)4 DateTimeParseException (java.time.format.DateTimeParseException)4 Temporal (java.time.temporal.Temporal)4 TemporalAmount (java.time.temporal.TemporalAmount)4 Arrays (java.util.Arrays)4 Collectors (java.util.stream.Collectors)4