Build up a String representing the difference in time between two Calendar objects. - Java java.util

Java examples for java.util:Time

Description

Build up a String representing the difference in time between two Calendar objects.

Demo Code

/*//  w ww  .  jav  a  2  s.  co  m
 * This document is a part of the source code and related artifacts for StilesLib, an open source library that
 * provides a set of commonly-used functions for Bukkit plugins.
 *
 * http://github.com/mstiles92/StilesLib
 *
 * Copyright (c) 2014 Matthew Stiles (mstiles92)
 *
 * Licensed under the Common Development and Distribution License Version 1.0
 * You may not use this file except in compliance with this License.
 *
 * You may obtain a copy of the CDDL-1.0 License at http://opensource.org/licenses/CDDL-1.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the license.
 */
//package com.java2s;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Main {
    public static void main(String[] argv) throws Exception {
        Calendar first = Calendar.getInstance();
        Calendar second = Calendar.getInstance();
        System.out.println(buildTimeDifference(first, second));
    }

    private static int[] calendarConstants = new int[] { Calendar.YEAR,
            Calendar.MONTH, Calendar.WEEK_OF_YEAR, Calendar.DAY_OF_YEAR,
            Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND };
    private static String[] calendarConstantNames = new String[] { "year",
            "month", "week", "day", "hour", "minute", "second" };

    /**
     * Build up a String representing the difference in time between two Calendar objects.
     *
     * @param first the first Calendar object
     * @param second the second Calendar object
     * @return the time difference between the two Calendar objects
     */
    public static String buildTimeDifference(Calendar first, Calendar second) {
        Calendar firstCopy = new GregorianCalendar();
        firstCopy.setTimeInMillis(first.getTimeInMillis());

        if (firstCopy.equals(second)) {
            return "now";
        }

        StringBuilder s = new StringBuilder();

        for (int i = 0; i < calendarConstants.length; i++) {
            int difference = getDifference(calendarConstants[i], firstCopy,
                    second);
            if (difference > 0) {
                s.append(difference).append(" ")
                        .append(calendarConstantNames[i])
                        .append((difference > 1) ? "s " : " ");
                firstCopy.add(calendarConstants[i], difference);
            }
        }

        if (s.length() == 0) {
            return "now";
        }

        return s.toString().trim();
    }

    /**
     * Get the difference in time between two calendars for an individual time unit.
     *
     * @param constant the constant representing the time unit to compare
     * @param first the first Calendar object
     * @param second the second Calendar object
     * @return the number of the specified time units between the first and second Calendar objects
     */
    private static int getDifference(int constant, Calendar first,
            Calendar second) {
        int difference = 0;
        Calendar temp = new GregorianCalendar();
        temp.setTimeInMillis(first.getTimeInMillis());

        while (!temp.after(second)) {
            temp.add(constant, 1);
            difference += 1;
        }

        return difference - 1;
    }
}

Related Tutorials