Example usage for org.joda.time LocalDate LocalDate

List of usage examples for org.joda.time LocalDate LocalDate

Introduction

In this page you can find the example usage for org.joda.time LocalDate LocalDate.

Prototype

public LocalDate(Object instant, Chronology chronology) 

Source Link

Document

Constructs an instance from an Object that represents a datetime, using the specified chronology.

Usage

From source file:com.axelor.db.JpaFixture.java

License:Open Source License

@Transactional
public void load(String fixture) {

    final InputStream stream = read(fixture);
    final Map<Node, Object> objects = Maps.newLinkedHashMap();

    if (stream == null) {
        throw new IllegalArgumentException("No such fixture found: " + fixture);
    }//  ww w  .jav  a  2s  .  c  o m

    final Constructor ctor = new Constructor() {
        {
            yamlClassConstructors.put(NodeId.scalar, new TimeStampConstruct());
        }

        class TimeStampConstruct extends Constructor.ConstructScalar {

            Construct dateConstructor = yamlConstructors.get(Tag.TIMESTAMP);

            @Override
            public Object construct(Node nnode) {
                if (nnode.getTag().equals(Tag.TIMESTAMP)) {
                    Date date = (Date) dateConstructor.construct(nnode);
                    if (nnode.getType() == LocalDate.class) {
                        return new LocalDate(date, DateTimeZone.UTC);
                    }
                    if (nnode.getType() == LocalDateTime.class) {
                        return new LocalDateTime(date, DateTimeZone.UTC);
                    }
                    return new DateTime(date, DateTimeZone.UTC);
                } else {
                    return super.construct(nnode);
                }
            }

        }

        @Override
        protected Object constructObject(Node node) {

            Object obj = super.constructObject(node);

            if (objects.containsKey(node)) {
                return objects.get(node);
            }

            if (obj instanceof Model) {
                objects.put(node, obj);
                return obj;
            }
            return obj;
        }
    };

    for (Class<?> klass : JPA.models()) {
        ctor.addTypeDescription(new TypeDescription(klass, "!" + klass.getSimpleName() + ":"));
    }

    Yaml data = new Yaml(ctor);
    data.load(stream);

    for (Object item : Lists.reverse(Lists.newArrayList(objects.values()))) {
        try {
            JPA.manage((Model) item);
        } catch (Exception e) {
        }
    }
}

From source file:com.helger.datetime.config.PDTTypeConverterRegistrar.java

License:Apache License

