Example usage for org.joda.time LocalDateTime LocalDateTime

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

Introduction

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

Prototype

public LocalDateTime(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour,
        int secondOfMinute, int millisOfSecond) 

Source Link

Document

Constructs an instance set to the specified date and time using ISOChronology.

Usage

From source file:be.wegenenverkeer.common.resteasy.json.Iso8601NozoneFormat.java

License:Apache License

/**
 * Parse a date from ISO-8601 formatted string. It expects a format yyyy-MM-ddTHH:mm:ss[.sss][Z|[+-]HH:mm]
 * The timezone is not applied when present. It is expected that the timezone on both ends is the same.
 *
 * @param date ISO string to parse in the appropriate format.
 * @param parseTimezone true when the timezone also needs to be parsed for correctness
 * @return the parsed date/* w ww  . ja  v  a2  s .com*/
 * @throws IllegalArgumentException if the date is not in the appropriate format
 */
// CHECKSTYLE INNER_ASSIGNMENT: OFF
public Date parse(String date, boolean parseTimezone) throws IllegalArgumentException {
    int max = date.length();
    try {
        int offset = 0;

        // extract year
        int year = parseInt(date, offset, offset + 4);
        offset += 4;
        checkOffset(date, offset, '-');
        offset += 1;

        // extract month
        int month = parseInt(date, offset, offset + 2);
        offset += 2;
        checkOffset(date, offset, '-');
        offset += 1;

        // extract day
        int day = parseInt(date, offset, offset + 2);
        offset += 2;

        int hour = 0;
        int minutes = 0;
        int seconds = 0;
        int milliseconds = 0; // always use 0 otherwise returned date will include millis of current time

        if (offset < max) { // time can be optional
            checkOffset(date, offset, 'T');
            offset += 1;
            // extract hours, minutes, seconds and milliseconds
            hour = parseInt(date, offset, offset + 2);
            offset += 2;
            checkOffset(date, offset, ':');
            offset += 1;

            minutes = parseInt(date, offset, offset + 2);
            offset += 2;
            checkOffset(date, offset, ':');
            offset += 1;

            seconds = parseInt(date, offset, offset + 2);
            offset += 2;
            // milliseconds can be optional in the format

            if (offset < max) { // milliseconds are be optional
                if (date.charAt(offset) == '.') {
                    checkOffset(date, offset, '.');
                    offset += 1;
                    milliseconds = parseInt(date, offset, offset + 3);
                    offset += 3;
                }
            }
        }

        // extract timezone
        if (parseTimezone && offset < max) {
            String timezoneId;
            char timezoneIndicator = date.charAt(offset);
            if (timezoneIndicator == '+' || timezoneIndicator == '-') {
                timezoneId = GMT_ID + date.substring(offset);
            } else if (timezoneIndicator == 'Z') {
                timezoneId = GMT_ID;
            } else {
                throw new IndexOutOfBoundsException("Invalid time zone indicator " + timezoneIndicator);
            }
            TimeZone timezone = TimeZone.getTimeZone(timezoneId);
            if (!timezone.getID().equals(timezoneId)) {
                throw new IndexOutOfBoundsException();
            }
        }

        return new LocalDateTime(year, month, day, hour, minutes, seconds, milliseconds).toDate();
    } catch (IndexOutOfBoundsException e) {
        throw new IllegalArgumentException(PARSE_FAILED + date, e);
    } catch (NumberFormatException e) {
        throw new IllegalArgumentException(PARSE_FAILED + date, e);
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException(PARSE_FAILED + date, e);
    }
}

From source file:cherry.goods.util.JodaTimeUtil.java

License:Apache License

/**
 * @param ts ???{@link Timestamp}/*from  w w w .  j a  va 2s . c  o m*/
 * @return ?????{@link LocalDateTime}?????????
 */
public static LocalDateTime getLocalDateTime(Timestamp ts) {
    Calendar cal = getCalendar(ts);
    return new LocalDateTime(cal.get(YEAR), cal.get(MONTH) + 1, cal.get(DAY_OF_MONTH), cal.get(HOUR_OF_DAY),
            cal.get(MINUTE), cal.get(SECOND), cal.get(MILLISECOND));
}

