Java Date Format formatDate(Date date)

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

Description

format Date

License

Open Source License

Declaration

public static String formatDate(Date date) 

Method Source Code

//package com.java2s;
/*//  w ww .j a  v  a 2 s.co  m

 This file is part of OpenMyEWB.

 OpenMyEWB 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.

 OpenMyEWB 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 OpenMyEWB.  If not, see <http://www.gnu.org/licenses/>.

 OpenMyEWB is Copyright 2005-2009 Nicolas Kruchten (nicolas@kruchten.com), Francis Kung, Engineers Without Borders Canada, Michael Trauttmansdorff, Jon Fishbein, David Kadish

 */

import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    private static SimpleDateFormat dateFormatter = new SimpleDateFormat(
            "EEE, MMM d, yyyy");

    public static String formatDate(Date date) {
        if (date == null) {
            return "never";
        }

        GregorianCalendar today = new GregorianCalendar();
        today.setTime(new Date());
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 1);

        GregorianCalendar yesterday = (GregorianCalendar) today.clone();
        yesterday.add(Calendar.DAY_OF_YEAR, -1);
        yesterday.set(Calendar.HOUR_OF_DAY, 0);
        yesterday.set(Calendar.MINUTE, 0);
        yesterday.set(Calendar.SECOND, 1);

        GregorianCalendar tomorrow = (GregorianCalendar) today.clone();
        tomorrow.add(Calendar.DAY_OF_YEAR, 1);
        tomorrow.set(Calendar.HOUR_OF_DAY, 0);
        tomorrow.set(Calendar.MINUTE, 0);
        tomorrow.set(Calendar.SECOND, 1);

        GregorianCalendar dayAfterTomorrow = (GregorianCalendar) today
                .clone();
        dayAfterTomorrow.add(Calendar.DAY_OF_YEAR, 2);
        dayAfterTomorrow.set(Calendar.HOUR_OF_DAY, 0);
        dayAfterTomorrow.set(Calendar.MINUTE, 0);
        dayAfterTomorrow.set(Calendar.SECOND, 1);

        GregorianCalendar dayOfDate = new GregorianCalendar();
        dayOfDate.setTime(date);
        dayOfDate.set(Calendar.HOUR_OF_DAY, 0);
        dayOfDate.set(Calendar.MINUTE, 0);
        dayOfDate.set(Calendar.SECOND, 2);

        if (dayOfDate.before(yesterday)
                || dayOfDate.after(dayAfterTomorrow)) {
            return "on " + dateFormatter.format(date);
        } else if (dayOfDate.before(today)) {
            return "yesterday";
        } else if (dayOfDate.before(tomorrow)) {
            return "today";
        } else {
            return "tomorrow";
        }
    }
}

Related

  1. formatDate(Date date)
  2. formatDate(Date date)
  3. formatDate(Date date)
  4. formatDate(Date date)
  5. formatDate(Date date)
  6. formatDate(Date date)
  7. formatDate(Date date)
  8. formatDate(Date date)
  9. formatDate(Date date)