Java tutorial
/* * Copyright (c) 2012, Robert von Burg * * All rights reserved. * * This file is part of the XXX. * * XXX is free software: you can redistribute * it and/or modify it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3 of the License, * or (at your option) any later version. * * XXX is distributed in the hope that it will * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with XXX. If not, see * <http://www.gnu.org/licenses/>. */ package ch.eitchnet.android.mabea; import org.joda.time.Days; import org.joda.time.Duration; import org.joda.time.Hours; import org.joda.time.LocalDate; import org.joda.time.LocalDateTime; import org.joda.time.LocalTime; import org.joda.time.Minutes; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.PeriodFormatterBuilder; import ch.eitchnet.android.mabea.model.MabeaState; import ch.eitchnet.android.mabea.model.MabeaState.State; /** * @author Robert von Burg <eitch@eitchnet.ch> * */ public class MabeaParsing { public static Duration parseBalance(String value) throws MabeaParsingException { try { int posColon = value.indexOf(':'); int posSpace = value.indexOf(' '); int hours = Integer.parseInt(value.substring(0, posColon)); int minutes = Integer.parseInt(value.substring(posColon + 1, posSpace)); if (hours < 0) minutes = -minutes; Duration duration = Hours.hours(hours).toStandardDuration(); duration = duration.plus(Minutes.minutes((int) minutes).toStandardDuration()); return duration; } catch (Exception e) { throw new MabeaParsingException("Balance can not be parsed from value " + value, e); } } public static Duration parseRemainingVacation(String value) throws MabeaParsingException { try { int posSpace = value.indexOf(' '); String daysS = value.substring(0, posSpace).replace(",", "."); double days = Double.parseDouble(daysS); Duration duration = Days.days((int) Math.floor(days)).toStandardDuration(); if ((Math.floor(days)) <= (int) days) { duration = duration.plus(Hours.hours(12).toStandardDuration()); } return duration; } catch (Exception e) { throw new MabeaParsingException("Remaining vacation can not be parsed from value " + value, e); } } public static MabeaState.State parseState(String value) throws MabeaParsingException { try { int posSpace = value.indexOf(' '); String stateS = value.substring(0, posSpace); if (stateS.equals("Abwesend")) return State.LOGGED_OUT; else if (stateS.equals("Anwesend")) return State.LOGGED_IN; else throw new MabeaParsingException("Unknown value " + value); } catch (Exception e) { throw new MabeaParsingException("State can not be parsed from value " + value, e); } } public static LocalDateTime parseStateTime(String value) throws MabeaParsingException { try { int posOpenBracket = value.indexOf('('); int posCloseBracket = value.indexOf(')'); String time = value.substring(posOpenBracket + 1, posCloseBracket); LocalTime localTime = DateTimeFormat.forPattern("HH:mm").parseLocalTime(time); return LocalDate.now().toLocalDateTime(localTime); } catch (Exception e) { throw new MabeaParsingException("State time can not be parsed from value " + value, e); } } public static String toHourMin(Duration duration) { return new PeriodFormatterBuilder().appendHours().appendSeparator(":").appendMinutes().toFormatter() .print(duration.toPeriod()); } }