From source file:com.gs.fw.common.mithra.test.cacheloader.PYETopLevelLoaderFactory.java

License:Apache License

protected Timestamp shiftBusinessDate(Timestamp businessDate) {
    LocalDate localDate = new LocalDate(businessDate);
    int year = localDate.getYear();
    LocalDateTime pye = new LocalDateTime(year - 1, 12, 31, 23, 59, 0, 0);
    int dayOfWeek = pye.dayOfWeek().get();
    if (dayOfWeek > 5) {
        pye = pye.minusDays(dayOfWeek - 5);
    }/*from   w ww .  ja v  a2 s  .  c o m*/
    return new Timestamp(pye.toDateTime().getMillis());
}

From source file:com.ramzcalender.RWeekCalendar.java

License:Open Source License

public void startDate(int year, int month, int date) {
    mStartDate = new LocalDateTime(year, month, date, 0, 0, 0, 0);
    start = new DateTime(year, month, date, 0, 0, 0, 0);
}

From source file:de.jpaw.bonaparte.core.CompactByteArrayParser.java

License:Apache License

@Override
public LocalDateTime readDayTime(TemporalElementaryDataItem di) throws MessageParserException {
    if (checkForNull(di))
        return null;
    boolean fractional = false;
    int c = needToken();
    switch (c) {/*www.j av  a  2 s  .  c o m*/
    case COMPACT_DATETIME:
        break;
    case COMPACT_DATETIME_MILLIS:
        fractional = true;
        break;
    default:
        throw newMPE(MessageParserException.UNEXPECTED_CHARACTER,
                String.format("(expected COMPACT_DATETIME_*, got 0x%02x)", c));
    }
    String fieldname = di.getName();
    int year = readInt(needToken(), fieldname);
    int month = readInt(needToken(), fieldname);
    int day = readInt(needToken(), fieldname);
    int secondsOfDay = readInt(needToken(), fieldname);
    int millis = 0;
    if (fractional) {
        millis = secondsOfDay % 1000;
        secondsOfDay /= 1000;
    }
    return new LocalDateTime(year, month, day, secondsOfDay / 3600, (secondsOfDay % 3600) / 60,
            secondsOfDay % 60, millis);
}

From source file:de.jpaw.bonaparte.core.ExternalizableParser.java

License:Apache License

@Override
public LocalDateTime readDayTime(TemporalElementaryDataItem di) throws IOException {
    if (checkForNull(di)) {
        return null;
    }//from w ww.  ja v a2 s  . c  o m
    String fieldname = di.getName();
    int fractional = 0;
    byte c = nextByte();
    if ((c > FRAC_SCALE_0) && (c <= FRAC_SCALE_18)) {
        // read fractional part
        long fraction = readLongNoNull(fieldname);
        int scale = c - FRAC_SCALE_0;
        if (scale > 9) {
            fraction /= powersOfTen[scale - 9];
        } else if (scale < 9) {
            fraction *= powersOfTen[9 - scale];
        }
        fractional = (int) fraction;
    } else {
        pushBack(c);
    }
    int date = readVarInt(fieldname, 32);
    if ((date < 0) || (fractional < 0)) {
        throw new IOException(String.format("negative numbers found for date field: %d.%d in %s.%s", date,
                fractional, currentClass, fieldname));
    }
    // set the date and time
    int day, month, year, hour, minute, second;
    year = date / 10000;
    month = (date %= 10000) / 100;
    day = date %= 100;
    if (di.getHhmmss()) {
        hour = fractional / 10000000;
        minute = (fractional %= 10000000) / 100000;
        second = (fractional %= 100000) / 1000;
    } else {
        hour = fractional / 3600000;
        minute = (fractional %= 3600000) / 60000;
        second = (fractional %= 60000) / 1000;
    }
    fractional %= 1000;
    // first checks
    if ((year < 1601) || (year > 2399) || (month == 0) || (month > 12) || (day == 0) || (day > 31)) {
        throw new IOException(String.format("ILLEGAL DAY: found %d-%d-%d in %s.%s", year, month, day,
                currentClass, fieldname));
    }
    if ((hour > 23) || (minute > 59) || (second > 59)) {
        throw new IOException(String.format("ILLEGAL TIME: found %d:%d:%d in %s.%s", hour, minute, second,
                currentClass, fieldname));
    }
    // now set the return value
    LocalDateTime result;
    try {
        // TODO! default is lenient mode, therefore will not check. Solution
        // is to read the data again and compare the values of day, month
        // and year
        result = new LocalDateTime(year, month, day, hour, minute, second, fractional);
    } catch (Exception e) {
        throw new IOException(String.format("exception creating LocalDateTime for %d-%d-%d in %s.%s", year,
                month, day, currentClass, fieldname));
    }
    return result;
}

