Java Hour Format formatWithAge(Date date, String dateFormat, String suffix)

Here you can find the source of formatWithAge(Date date, String dateFormat, String suffix)

Description

Provided a date you will get a timestamp and the age/old that you want.

License

Apache License

Parameter

Parameter Description
date the date to compare to now.
dateFormat the SimpleDateFormat format string to use for the date.
suffix the suffix in the age string. using "ago" here would result in "2006 Aug 23, 11:43 pm - 12 days ago"

Return

the formated string.

Declaration

public static String formatWithAge(Date date, String dateFormat, String suffix) 

Method Source Code


//package com.java2s;
/*/*w w w. ja v  a 2s  .co  m*/
 * Copyright 2005-2006 The Codehaus.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.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.
 */

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    /**
     * Provided a date you will get a timestamp and the age/old that you want
     *
     * @param date   the date to compare to now.
     * @param suffix the suffix in the age string.  using "ago" here would result in "2006 Aug 23, 11:43 pm - 12 days ago"
     * @return the formated string.
     */
    public static String formatWithAge(Date date, String suffix) {
        return formatWithAge(date, "EEE, d MMM yyyy HH:mm:ss Z", suffix);
    }

    /**
     * Provided a date you will get a timestamp and the age/old that you want.
     *
     * @param date       the date to compare to now.
     * @param dateFormat the {@link SimpleDateFormat} format string to use for the date.
     * @param suffix     the suffix in the age string.  using "ago" here would result in "2006 Aug 23, 11:43 pm - 12 days ago"
     * @return the formated string.
     */
    public static String formatWithAge(Date date, String dateFormat, String suffix) {
        if (date == null) {
            return null;
        }

        SimpleDateFormat format = new SimpleDateFormat(dateFormat);

        StringBuffer out = new StringBuffer();
        out.append(format.format(date));
        out.append(" - ");

        Calendar now = Calendar.getInstance();
        Calendar then = Calendar.getInstance();
        then.setTime(date);

        long diffMillis = now.getTimeInMillis() - then.getTimeInMillis();

        long days = diffMillis / (24 * 60 * 60 * 1000);
        long hours = diffMillis / (60 * 60 * 1000);
        long minutes = diffMillis / (60 * 1000);
        long seconds = diffMillis / (1000);

        if (days > 0) {
            out.append(String.valueOf(days)).append(" day");
            if (days > 1) {
                out.append('s');
            }
        } else if (hours > 0) {
            out.append(String.valueOf(hours)).append(" hour");
            if (hours > 1) {
                out.append('s');
            }
        } else if (minutes > 0) {
            out.append(String.valueOf(minutes)).append(" minute");
            if (minutes > 1) {
                out.append('s');
            }
        } else if (seconds > 0) {
            out.append(String.valueOf(seconds)).append(" second");
            if (seconds > 1) {
                out.append('s');
            }
        }

        out.append(' ').append(suffix);

        return out.toString();
    }
}

Related

  1. formatValue(Object value)
  2. formatVerbose(int level, String message, String source)
  3. formatW3CDateTime(Date date)
  4. formatW3CDateTime(Date date)
  5. formatWCPDate(String date, String sFormat)
  6. formatWithSql92Date(Date date)
  7. formatXEP0082Date(Date date)
  8. formatXsdDate (final Date date)
  9. formatXsdDateTime(Date d)