Example usage for org.joda.time DateTime parse

List of usage examples for org.joda.time DateTime parse

Introduction

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

Prototype

public static DateTime parse(String str, DateTimeFormatter formatter) 

Source Link

Document

Parses a DateTime from the specified string using a formatter.

Usage

From source file:org.entcore.feeder.timetable.udt.UDTImporter.java

License:Open Source License

void initHolidays(JsonObject currentEntity) {
    DateTime s = DateTime.parse(currentEntity.getString("debut"), DateTimeFormat.forPattern(DATE_FORMAT));
    DateTime e = DateTime.parse(currentEntity.getString("fin"), DateTimeFormat.forPattern(DATE_FORMAT));
    while (s.isBefore(e)) {
        holidays.add(s);/*from   ww w  .  j av  a2 s.  c  o m*/
        s = s.plusDays(1);
        holidaysWeeks.add(s.getWeekOfWeekyear());
    }
    holidays.add(e);
}

From source file:org.fenixedu.academic.api.infra.FenixAPICanteen.java

License:Open Source License

public static String get(String daySearch) {

    String locale = I18N.getLocale().toString().replace("_", "-");
    if (canteenInfo == null || canteenInfo.isJsonNull() || oldInformation()) {

        String canteenUrl = FenixEduAcademicConfiguration.getConfiguration().getFenixApiCanteenUrl();
        try {/* www  . j  a  v a 2  s .  c o m*/
            Response response = HTTP_CLIENT.target(canteenUrl).request(MediaType.APPLICATION_JSON)
                    .header("Authorization", getServiceAuth()).get();

            if (response.getStatus() == 200) {
                JsonParser parser = new JsonParser();
                canteenInfo = (JsonObject) parser.parse(response.readEntity(String.class));
                day = new DateTime();
            } else {
                return new JsonObject().toString();
            }
        } catch (ProcessingException e) {
            e.printStackTrace();
            return new JsonObject().toString();
        }
    }

    JsonArray jsonArrayWithLang = canteenInfo.getAsJsonArray(locale);

    DateTime dayToCompareStart;
    DateTime dayToCompareEnd;

    DateTime dateTime = DateTime.parse(daySearch, DateTimeFormat.forPattern(datePattern));
    int dayOfWeek = dateTime.getDayOfWeek();
    if (dayOfWeek != 7) {
        dayToCompareStart = dateTime.minusDays(dayOfWeek);
        dayToCompareEnd = dateTime.plusDays(7 - dayOfWeek);
    } else {
        dayToCompareStart = dateTime;
        dayToCompareEnd = dateTime.plusDays(7);
    }

    JsonArray jsonResult = new JsonArray();
    for (JsonElement jObj : jsonArrayWithLang) {

        DateTime dateToCompare = DateTime.parse(((JsonObject) jObj).get("day").getAsString(),
                DateTimeFormat.forPattern(datePattern));

        if (dateToCompare.isAfter(dayToCompareStart) && dateToCompare.isBefore(dayToCompareEnd)) {
            jsonResult.add(jObj);
        }
    }
    Gson gson = new GsonBuilder().setPrettyPrinting().create();

    return gson.toJson(jsonResult);

}

From source file:org.fenixedu.academic.api.infra.FenixAPIFromExternalServer.java

License:Open Source License

