Android Calendar Format formatDate(Calendar date, String format)

Here you can find the source of formatDate(Calendar date, String format)

Description

format Date

License

Open Source License

Declaration

public static CharSequence formatDate(Calendar date, String format) 

Method Source Code

/*******************************************************************************
 * Mirakel is an Android App for managing your ToDo-Lists
 * /* w ww.  j a va 2 s.com*/
 * Copyright (c) 2013-2014 Anatolij Zelenin, Georg Semmler.
 * 
 *     This program 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
 *     any later version.
 * 
 *     This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import android.content.Context;
import android.os.Build;
import android.text.format.DateUtils;
import android.text.format.Time;
import de.azapps.mirakelandroid.R;

public class Main{
    public static final SimpleDateFormat dateFormat = new SimpleDateFormat(
            "yyyy-MM-dd", Locale.getDefault());
    public static String formatDate(Calendar c) {
        return c == null ? null : dateFormat.format(c.getTime());
    }
    
    public static CharSequence formatDate(Calendar date, String format) {
        if (date == null)
            return "";
        return new SimpleDateFormat(format, Locale.getDefault())
                .format(date.getTime());
    }
    
    public static CharSequence formatDate(Context ctx, Calendar date) {
        if (date == null)
            return "";
        if (MirakelPreferences.isDateFormatRelative()) {
            GregorianCalendar now = new GregorianCalendar();
            now.setTime(new Date());
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1
                    || !(now.get(Calendar.YEAR) == date.get(Calendar.YEAR)
                            && now.get(Calendar.DAY_OF_MONTH) == date
                                    .get(Calendar.DAY_OF_MONTH) && now
                            .get(Calendar.MONTH) == date
                            .get(Calendar.MONTH)))
                return DateUtils.getRelativeTimeSpanString(
                        date.getTimeInMillis(), new Date().getTime(),
                        DateUtils.DAY_IN_MILLIS);
            return ctx.getString(R.string.today);
        }
        return new SimpleDateFormat(ctx.getString(R.string.dateFormat),
                Locale.getDefault()).format(date.getTime());
    }
}

Related

  1. formatDA(Calendar c, StringBuffer sb)
  2. formatDBDateTime(Calendar c)
  3. formatDate(Calendar c)
  4. formatDate(Context ctx, Calendar date)
  5. formatDateTime(Calendar c)
  6. formatReminder(Context ctx, Calendar date)
  7. formatTM(Calendar c, StringBuffer sb)