ch.eitchnet.android.util.JodaHelper.java Source code

Java tutorial

Introduction

Here is the source code for ch.eitchnet.android.util.JodaHelper.java

Source

/*
 * 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.util;

import org.joda.time.Days;
import org.joda.time.Duration;
import org.joda.time.Period;
import org.joda.time.ReadablePartial;
import org.joda.time.format.DateTimeFormatterBuilder;
import org.joda.time.format.PeriodFormatterBuilder;

/**
 * @author Robert von Burg <eitch@eitchnet.ch>
 * 
 */
public class JodaHelper {

    public static String toHourMinute(Duration duration) {
        PeriodFormatterBuilder builder = new PeriodFormatterBuilder();
        builder.printZeroAlways();
        if (duration.isShorterThan(Duration.ZERO))
            builder.appendLiteral("-");
        builder.minimumPrintedDigits(2).appendHours().appendLiteral(":").minimumPrintedDigits(2).appendMinutes();
        return builder.toFormatter().print(new Period(Math.abs(duration.getMillis())));
    }

    /**
     * @param timestamp
     * @return
     */
    public static String toHourMinute(ReadablePartial timestamp) {
        DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
        builder.appendHourOfDay(2).appendLiteral(":").appendMinuteOfHour(2);
        return builder.toFormatter().print(timestamp);
    }

    /**
     * @param timestamp
     * @return
     */
    public static String toDateHourMinute(ReadablePartial timestamp) {
        DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
        builder.appendYear(4, 4).appendLiteral("-").appendMonthOfYear(2).appendLiteral("-").appendDayOfMonth(2);
        builder.appendLiteral(" ").appendHourOfDay(2).appendLiteral(":").appendMinuteOfHour(2);
        return builder.toFormatter().print(timestamp);
    }

    public static String toDays(Duration duration) {
        Duration tmp = duration;
        double days = tmp.getStandardDays();
        if (days >= Integer.MAX_VALUE)
            throw new RuntimeException("Buffer overflow for number of days in duration: " + tmp);
        tmp = tmp.minus(Days.days((int) days).toStandardDuration());
        double hours = tmp.getStandardHours();
        if (hours >= Integer.MAX_VALUE)
            throw new RuntimeException("Buffer overflow for number of hours in duration: " + tmp);

        return Double.toString(days + (hours / 24));
    }

}