public static String getCanteen(String daySearch) {
    getInformation();/*from  ww  w  .java2s.  co m*/

    String lang = I18N.getLocale().toLanguageTag();

    if (!canteenInfo.has(lang)) {
        return empty.toString();
    }
    JsonArray jsonArrayWithLang = canteenInfo.getAsJsonObject().getAsJsonArray(lang);

    DateTime dayToCompareStart;
    DateTime dayToCompareEnd;

    DateTime dateTime = DateTime.parse(daySearch, DateTimeFormat.forPattern(datePattern));
    int dayOfWeek = dateTime.getDayOfWeek();
    if (dayOfWeek != 7) {
        dayToCompareStart = dateTime.minusDays(dayOfWeek);
        dayToCompareEnd = dateTime.plusDays(7 - dayOfWeek);
    } else {
        dayToCompareStart = dateTime;
        dayToCompareEnd = dateTime.plusDays(7);
    }

    Interval validInterval = new Interval(dayToCompareStart, dayToCompareEnd);
    JsonArray jsonResult = new JsonArray();

    for (JsonElement jObj : jsonArrayWithLang) {

        DateTime dateToCompare = DateTime.parse(((JsonObject) jObj).get("day").getAsString(),
                DateTimeFormat.forPattern(datePattern));

        if (validInterval.contains(dateToCompare)) {
            jsonResult.add(jObj);
        }
    }

    return gson.toJson(jsonResult);
}

From source file:org.fenixedu.spaces.ui.services.OccupationService.java

License:Open Source License

private OccupationConfig parseConfig(String config, List<Interval> intervals) {
    JsonObject json = jsonParser.parse(config).getAsJsonObject();

    DateTime start = DateTime.parse(json.get("start").getAsString(), datetimeFormatter);
    DateTime end = DateTime.parse(json.get("end").getAsString(), datetimeFormatter);
    String jsonFrequency = json.get("frequency").getAsString();
    Boolean allDay = json.get("isAllDay").getAsBoolean();
    Integer repeatsEvery = null;//from w w  w.java  2  s.co  m
    List<Integer> weekdays = null;
    MonthlyType monthlyType = null;
    Frequency frequency = null;

    switch (jsonFrequency) {
    case "n":
        frequency = Frequency.NEVER;
        break;
    case "d":
        frequency = Frequency.DAILY;
        repeatsEvery = json.get("repeatsevery").getAsInt();
        break;
    case "w":
        frequency = Frequency.WEEKLY;
        repeatsEvery = json.get("repeatsevery").getAsInt();
        weekdays = new ArrayList<>();
        for (JsonElement day : json.get("weekdays").getAsJsonArray()) {
            weekdays.add(day.getAsInt());
        }
        break;
    case "m":
        frequency = Frequency.MONTHLY;
        repeatsEvery = json.get("repeatsevery").getAsInt();
        monthlyType = json.get("monthlyType").getAsString().equals("dayofmonth") ? MonthlyType.DAY_OF_MONTH
                : MonthlyType.DAY_OF_WEEK;
        break;
    case "y":
        frequency = Frequency.YEARLY;
        repeatsEvery = json.get("repeatsevery").getAsInt();
        break;
    }

    return new ExplicitConfigWithSettings(start, end, allDay, repeatsEvery, frequency, weekdays, monthlyType,
            intervals);
}

From source file:org.filteredpush.qc.date.DateUtils.java

License:Apache License

/**
 * Given a string that may be a date or a date range, extract a interval of
 * dates from that date range, up to the end milisecond of the last day.
 * /*from w  w w.j  av  a  2  s.  c o m*/
 * @see DateUtils#extractDateInterval(String) which returns a pair of DateMidnights.
 * 
 * @param eventDate a string containing a dwc:eventDate from which to extract an interval.
 * @return an interval from the beginning of event date to the end of event date.
 */
