Example usage for org.joda.time LocalDate now

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

Introduction

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

Prototype

public static LocalDate now() 

Source Link

Document

Obtains a LocalDate set to the current system millisecond time using ISOChronology in the default time zone.

Usage

From source file:me.vertretungsplan.parser.IndiwareMobileParser.java

License:Mozilla Public License

@Override
public SubstitutionSchedule getSubstitutionSchedule()
        throws IOException, JSONException, CredentialInvalidException {
    new LoginHandler(scheduleData, credential, cookieProvider).handleLogin(executor, cookieStore);

    String baseurl = data.getString(PARAM_BASEURL) + "/";

    List<Document> docs = new ArrayList<>();

    HttpResponseException lastException = null;
    for (int i = 0; i < MAX_DAYS; i++) {
        LocalDate date = LocalDate.now().plusDays(i);
        String dateStr = DateTimeFormat.forPattern("yyyyMMdd").print(date);
        String filePrefix = scheduleData.getType() == SubstitutionSchedule.Type.TEACHER ? "PlanLe" : "PlanKl";
        String url = baseurl + "mobdaten/" + filePrefix + dateStr + "" + ".xml?_=" + System.currentTimeMillis();
        try {//  w w  w  . j  ava  2 s.  c  o m
            String xml = httpGet(url, "UTF-8");
            Document doc = Jsoup.parse(xml, url, Parser.xmlParser());
            if (doc.select("kopf datei").text().equals(filePrefix + dateStr + ".xml")) {
                docs.add(doc);
            }
        } catch (HttpResponseException e) {
            lastException = e;
        }
    }
    if (docs.size() == 0 && lastException != null) {
        throw lastException;
    }

    SubstitutionSchedule v = SubstitutionSchedule.fromData(scheduleData);
    for (Document doc : docs) {
        v.addDay(parseDay(doc, colorProvider, scheduleData));
    }

    v.setClasses(getAllClasses());
    v.setTeachers(getAllTeachers());
    v.setWebsite(baseurl + "plankl.html");

    return v;
}

From source file:me.vertretungsplan.parser.IndiwareMobileParser.java

License:Mozilla Public License

@NotNull
private List<String> parseClasses(String filePrefix)
        throws JSONException, IOException, CredentialInvalidException {
    String baseurl = data.getString(PARAM_BASEURL) + "/";
    HttpResponseException lastException = null;
    for (int i = -4; i < MAX_DAYS; i++) {
        LocalDate date = LocalDate.now().plusDays(i);
        String dateStr = DateTimeFormat.forPattern("yyyyMMdd").print(date);
        String url = baseurl + "mobdaten/" + filePrefix + dateStr + ".xml?_=" + System.currentTimeMillis();
        try {//from   w w w.j a  v  a 2s  . c  om
            String xml = httpGet(url, "UTF-8");
            Document doc = Jsoup.parse(xml, url, Parser.xmlParser());

            List<String> classes = new ArrayList<>();
            for (Element klasse : doc.select("Klassen > Kl")) {
                classes.add(klasse.select("Kurz").first().text());
            }
            return classes;
        } catch (HttpResponseException e) {
            lastException = e;
        }
    }
    if (lastException != null) {
        throw lastException;
    } else {
        return new ArrayList<>();
    }
}

From source file:me.vertretungsplan.parser.IndiwareStundenplan24Parser.java

License:Mozilla Public License

