Example usage for org.joda.time.format DateTimeFormatter parseDateTime

List of usage examples for org.joda.time.format DateTimeFormatter parseDateTime

Introduction

In this page you can find the example usage for org.joda.time.format DateTimeFormatter parseDateTime.

Prototype

public DateTime parseDateTime(String text) 

Source Link

Document

Parses a date-time from the given text, returning a new DateTime.

Usage

From source file:org.onebusaway.admin.search.impl.PulloutStatusFilter.java

License:Apache License

@Override
public boolean apply(VehicleStatus type) {
    if (StringUtils.isNotBlank(type.getPulloutTime()) && StringUtils.isNotBlank(type.getPullinTime())) {
        DateTimeFormatter format = ISODateTimeFormat.dateTimeNoMillis();
        DateTime pulloutTime = format.parseDateTime(type.getPulloutTime());
        DateTime pullinTime = format.parseDateTime(type.getPullinTime());
        DateTime now = new DateTime();
        boolean isActivePullout = pulloutTime.isBeforeNow() || pulloutTime.equals(now);
        boolean isActivePullin = pullinTime.isAfterNow() || pullinTime.equals(now);
        return isActivePullout && isActivePullin;
    }//from ww w  .  ja  v a 2 s .com
    return false;
}

From source file:org.onebusaway.admin.search.impl.TimeWindowFilter.java

License:Apache License

private BigDecimal getTimeDifference(String timeReported) {
    DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
    DateTime lastReportedTime = formatter.parseDateTime(timeReported);
    DateTime now = new DateTime();
    int seconds = Seconds.secondsBetween(lastReportedTime, now).getSeconds();
    BigDecimal difference = new BigDecimal(seconds);
    return difference;
}

From source file:org.onebusaway.admin.util.VehicleStatusBuilder.java

License:Apache License

private String getPullinTime(String pulloutTime, String pullinTime) {
    StringBuilder pullinTimeBuilder = new StringBuilder(extractTime(pullinTime));

    DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis();

    DateTime pulloutDateTime = formatter.parseDateTime(pulloutTime);
    int pulloutDay = pulloutDateTime.getDayOfMonth();

    DateTime pullinDateTime = formatter.parseDateTime(pullinTime);
    int pullinDay = pullinDateTime.getDayOfMonth();

    //Check if pullout time falls on the next day
    if (pulloutDay < pullinDay) {
        pullinTimeBuilder.append(" +1 day");
    }// w w  w.  j a  v a2s.  c  o m

    return pullinTimeBuilder.toString();
}

From source file:org.onebusaway.admin.util.VehicleStatusBuilder.java

License:Apache License

private String extractTime(String date) {
    DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis();
    DateTime dateTime = formatter.parseDateTime(date);
    int hour = dateTime.getHourOfDay();
    String formattedHour = String.format("%02d", hour);
    int minute = dateTime.getMinuteOfHour();
    String formattedMinute = String.format("%02d", minute);
    return formattedHour + ":" + formattedMinute;
}

From source file:org.opendaylight.iotdm.client.util.Onem2mDateTime.java

License:Open Source License

private static DateTime stringToDate(String dateTimeString) {

    DateTime dt = null;//from w  w  w.  java 2 s  .com
    DateTimeFormatter fmt = ISODateTimeFormat.basicDateTimeNoMillis();
    try {
        dt = fmt.parseDateTime(dateTimeString);
    } catch (IllegalArgumentException e) {
        return null;
    }
    return dt;
}

From source file:org.opendaylight.iotdm.client.util.Onem2mDateTime.java

License:Open Source License

public static boolean isValidDateTime(String dateTimeString) {

    DateTimeFormatter fmt = ISODateTimeFormat.basicDateTimeNoMillis();
    try {//from  w ww  .j ava  2s  . c o  m
        DateTime dt = fmt.parseDateTime(dateTimeString);
    } catch (IllegalArgumentException e) {
        return false;
    }
    return true;
}

From source file:org.openehr.rm.datatypes.quantity.datetime.DvDateTimeParser.java

License:LGPL