public static Interval extractInterval(String eventDate) {
    Interval result = null;
    DateTimeParser[] parsers = { DateTimeFormat.forPattern("yyyy-MM").getParser(),
            DateTimeFormat.forPattern("yyyy").getParser(),
            ISODateTimeFormat.dateOptionalTimeParser().getParser() };
    DateTimeFormatter formatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter();
    if (eventDate != null && eventDate.contains("/") && isRange(eventDate)) {
        String[] dateBits = eventDate.split("/");
        try {
            // must be at least a 4 digit year.
            if (dateBits[0].length() > 3 && dateBits[1].length() > 3) {
                DateMidnight startDate = DateMidnight.parse(dateBits[0], formatter);
                DateTime endDate = DateTime.parse(dateBits[1], formatter);
                logger.debug(startDate);
                logger.debug(endDate);
                if (dateBits[1].length() == 4) {
                    result = new Interval(startDate, endDate.plusMonths(12).minus(1l));
                } else if (dateBits[1].length() == 7) {
                    result = new Interval(startDate, endDate.plusMonths(1).minus(1l));
                } else {
                    result = new Interval(startDate, endDate.plusDays(1).minus(1l));
                }
                logger.debug(result);
            }
        } catch (Exception e) {
            // not a date range
            logger.error(e.getMessage());
        }
    } else {
        try {
            DateMidnight startDate = DateMidnight.parse(eventDate, formatter);
            logger.debug(startDate);
            if (eventDate.length() == 4) {
                DateTime endDate = startDate.toDateTime().plusMonths(12).minus(1l);
                result = new Interval(startDate, endDate);
                logger.debug(result);
            } else if (eventDate.length() == 7) {
                DateTime endDate = startDate.toDateTime().plusMonths(1).minus(1l);
                result = new Interval(startDate, endDate);
                logger.debug(result);
            } else {
                DateTime endDate = startDate.toDateTime().plusDays(1).minus(1l);
                result = new Interval(startDate, endDate);
                logger.debug(result);
            }
        } catch (Exception e) {
            // not a date
            logger.error(e.getMessage());
        }
    }
    return result;
}

From source file:org.freelectron.leobel.testlwa.models.message.request.context.Alert.java

License:Open Source License

@JsonCreator
public Alert(@JsonProperty("token") String token, @JsonProperty("type") SetAlert.AlertType type,
        @JsonProperty("scheduledTime") String scheduledTime) {
    this.token = token;
    this.type = type;
    this.scheduledTime = DateTime.parse(scheduledTime, DateUtils.AVS_ISO_OFFSET_DATE_TIME);
}

From source file:org.freelectron.leobel.testlwa.models.message.response.alerts.SetAlert.java

License:Open Source License

@JsonProperty("scheduledTime")
public void setScheduledTime(String dateTime) {
    scheduledTime = DateTime.parse(dateTime, DateUtils.AVS_ISO_OFFSET_DATE_TIME);
}

From source file:org.fuin.objects4j.common.DateTimeAdapter.java

License:Open Source License

@Override
public final DateTime unmarshal(final String str) {
    if (str == null) {
        return null;
    }//from ww w . j a va 2  s .  co m
    return DateTime.parse(str, ISODateTimeFormat.dateTimeParser());
}

From source file:org.fuin.objects4j.common.DateTimeAdapter.java

License:Open Source License

@Override
public final DateTime convertToEntityAttribute(final String str) {
    if (str == null) {
        return null;
    }// w  w  w  .  j av  a2 s.  c om
    return DateTime.parse(str, ISODateTimeFormat.dateTimeParser());
}

From source file:org.georchestra.analytics.StatisticsController.java

License:Open Source License

/**
 * Calculates the appropriate granularity given the begin date and the end date.
 *
 * @param beginDate the begin date.//www  . j  a v  a  2s .  c o  m
 * @param endDate the end date.
 * @return the most relevant GRANULARITY.
 */
private GRANULARITY guessGranularity(String beginDate, String endDate) {
    DateTime from = DateTime.parse(beginDate, this.dbOutputFormatter);
    DateTime to = DateTime.parse(endDate, this.dbOutputFormatter);

    Duration duration = new Duration(from, to);
    long numdays = duration.getStandardDays();
    if (numdays < 2) {
        return GRANULARITY.HOUR;
    } else if (numdays < 90) {
        return GRANULARITY.DAY;
    } else if (numdays < 365) {
        return GRANULARITY.WEEK;
    } else {
        return GRANULARITY.MONTH;
    }
}