Java Calendar Format formatDate(Calendar cal)

Here you can find the source of formatDate(Calendar cal)

Description

format Date

License

Open Source License

Declaration

public static String formatDate(Calendar cal) 

Method Source Code


//package com.java2s;
/*//from   ww w.  j  a v  a2s .com
Copyright 2005-2015, Olivier PETRUCCI.
    
This file is part of Areca.
    
Areca 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 2 of the License, or
(at your option) any later version.
    
Areca 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 Areca; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    
*/

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

public class Main {
    private static int ZERO_YEAR = 2000;
    private static GregorianCalendar ZERO = new GregorianCalendar(ZERO_YEAR, 0, 1);
    private static int[] CUMUL_DAYS_PER_YEAR = { 0, 366, 731, 1096, 1461, 1827, 2192, 2557, 2922, 3288, 3653, 4018,
            4383, 4749, 5114, 5479, 5844, 6210, 6575, 6940, 7305, 7671, 8036, 8401, 8766, 9132, 9497, 9862, 10227 };

    public static String formatDate(Calendar cal) {
        StringBuffer sb = new StringBuffer();
        sb.append(cal.get(cal.YEAR)).append("-").append(1 + cal.get(cal.MONTH)).append("-")
                .append(cal.get(cal.DAY_OF_MONTH)).append("|").append(translateToDayNumber(cal));
        return sb.toString();
    }

    public static String formatDate(int date) {
        return formatDate(translateToCalendar(date));
    }

    /**
     * 
     * @param y
     * @param m Month number, 1=Jan, 2=Feb, ..., 12=Dec
     * @param d
     * @return
     */
    public static int translateToDayNumber(int y, int m, int d) {
        Calendar cal = new GregorianCalendar(y, m - 1, d);
        return translateToDayNumber(cal);
    }

    /**
     * Translate the calendar to a number of days since 01/01/2000
     */
    public static int translateToDayNumber(Calendar cal) {
        if (cal == null) {
            return 0;
        } else {
            int y = cal.get(cal.YEAR) - ZERO_YEAR;

            int nbD = CUMUL_DAYS_PER_YEAR[y];
            nbD += cal.get(Calendar.DAY_OF_YEAR) - 1;

            return nbD;
        }
    }

    public static Calendar translateToCalendar(int day) {
        if (day == 0) {
            return null;
        } else {
            Calendar ret = (Calendar) ZERO.clone();
            ret.add(ZERO.DAY_OF_MONTH, day);
            return ret;
        }
    }

    public static Calendar translateToCalendar(long millisec) {
        if (millisec == 0) {
            return null;
        } else {
            long nbDays = (long) (millisec / 86400000.0);

            Calendar ret = translateToCalendar((int) nbDays);

            long dayMillisec = millisec - nbDays * 86400000;

            ret.add(ZERO.MILLISECOND, (int) dayMillisec);
            return ret;
        }
    }
}

Related

  1. formatCalendarXsdZulu(Calendar c, int millisDigits)
  2. formatDate(Calendar c)
  3. formatDate(Calendar c)
  4. formatDate(Calendar c)
  5. formatDate(Calendar cal)
  6. formatDate(Calendar cal)
  7. formatDate(Calendar calendar)
  8. formatDate(Calendar date)
  9. formatDate(Calendar date)