From source file:fm.last.commons.lang.time.XmlGregorianCalendarUtils.java

License:Apache License

/**
 * Converts from an XMLGregorianCalendar to a LocalDateTime i.e. represents a date and time without timezone
 * inform.ation/*  w  w w  .  j  a v a2  s .  co  m*/
 * 
 * @param calendar XMLGregorianCalendar object.
 * @return The calendar converted to a LocalDateTime.
 */
public static LocalDateTime convertToLocalDateTime(XMLGregorianCalendar calendar) {
    if (calendar == null) {
        return null;
    }
    int year = calendar.getYear() > 0 ? calendar.getYear() : 0;
    int hour = calendar.getHour() > 0 ? calendar.getHour() : 0;
    int minute = calendar.getMinute() > 0 ? calendar.getMinute() : 0;
    int second = calendar.getSecond() > 0 ? calendar.getSecond() : 0;
    int millisecond = calendar.getMillisecond() > 0 ? calendar.getMillisecond() : 0;

    return new LocalDateTime(year, calendar.getMonth(), calendar.getDay(), hour, minute, second, millisecond);
}

From source file:net.solarnetwork.node.hw.hc.EM5600Support.java

License:Open Source License

/**
 * Parse a DateTime value from raw Modbus register values. The {@code data}
 * array is expected to have a length of {@code 2} and follow the documented
 * F10 and F9 formats./*  www . j a  v  a  2s  . c  om*/
 * 
 * @param data
 *        the data array
 * @return the parsed date, or <em>null</em> if not available
 */
public static LocalDateTime parseDate(final int[] data) {
    LocalDateTime result = null;
    if (data != null && data.length == 2) {
        int day = (data[0] & 0x1F00) >> 8; // 1 - 31
        int year = 2000 + ((data[1] & 0xFF00) >> 8); // 0 - 255
        int month = (data[1] & 0xF); //1-12
        result = new LocalDateTime(year, month, day, 0, 0, 0, 0);
    }
    return result;
}

From source file:net.solarnetwork.node.hw.schneider.meter.PM3200Support.java

License:Open Source License

/**
 * Parse a DateTime value from raw Modbus register values. The {@code data}
 * array is expected to have a length of {@code 4}.
 * /*from  w ww . ja  va2  s. c  o  m*/
 * @param data
 *        the data array
 * @return the parsed date, or <em>null</em> if not available
 */
public static LocalDateTime parseDateTime(final int[] data) {
    LocalDateTime result = null;
    if (data != null && data.length == 4) {
        int year = 2000 + (data[0] & 0x7F);
        int month = (data[1] & 0xF00) >> 8;
        int day = (data[1] & 0x1F);
        int hour = (data[2] & 0x1F00) >> 8;
        int minute = (data[2] & 0x3F);
        int ms = (data[3]); // this is really seconds + milliseconds
        int sec = ms / 1000;
        ms = ms - (sec * 1000);
        result = new LocalDateTime(year, month, day, hour, minute, sec, ms);
    }
    return result;
}

From source file:org.apache.isis.schema.utils.jaxbadapters.JodaLocalDateTimeXMLGregorianCalendarAdapter.java

License:Apache License

public static LocalDateTime parse(final XMLGregorianCalendar xgc) {
    if (xgc == null)
        return null;

    final int year = xgc.getYear();
    final int month = xgc.getMonth();
    final int day = xgc.getDay();
    final int hour = xgc.getHour();
    final int minute = xgc.getMinute();
    final int second = xgc.getSecond();
    final int millisecond = xgc.getMillisecond();

    return new LocalDateTime(year, month, day, hour, minute, second, millisecond);
}