Android Date Format getDefaultFormatter(Date date)

Here you can find the source of getDefaultFormatter(Date date)

Description

Returns SimpleDateFormat instance that formats date in default application format (time only for recent dates, day and month for messages earlier than year and month/day/year for older)

License

Open Source License

Parameter

Parameter Description
date Date will be formatted

Return

Formatter which will be format dates in default application format or null if something went wrong

Declaration

public static SimpleDateFormat getDefaultFormatter(Date date) 

Method Source Code

/**//from www.j  a v a 2 s  .  c o m
 * Copyright ? Microsoft Open Technologies, Inc.
 *
 * All Rights Reserved
 *
 * 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
 *
 * THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
 * ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A
 * PARTICULAR PURPOSE, MERCHANTABILITY OR NON-INFRINGEMENT.
 *
 * See the Apache License, Version 2.0 for the specific language
 * governing permissions and limitations under the License.
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import com.example.office.logger.Logger;

public class Main{
    /**
     * Returns {@link SimpleDateFormat} instance that formats date in default application format (time only for recent dates, day and month
     * for messages earlier than year and month/day/year for older)
     * 
     * @param date Date will be formatted
     * @return Formatter which will be format dates in default application format or null if something went wrong
     */
    public static SimpleDateFormat getDefaultFormatter(Date date) {
        try {
            Date now = new Date();
            if (now.before(date)) {
                // Message from future!
                // Specify locale explicitly, otherwise formatter will use system locale.
                // Can format month as "nov." instead of "Nov"
                return new SimpleDateFormat("MM/dd/yyyy h:mm:ss a",
                        Locale.US);
            }

            // ignore leap years and leap seconds
            long ms = now.getTime() - date.getTime();
            long seconds = ms / 1000;
            long minutes = seconds / 60;
            long hours = minutes / 60;
            long days = hours / 24;
            long years = days / 365;
            days %= 365;
            hours %= 24;
            minutes %= 60;
            seconds %= 60;
            ms %= 1000;

            SimpleDateFormat formatter;
            if (years >= 1) {
                formatter = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
            } else if (days >= 1) {
                formatter = new SimpleDateFormat("MMM, d", Locale.US);
            } else {
                // TODO should we use 24 hour format?
                formatter = new SimpleDateFormat("h:mm:ss a", Locale.US);
            }

            formatter.setTimeZone(TimeZone.getDefault());
            return formatter;
        } catch (Exception e) {
            Logger.logApplicationException(e,
                    DateTimeUtils.class.getSimpleName()
                            + ".getFormatter(): Error.");
        }
        return null;
    }
}

Related

  1. getCurrentFormatedDate()
  2. getCurrentMonth()
  3. getCurrentTime()
  4. getCurrentYear()
  5. getDateEN()
  6. getFormatCurrentAdd(int amount, String format)
  7. getFormatCurrentTime(String format)
  8. getFormatCurrentTime(String format)
  9. getFormatDate(Date date)