@Override
public SubstitutionSchedule getSubstitutionSchedule()
        throws IOException, JSONException, CredentialInvalidException {

    String baseurl;/*from   w ww .  java  2s .  c o m*/
    boolean isTeacher;
    if (data.has("schoolNumber")) {
        isTeacher = scheduleData.getType() == SubstitutionSchedule.Type.TEACHER;
        baseurl = "https://www.stundenplan24.de/" + data.getString("schoolNumber")
                + (isTeacher ? "/vplanle/" : "/vplan/");
        if (credential == null || !(credential instanceof UserPasswordCredential)) {
            throw new IOException("no login");
        }
        String login = ((UserPasswordCredential) credential).getUsername();
        String password = ((UserPasswordCredential) credential).getPassword();
        executor.auth(login, password);
    } else {
        baseurl = data.getString("baseurl") + "/";
        isTeacher = data.getString("baseurl").endsWith("vplanle");
        new LoginHandler(scheduleData, credential, cookieProvider).handleLogin(executor, cookieStore);
    }

    List<Document> docs = new ArrayList<>();

    for (int i = 0; i < MAX_DAYS; i++) {
        LocalDate date = LocalDate.now().plusDays(i);
        String dateStr = DateTimeFormat.forPattern("yyyyMMdd").print(date);
        String suffix = isTeacher ? "Le" : "Kl";
        String url = baseurl + "vdaten/Vplan" + suffix + dateStr + ".xml?_=" + System.currentTimeMillis();
        try {
            String xml = httpGet(url, ENCODING);
            Document doc = Jsoup.parse(xml, url, Parser.xmlParser());
            if (doc.select("kopf datei").text().equals("Vplan" + suffix + dateStr + ".xml")) {
                docs.add(doc);
            }
        } catch (HttpResponseException e) {
            if (e.getStatusCode() != 404 && e.getStatusCode() != 300)
                throw e;
        }
    }

    SubstitutionSchedule v = SubstitutionSchedule.fromData(scheduleData);

    for (Document doc : docs) {
        v.addDay(parseIndiwareDay(doc, false));
    }

    v.setWebsite(baseurl);

    v.setClasses(getAllClasses());
    v.setTeachers(getAllTeachers());

    return v;
}

From source file:me.vertretungsplan.parser.IphisParser.java

License:Mozilla Public License

/**
 * Returns a JSONArray with all changes from now to in one week.
 *///  ww  w. j  ava  2 s.c  o  m
private JSONArray getChanges() throws IOException, CredentialInvalidException {
    // Date (or alias of date) when the changes start
    final String startBy = LocalDate.now().toString();
    // Date (or alias of date) when the changes end
    final String endBy = LocalDate.now().plusWeeks(1).toString();

    final String url = api + "/vertretung/von/" + startBy + "/bis/" + endBy;
    return getJSONArray(url);
}

From source file:me.vertretungsplan.parser.IphisParser.java

License:Mozilla Public License

void parseIphis(SubstitutionSchedule substitutionSchedule, JSONArray changes, JSONArray grades,
        JSONArray teachers, JSONArray messages) throws IOException, JSONException {
    if (changes == null) {
        return;/*from ww w  .  jav  a 2s .  c o m*/
    }
    // Link course IDs to their names
    HashMap<String, String> coursesHashMap = null;
    if (grades != null) {
        coursesHashMap = new HashMap<>();
        for (int i = 0; i < grades.length(); i++) {
            JSONObject grade = grades.getJSONObject(i);
            coursesHashMap.put(grade.getString("id"), grade.getString("name"));
        }
    }
    // Link teacher IDs to their names
    HashMap<String, String> teachersHashMap = null;
    if (teachers != null) {
        teachersHashMap = new HashMap<>();
        for (int i = 0; i < teachers.length(); i++) {
            JSONObject teacher = teachers.getJSONObject(i);
            teachersHashMap.put(teacher.getString("id"), teacher.getString("name"));
        }
    }

    // Add Messages
    List<AdditionalInfo> infos = new ArrayList<>(messages.length());

    for (int i = 0; i < messages.length(); i++) {
        JSONObject message = messages.getJSONObject(i);
        AdditionalInfo info = new AdditionalInfo();
        info.setHasInformation(message.getBoolean("notification"));
        info.setTitle(message.getString("titel").trim());
        info.setText(message.getString("nachricht").trim());
        info.setFromSchedule(true);
        infos.add(info);
    }

    substitutionSchedule.getAdditionalInfos().addAll(infos);
    substitutionSchedule.setLastChange(lastUpdate);

    // Add changes to SubstitutionSchedule
    LocalDate currentDate = LocalDate.now();
    SubstitutionScheduleDay substitutionScheduleDay = new SubstitutionScheduleDay();
    substitutionScheduleDay.setDate(currentDate);
    for (int i = 0; i < changes.length(); i++) {
        final JSONObject change = changes.getJSONObject(i);
        final LocalDate substitutionDate = new LocalDate(change.getString("datum"));

        // If starting date of change does not equal date of SubstitutionScheduleDay
        if (!substitutionDate.isEqual(currentDate)) {
            if (!substitutionScheduleDay.getSubstitutions().isEmpty()
                    || !substitutionScheduleDay.getMessages().isEmpty()) {
                substitutionSchedule.addDay(substitutionScheduleDay);
            }
            substitutionScheduleDay = new SubstitutionScheduleDay();
            substitutionScheduleDay.setDate(substitutionDate);
            currentDate = substitutionDate;
        }

        if (change.getInt("id") > 0) {
            final Substitution substitution = getSubstitution(change, coursesHashMap, teachersHashMap);

            substitutionScheduleDay.addSubstitution(substitution);
        } else if (!change.optString("nachricht").isEmpty()) {
            substitutionScheduleDay.addMessage(change.optString("nachricht"));
        }
    }
    substitutionSchedule.addDay(substitutionScheduleDay);
}