public void registerTypeConverter(@Nonnull final ITypeConverterRegistry aRegistry) {
    // Register Joda native converters
    _registerJodaConverter();/* w  w w  . ja  va 2  s .c o  m*/

    final Class<?>[] aSourceClasses = new Class<?>[] { String.class, Calendar.class, GregorianCalendar.class,
            Date.class, AtomicInteger.class, AtomicLong.class, BigDecimal.class, BigInteger.class, Byte.class,
            Double.class, Float.class, Integer.class, Long.class, Short.class };

    // DateTime
    aRegistry.registerTypeConverter(aSourceClasses, DateTime.class, new ITypeConverter() {
        @Nonnull
        public DateTime convert(@Nonnull final Object aSource) {
            return new DateTime(aSource, PDTConfig.getDefaultChronology());
        }
    });
    aRegistry.registerTypeConverter(LocalDate.class, DateTime.class, new ITypeConverter() {
        @Nonnull
        public DateTime convert(@Nonnull final Object aSource) {
            return PDTFactory.createDateTime((LocalDate) aSource);
        }
    });
    aRegistry.registerTypeConverter(LocalTime.class, DateTime.class, new ITypeConverter() {
        @Nonnull
        public DateTime convert(@Nonnull final Object aSource) {
            return PDTFactory.createDateTime((LocalTime) aSource);
        }
    });
    aRegistry.registerTypeConverter(LocalDateTime.class, DateTime.class, new ITypeConverter() {
        @Nonnull
        public DateTime convert(@Nonnull final Object aSource) {
            return PDTFactory.createDateTime((LocalDateTime) aSource);
        }
    });

    // LocalDateTime
    aRegistry.registerTypeConverter(aSourceClasses, LocalDateTime.class, new ITypeConverter() {
        @Nonnull
        public LocalDateTime convert(@Nonnull final Object aSource) {
            return new LocalDateTime(aSource, PDTConfig.getDefaultChronology());
        }
    });
    aRegistry.registerTypeConverter(DateTime.class, LocalDateTime.class, new ITypeConverter() {
        @Nonnull
        public LocalDateTime convert(@Nonnull final Object aSource) {
            return PDTFactory.createLocalDateTime((DateTime) aSource);
        }
    });
    aRegistry.registerTypeConverter(LocalDate.class, LocalDateTime.class, new ITypeConverter() {
        @Nonnull
        public LocalDateTime convert(@Nonnull final Object aSource) {
            return PDTFactory.createLocalDateTime((LocalDate) aSource);
        }
    });
    aRegistry.registerTypeConverter(LocalTime.class, LocalDateTime.class, new ITypeConverter() {
        @Nonnull
        public LocalDateTime convert(@Nonnull final Object aSource) {
            return PDTFactory.createLocalDateTime((LocalTime) aSource);
        }
    });

    // LocalDate
    aRegistry.registerTypeConverter(aSourceClasses, LocalDate.class, new ITypeConverter() {
        @Nonnull
        public LocalDate convert(@Nonnull final Object aSource) {
            return new LocalDate(aSource, PDTConfig.getDefaultChronology());
        }
    });
    aRegistry.registerTypeConverter(DateTime.class, LocalDate.class, new ITypeConverter() {
        @Nonnull
        public LocalDate convert(@Nonnull final Object aSource) {
            return PDTFactory.createLocalDate((DateTime) aSource);
        }
    });
    aRegistry.registerTypeConverter(LocalDateTime.class, LocalDate.class, new ITypeConverter() {
        @Nonnull
        public LocalDate convert(@Nonnull final Object aSource) {
            return PDTFactory.createLocalDate((LocalDateTime) aSource);
        }
    });

    // LocalTime
    aRegistry.registerTypeConverter(aSourceClasses, LocalTime.class, new ITypeConverter() {
        @Nonnull
        public LocalTime convert(@Nonnull final Object aSource) {
            return new LocalTime(aSource, PDTConfig.getDefaultChronology());
        }
    });
    aRegistry.registerTypeConverter(DateTime.class, LocalTime.class, new ITypeConverter() {
        @Nonnull
        public LocalTime convert(@Nonnull final Object aSource) {
            return PDTFactory.createLocalTime((DateTime) aSource);
        }
    });
    aRegistry.registerTypeConverter(LocalDateTime.class, LocalTime.class, new ITypeConverter() {
        @Nonnull
        public LocalTime convert(@Nonnull final Object aSource) {
            return PDTFactory.createLocalTime((LocalDateTime) aSource);
        }
    });

    // Duration
    aRegistry.registerTypeConverter(new Class<?>[] { String.class, AtomicInteger.class, AtomicLong.class,
            BigDecimal.class, BigInteger.class, Byte.class, Double.class, Float.class, Integer.class,
            Long.class, Short.class }, Duration.class, new ITypeConverter() {
                @Nonnull
                public Duration convert(@Nonnull final Object aSource) {
                    return new Duration(aSource);
                }
            });

    // Period
    aRegistry.registerTypeConverter(new Class<?>[] { String.class, AtomicInteger.class, AtomicLong.class,
            BigDecimal.class, BigInteger.class, Byte.class, Double.class, Float.class, Integer.class,
            Long.class, Short.class }, Period.class, new ITypeConverter() {
                @Nonnull
                public Period convert(@Nonnull final Object aSource) {
                    return new Period(aSource);
                }
            });

    // MutablePeriod
    aRegistry.registerTypeConverter(new Class<?>[] { String.class, AtomicInteger.class, AtomicLong.class,
            BigDecimal.class, BigInteger.class, Byte.class, Double.class, Float.class, Integer.class,
            Long.class, Short.class }, MutablePeriod.class, new ITypeConverter() {
                @Nonnull
                public MutablePeriod convert(@Nonnull final Object aSource) {
                    return new MutablePeriod(aSource);
                }
            });
}

