Example usage for org.joda.time LocalDate toDateTimeAtStartOfDay

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

Introduction

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

Prototype

public DateTime toDateTimeAtStartOfDay(DateTimeZone zone) 

Source Link

Document

Converts this LocalDate to a full datetime at the earliest valid time for the date using the specified time zone.

Usage

From source file:org.apache.isis.runtimes.dflt.objectstores.sql.jdbc.JdbcConnector.java

License:Apache License

private void addPreparedValues(final PreparedStatement statement) throws SQLException {
    if (queryValues.size() > 0) {
        int i = 1;
        try {//from w  w  w .  j  a  v  a 2 s .co m
            for (final Object value : queryValues) {
                if (value instanceof LocalDate) {
                    try {
                        statement.setObject(i, value, java.sql.Types.DATE);
                    } catch (final SQLException e) {
                        // TODO This daft catch is required my MySQL, which
                        // also requires the TimeZone offset to be
                        // "undone"
                        final LocalDate localDate = (LocalDate) value;
                        final int millisOffset = -DateTimeZone.getDefault().getOffset(null);
                        final java.util.Date javaDate = localDate
                                .toDateTimeAtStartOfDay(DateTimeZone.forOffsetMillis(millisOffset)).toDate();

                        statement.setObject(i, javaDate, java.sql.Types.DATE);
                    }
                } else {
                    statement.setObject(i, value);
                }
                i++;
            }
        } catch (final SQLException e) {
            LOG.error("Error adding prepared value " + i + " of type "
                    + queryValues.get(i - 1).getClass().getSimpleName(), e);
            throw e;
        }
    }
}

From source file:org.killbill.billing.plugin.bitcoin.osgi.http.PaymentRequestServlet.java

License:Apache License

private long localDateToMillis(LocalDate localDate) {
    return localDate.toDateTimeAtStartOfDay(DateTimeZone.UTC).getMillis();
}

From source file:org.killbill.billing.plugin.simpletax.config.ConvertionHelpers.java

License:Apache License

/**
 * Converts a local date from an origin? time zone to a target? time zone,
 * assuming that the date actually refers to the first instant of the
 * designated day, in the origin? time zone.
 * <p>/*from   w  ww.ja  va 2 s .co  m*/
 * This is useful to know which day it was in target? time zone when the
 * designated day started in the origin? time zone.
 *
 * @param localDate
 *            A date specification, to be interpreted in the
 *            {@code fromTimeZone}. Never {@code null}.
 * @param originTimeZone
 *            The zone in which the partial date should be interpreted?.
 *            Never {@code null}.
 * @param targetTimeZone
 *            The zone in which the first instant of the day is requested.
 *            Never {@code null}.
 * @return The date in {@code toTimeZone} for the first instant of the day
 *         {@code localDate} in the time zone {@code fromTimeZone}
 */
public static LocalDate convertTimeZone(LocalDate localDate, DateTimeZone originTimeZone,
        DateTimeZone targetTimeZone) {
    checkNotNull(originTimeZone);
    checkNotNull(targetTimeZone);
    return localDate.toDateTimeAtStartOfDay(originTimeZone).withZone(targetTimeZone).toLocalDate();
}

From source file:se.toxbee.sleepfighter.text.DateTextUtils.java

License:Open Source License

/**
 * Builds and returns text for the "exact" time an alarm occurs as opposed to the period left for it to occur.<br/>
 * In English, 12:00 today would become "Today 12:00", tomorrow would be come "Tomorrow 12:00", and on Monday it would become
 *
 * @param formats the formats to use, e.g: [Today %1$s, Tomorrow %1$s, %2$s %1$s].
 * @param noActive if no alarm was active, this is used.
 * @param now current time in Unix epoch timestamp.
 * @param ats an AlarmTimestamp: the information about the alarm & its timestamp.
 * @param locale the locale to use for weekdays.
 * @return the built time-to string./*from  w  ww  .  j  a v a  2  s. com*/
 */
public static final String getTime(String[] formats, String noActive, long now, AlarmTimestamp ats,
        Locale locale) {

    if (ats == AlarmTimestamp.INVALID) {
        return noActive;
    } else {
        if (ats.getMillis() < now) {
            throw new RuntimeException("Time given is before now.");
        }

        // Prepare replacements.
        Alarm alarm = ats.getAlarm();
        String timeReplacement = StringUtils.joinTime(alarm.getHour(), alarm.getMinute());

        // Calculate start of tomorrow.
        DateTime nowTime = new DateTime(now);
        DateTime time = new DateTime(ats.getMillis());
        LocalDate tomorrow = new LocalDate(nowTime).plusDays(1);
        DateTime startOfTomorrow = tomorrow.toDateTimeAtStartOfDay(nowTime.getZone());

        if (time.isBefore(startOfTomorrow)) {
            // Alarm is today.
            Log.d(TAG, "today");
            return String.format(formats[0], timeReplacement);
        }

        // Calculate start of the day after tomorrow.
        LocalDate afterTomorrow = tomorrow.plusDays(1);
        DateTime startOfAfterTomorrow = afterTomorrow.toDateTimeAtStartOfDay(nowTime.getZone());

        if (time.isBefore(startOfAfterTomorrow)) {
            Log.d(TAG, "tomorrow");
            // Alarm is tomorrow.
            return String.format(formats[1], timeReplacement);
        }

        // Alarm is after tomorrow.
        Log.d(TAG, "after tomorrow");
        String weekday = new DateTime(ats.getMillis()).dayOfWeek().getAsText(locale);
        return String.format(formats[2], timeReplacement, weekday);
    }
}