From source file:me.vertretungsplan.parser.LegionBoardParser.java

License:Mozilla Public License

void parseLegionBoard(SubstitutionSchedule substitutionSchedule, JSONArray changes, JSONArray courses,
        JSONArray teachers) throws IOException, JSONException {
    if (changes == null) {
        return;//from w  w  w.  j  a v a2 s  .c o m
    }
    // Link course IDs to their names
    HashMap<String, String> coursesHashMap = null;
    if (courses != null) {
        coursesHashMap = new HashMap<>();
        for (int i = 0; i < courses.length(); i++) {
            JSONObject course = courses.getJSONObject(i);
            coursesHashMap.put(course.getString("id"), course.getString("name"));
        }
    }
    // Link teacher IDs to their names
    HashMap<String, String> teachersHashMap = null;
    if (teachers != null) {
        teachersHashMap = new HashMap<>();
        for (int i = 0; i < teachers.length(); i++) {
            JSONObject teacher = teachers.getJSONObject(i);
            teachersHashMap.put(teacher.getString("id"), teacher.getString("name"));
        }
    }
    // Add changes to SubstitutionSchedule
    LocalDate currentDate = LocalDate.now();
    SubstitutionScheduleDay substitutionScheduleDay = new SubstitutionScheduleDay();
    substitutionScheduleDay.setDate(currentDate);
    for (int i = 0; i < changes.length(); i++) {
        final JSONObject change = changes.getJSONObject(i);
        final Substitution substitution = getSubstitution(change, coursesHashMap, teachersHashMap);
        final LocalDate startingDate = new LocalDate(change.getString("startingDate"));
        final LocalDate endingDate = new LocalDate(change.getString("endingDate"));
        // Handle multi-day changes
        if (!startingDate.isEqual(endingDate)) {
            if (!substitutionScheduleDay.getSubstitutions().isEmpty()) {
                substitutionSchedule.addDay(substitutionScheduleDay);
            }
            for (int k = 0; k < 8; k++) {
                final LocalDate date = LocalDate.now().plusDays(k);
                if ((date.isAfter(startingDate) || date.isEqual(startingDate))
                        && (date.isBefore(endingDate) || date.isEqual(endingDate))) {
                    substitutionScheduleDay = new SubstitutionScheduleDay();
                    substitutionScheduleDay.setDate(date);
                    substitutionScheduleDay.addSubstitution(substitution);
                    substitutionSchedule.addDay(substitutionScheduleDay);
                    currentDate = date;
                }
            }
            continue;
        }
        // If starting date of change does not equal date of SubstitutionScheduleDay
        if (!startingDate.isEqual(currentDate)) {
            if (!substitutionScheduleDay.getSubstitutions().isEmpty()) {
                substitutionSchedule.addDay(substitutionScheduleDay);
            }
            substitutionScheduleDay = new SubstitutionScheduleDay();
            substitutionScheduleDay.setDate(startingDate);
            currentDate = startingDate;
        }
        substitutionScheduleDay.addSubstitution(substitution);
    }
    substitutionSchedule.addDay(substitutionScheduleDay);
}

From source file:me.vertretungsplan.parser.ParserUtils.java

License:Mozilla Public License