From source file:com.helger.datetime.holiday.CalendarUtil.java

License:Apache License

/**
 * Searches for the occurrences of a month/day in one chronology within one
 * gregorian year.//from w w w. j a  v  a  2s .  c o m
 *
 * @param nTargetMonth
 *        Target month
 * @param nTargetDay
 *        Target day
 * @param nGregorianYear
 *        Gregorian year
 * @param aTargetChronoUTC
 *        Target chronology
 * @return the list of gregorian dates.
 */
@Nonnull
public static Set<LocalDate> getDatesFromChronologyWithinGregorianYear(final int nTargetMonth,
        final int nTargetDay, final int nGregorianYear, final Chronology aTargetChronoUTC) {
    final Set<LocalDate> aHolidays = new HashSet<LocalDate>();
    final LocalDate aFirstGregorianDate = PDTFactory.createLocalDate(nGregorianYear, DateTimeConstants.JANUARY,
            1);
    final LocalDate aLastGregorianDate = PDTFactory.createLocalDate(nGregorianYear, DateTimeConstants.DECEMBER,
            31);

    final LocalDate aFirstTargetDate = new LocalDate(
            aFirstGregorianDate.toDateTimeAtStartOfDay(PDTConfig.getDateTimeZoneUTC()).getMillis(),
            aTargetChronoUTC);
    final LocalDate aLastTargetDate = new LocalDate(
            aLastGregorianDate.toDateTimeAtStartOfDay(PDTConfig.getDateTimeZoneUTC()).getMillis(),
            aTargetChronoUTC);

    final Interval aInterv = new Interval(
            aFirstTargetDate.toDateTimeAtStartOfDay(PDTConfig.getDateTimeZoneUTC()),
            aLastTargetDate.plusDays(1).toDateTimeAtStartOfDay(PDTConfig.getDateTimeZoneUTC()));

    for (int nTargetYear = aFirstTargetDate.getYear(); nTargetYear <= aLastTargetDate
            .getYear(); ++nTargetYear) {
        final LocalDate aLocalDate = new LocalDate(nTargetYear, nTargetMonth, nTargetDay, aTargetChronoUTC);
        if (aInterv.contains(aLocalDate.toDateTimeAtStartOfDay(PDTConfig.getDateTimeZoneUTC()))) {
            aHolidays.add(convertToGregorianDate(aLocalDate));
        }
    }
    return aHolidays;
}

From source file:com.helger.datetime.PDTFactory.java

License:Apache License

@Nonnull
public static LocalDate createLocalDateFromMillis(final long nMillis) {
    return new LocalDate(nMillis, getLocalChronology());
}

From source file:com.helger.datetime.PDTFactory.java

License:Apache License

/**
 * Parse string using ISO format/*  www  . j  ava 2 s.  co  m*/
 *
 * @param sDate
 *        A date in the format yyyy-MM-dd
 * @return the {@link LocalDate}
 */
@Nonnull
public static LocalDate createLocalDate(@Nonnull final String sDate) {
    return new LocalDate(sDate, getLocalChronology());
}

From source file:com.helger.datetime.PDTFactory.java

License:Apache License

/**
 * Creates a LocalDate. Does not use the Chronology of the Calendar.
 *
 * @param aCalendar//from   ww w.  ja v a2s  .  c  o  m
 *        The calendar to be converted.
 * @return The local date representing the provided date.
 */
@Nonnull
public static LocalDate createLocalDate(@Nonnull final Calendar aCalendar) {
    return new LocalDate(aCalendar,
            getLocalChronology().withZone(DateTimeZone.forTimeZone(aCalendar.getTimeZone())));
}

From source file:com.helger.datetime.PDTFactory.java

License:Apache License

@Nonnull
public static LocalDate createLocalDate(@Nonnull final Date aDate, final DateTimeZone aDateTimeZone) {
    return new LocalDate(aDate, getLocalChronology().withZone(aDateTimeZone));
}