public static DateTime parseTime(String value) {
    if (value == null) {
        throw new IllegalArgumentException("null value");
    }/* w w  w .j av a  2  s . c  o  m*/
    DateTime dt = null;
    if (value.matches(EXT_TIME)) {
        try {
            //dealing with extended format(complete and partial)               
            dt = eTimeParser.parseDateTime(value);
        } catch (Exception e) {
            throw new IllegalArgumentException("invalid value for time in extended format: " + value);
        }
    } else if (value.matches(BTIME_COMPLETE)) {
        value = value.replace(",", ".");
        try {
            dt = timeFormatter.parseDateTime(padTimeValue(value));
        } catch (Exception e) {
            throw new IllegalArgumentException("invalid value for time in basic format: " + value);
        }
    } else if (value.matches(TIME_PARTIAL)) {
        DateTimeFormatter partial = null;
        int zonePosition = tZonePresent(value);
        if (zonePosition > 0) {
            String timeElem = value.substring(0, zonePosition);
            if (timeElem.length() > 2) {
                partial = new DateTimeFormatterBuilder().appendHourOfDay(2).appendMinuteOfHour(2)
                        .appendTimeZoneOffset("Z", false, 1, 2).toFormatter().withOffsetParsed();
            } else {
                partial = new DateTimeFormatterBuilder().appendHourOfDay(2)
                        .appendTimeZoneOffset("Z", false, 1, 2).toFormatter().withOffsetParsed();
            }
        } else {
            if (value.length() > 2) {
                partial = new DateTimeFormatterBuilder().appendHourOfDay(2).appendMinuteOfHour(2).toFormatter()
                        .withOffsetParsed();
            } else {
                partial = new DateTimeFormatterBuilder().appendHourOfDay(2).toFormatter().withOffsetParsed();
            }
        }
        try {
            dt = partial.parseDateTime(value);
        } catch (Exception e) {
            throw new IllegalArgumentException("invalid partial time value in basic format: " + value);
        }
    } else {
        throw new IllegalArgumentException("invalid format for time: " + value);
    }

    return dt;
}

From source file:org.openehr.rm.datatypes.quantity.datetime.DvDateTimeParser.java

License:LGPL

public static DateTime parseDate(String value) {
    if (value == null) {
        throw new IllegalArgumentException("null value for date");
    }/*  w w  w.  j av  a  2 s  .  c  o m*/
    if (!value.matches(EXT_DATE) && !value.matches(BDATE)) {
        throw new IllegalArgumentException("invalid pattern for date: " + value);
    }
    DateTime dt = null;
    if (value.indexOf("-") > 0 || value.length() == 4) {
        try {
            dt = ISODateTimeFormat.dateElementParser().withOffsetParsed().parseDateTime(value);
        } catch (Exception e) {
            throw new IllegalArgumentException("invalid value for extended date: " + value);
        }
    } else {
        int size = analyseDateString(value);
        DateTimeFormatter formatter = null;
        switch (size) {
        case 1:
            formatter = ISODateTimeFormat.year().withOffsetParsed();
            break;
        case 2:
            formatter = DateTimeFormat.forPattern("yyyyMM").withOffsetParsed();
            break;
        case 3:
            formatter = ISODateTimeFormat.basicDate().withOffsetParsed();
            break;
        }
        try {
            dt = formatter.parseDateTime(value);
        } catch (Exception e) {
            throw new IllegalArgumentException("invalid value for basic date: " + value);
        }
    }
    return dt;
}

From source file:org.openhab.binding.sonos.internal.SonosZonePlayer.java

License:Open Source License

public boolean setAlarm(boolean alarmSwitch) {

    List<SonosAlarm> sonosAlarms = getCurrentAlarmList();

    if (isConfigured()) {

        // find the nearest alarm - take the current time from the Sonos System, not the system where openhab is
        // running

        String currentLocalTime = getTime();
        DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");

        DateTime currentDateTime = fmt.parseDateTime(currentLocalTime);

        Duration shortestDuration = Period.days(10).toStandardDuration();
        SonosAlarm firstAlarm = null;//  ww w  .  j a  v  a2  s.  c o  m

        for (SonosAlarm anAlarm : sonosAlarms) {
            Duration duration = new Duration(currentDateTime, anAlarm.getStartTime());
            if (anAlarm.getStartTime().isBefore(currentDateTime.plus(shortestDuration))
                    && anAlarm.getRoomUUID().equals(udn.getIdentifierString())) {
                shortestDuration = duration;
                firstAlarm = anAlarm;
            }
        }

        // Set the Alarm
        if (firstAlarm != null) {

            if (alarmSwitch) {
                firstAlarm.setEnabled(true);
            } else {
                firstAlarm.setEnabled(false);
            }

            return updateAlarm(firstAlarm);

        } else {
            return false;
        }
    } else {
        return false;
    }

}

From source file:org.openiot.gsn.utils.Helpers.java

License:Open Source License

public static long convertTimeFromIsoToLong(String time) throws Exception {
    DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
    return fmt.parseDateTime(time).getMillis();
}