static List<String> handleUrlWithDateFormat(String url) {
    List<String> urls = new ArrayList<>();
    Pattern dateFormatPattern = Pattern.compile("\\{date\\(([^)]+)\\)\\}");
    Matcher matcher = dateFormatPattern.matcher(url);
    if (matcher.find()) {
        String pattern = matcher.group(1);
        for (int j = 0; j < 7; j++) {
            LocalDate date = LocalDate.now().plusDays(j);
            String dateStr = DateTimeFormat.forPattern(pattern).print(date);
            String urlWithDate = matcher.replaceFirst(dateStr);
            urls.add(urlWithDate);//from   w  ww . jav a 2 s .  c  om
        }
    } else {
        urls.add(url);
    }
    return urls;
}

From source file:me.vertretungsplan.parser.WebUntisParser.java

License:Mozilla Public License

@Override
public SubstitutionSchedule getSubstitutionSchedule()
        throws IOException, JSONException, CredentialInvalidException {
    try {/*from  ww  w  . ja  va 2 s  .  c o m*/
        login();
        SubstitutionSchedule schedule = SubstitutionSchedule.fromData(scheduleData);
        schedule.setLastChange(getLastImport());

        TimeGrid timegrid = new TimeGrid(getTimeGrid());
        final LocalDate today = LocalDate.now();
        int daysToAdd = getDaysToAdd();

        final LocalDate endDate = today.plusDays(6 + daysToAdd);

        try {
            schedule = parseScheduleUsingSubstitutions(schedule, timegrid, today, endDate);
        } catch (UnauthorizedException e) {
            schedule = parseScheduleUsingTimetable(schedule, timegrid, today, endDate);
        }

        schedule.setClasses(toNamesList(getClasses()));
        final String protocol = data.optString(PARAM_PROTOCOL, "https") + "://";
        schedule.setWebsite(protocol + data.getString(PARAM_HOST) + "/WebUntis");

        try {
            addMessagesOfDay(schedule);
        } catch (UnauthorizedException ignored) {

        }

        logout();
        return schedule;
    } catch (UnauthorizedException e) {
        throw new IOException(e);
    }
}

From source file:me.vertretungsplan.parser.WebUntisParser.java

License:Mozilla Public License

private void addMessagesOfDay(SubstitutionSchedule schedule)
        throws JSONException, CredentialInvalidException, IOException, UnauthorizedException {
    for (int i = 0; i < 7; i++) {
        LocalDate date = LocalDate.now().plusDays(i);
        JSONArray messages = getMessagesOfDay(date).getJSONObject("messageOfDayCollection")
                .getJSONArray("messages");
        if (messages.length() > 0) {
            SubstitutionScheduleDay day = getDayForDate(schedule, date);
            for (int j = 0; j < messages.length(); j++) {
                day.addMessage(messages.getJSONObject(j).getString("text"));
            }/*from   w  w  w. j av a  2s  .  c  om*/
        }
    }
}

From source file:me.vertretungsplan.parser.WebUntisParser.java

License:Mozilla Public License

/**
 * find out if there's a holiday currently and if so, also display substitutions after it
 *
 * @return//from w  w  w.  j  a  va 2  s.  c  o  m
 * @throws JSONException
 * @throws CredentialInvalidException
 * @throws IOException
 */
private int getDaysToAdd() throws JSONException, CredentialInvalidException, IOException {
    final LocalDate today = LocalDate.now();
    int daysToAdd = 0;
    try {
        //
        JSONArray holidays = getHolidays();
        for (int i = 0; i < holidays.length(); i++) {
            LocalDate startDate = DATE_FORMAT
                    .parseLocalDate(String.valueOf(holidays.getJSONObject(i).getInt("startDate")));
            LocalDate endDate = DATE_FORMAT
                    .parseLocalDate(String.valueOf(holidays.getJSONObject(i).getInt("endDate")));
            if (!startDate.isAfter(today.plusDays(6)) && !endDate.isBefore(today)) {
                if (startDate.isBefore(today)) {
                    daysToAdd += Days.daysBetween(today, endDate).getDays() + 1;
                } else {
                    daysToAdd += Days.daysBetween(startDate, endDate).getDays() + 2;
                }
            }
        }
    } catch (UnauthorizedException ignored) {

    }
    return daysToAdd;
}