From source file:com.jjlharrison.jollyday.util.CalendarUtil.java

License:Apache License

/**
 * Creates a LocalDate. Does not use the Chronology of the Calendar.
 *
 * @param c/*from w  w  w.  j  a v  a2  s.co m*/
 *            a {@link java.util.Calendar} object.
 * @return The local date representing the provided date.
 */
public LocalDate create(final Calendar c) {
    return new LocalDate(c, ISOChronology.getInstance());
}

From source file:com.jjlharrison.jollyday.util.CalendarUtil.java

License:Apache License

/**
 * Converts the provided date into a date within the ISO chronology. If it
 * is already a ISO date it will return it.
 *
 * @param date/* w ww. j  av  a  2  s .c o  m*/
 *            a {@link org.joda.time.LocalDate} object.
 * @return a {@link org.joda.time.LocalDate} object.
 */
public LocalDate convertToISODate(final LocalDate date) {
    if (!(date.getChronology() instanceof ISOChronology)) {
        return new LocalDate(date.toDateTimeAtStartOfDay().getMillis(), ISOChronology.getInstance());
    }
    return date;
}

From source file:com.ning.billing.invoice.generator.DefaultInvoiceGenerator.java

License:Apache License

private List<InvoiceItem> processEvents(final UUID invoiceId, final UUID accountId,
        final BillingEvent thisEvent, @Nullable final BillingEvent nextEvent, final LocalDate targetDate,
        final Currency currency, final StringBuilder logStringBuilder) throws InvoiceApiException {
    final List<InvoiceItem> items = new ArrayList<InvoiceItem>();

    // Handle fixed price items
    final InvoiceItem fixedPriceInvoiceItem = generateFixedPriceItem(invoiceId, accountId, thisEvent,
            targetDate, currency);/* w  w w. j  av  a 2 s.c om*/
    if (fixedPriceInvoiceItem != null) {
        items.add(fixedPriceInvoiceItem);
    }

    // Handle recurring items
    final BillingPeriod billingPeriod = thisEvent.getBillingPeriod();
    if (billingPeriod != BillingPeriod.NO_BILLING_PERIOD) {
        final BillingMode billingMode = instantiateBillingMode(thisEvent.getBillingMode());
        final LocalDate startDate = new LocalDate(thisEvent.getEffectiveDate(), thisEvent.getTimeZone());

        if (!startDate.isAfter(targetDate)) {
            final LocalDate endDate = (nextEvent == null) ? null
                    : new LocalDate(nextEvent.getEffectiveDate(), nextEvent.getTimeZone());

            final int billCycleDayLocal = thisEvent.getBillCycleDayLocal();

            final List<RecurringInvoiceItemData> itemData;
            try {
                itemData = billingMode.calculateInvoiceItemData(startDate, endDate, targetDate,
                        billCycleDayLocal, billingPeriod);
            } catch (InvalidDateSequenceException e) {
                throw new InvoiceApiException(ErrorCode.INVOICE_INVALID_DATE_SEQUENCE, startDate, endDate,
                        targetDate);
            }

            for (final RecurringInvoiceItemData itemDatum : itemData) {
                final BigDecimal rate = thisEvent.getRecurringPrice();

                if (rate != null) {
                    final BigDecimal amount = itemDatum.getNumberOfCycles().multiply(rate)
                            .setScale(NUMBER_OF_DECIMALS, ROUNDING_MODE);

                    final RecurringInvoiceItem recurringItem = new RecurringInvoiceItem(invoiceId, accountId,
                            thisEvent.getSubscription().getBundleId(), thisEvent.getSubscription().getId(),
                            thisEvent.getPlan().getName(), thisEvent.getPlanPhase().getName(),
                            itemDatum.getStartDate(), itemDatum.getEndDate(), amount, rate, currency);
                    items.add(recurringItem);
                }
            }
        }
    }

    // For debugging purposes
    logStringBuilder.append("\n").append(thisEvent);
    for (final InvoiceItem item : items) {
        logStringBuilder.append("\n\t").append(item);
    }

    return items;
}