Java Calendar to String calendarToString(Calendar calendar)

Here you can find the source of calendarToString(Calendar calendar)

Description

Converts the calender to a string.

License

Open Source License

Parameter

Parameter Description
calendar input date information

Return

formatted calendar string

Declaration

public static String calendarToString(Calendar calendar) 

Method Source Code

//package com.java2s;
/*//  w ww.j av a 2 s.  co m
 *   
 *
 * Copyright  1990-2007 Sun Microsystems, Inc. All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 only, as published by the Free Software Foundation.
 * 
 * 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 version 2 for more details (a copy is
 * included at /legal/license.txt).
 * 
 * You should have received a copy of the GNU General Public License
 * version 2 along with this work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 * 
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
 * Clara, CA 95054 or visit www.sun.com if you need additional
 * information or have any questions.
 */

import java.util.Calendar;

public class Main {
    /**
     * Converts the calender to a string.
     * @param calendar input date information
     * @return formatted calendar string
     */
    public static String calendarToString(Calendar calendar) {
        String[] months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

        String[] days = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

        if (calendar == null) {
            return "Thu Jan 01 00:00:00 UTC 1970";
        }

        int dow = calendar.get(Calendar.DAY_OF_WEEK);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour_of_day = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int seconds = calendar.get(Calendar.SECOND);
        int year = calendar.get(Calendar.YEAR);

        String yr = Integer.toString(year);

        //        TimeZone zone = calendar.getTimeZone();
        //        String zoneID = zone.getID();
        //        if (zoneID == null) zoneID = "";
        String zoneID = "GMT";

        // The total size of the string buffer
        // 3+1+3+1+2+1+2+1+2+1+2+1+zoneID.length+1+yr.length
        //  = 21 + zoneID.length + yr.length
        StringBuffer sb = new StringBuffer(25 + zoneID.length() + yr.length());

        sb.append(days[dow - 1]).append(' ');
        sb.append(months[month]).append(' ');
        appendTwoDigits(sb, day).append(' ');
        appendTwoDigits(sb, hour_of_day).append(':');
        appendTwoDigits(sb, minute).append(':');
        appendTwoDigits(sb, seconds).append(' ');
        if (zoneID.length() > 0)
            sb.append(zoneID).append(' ');
        appendFourDigits(sb, year);

        return sb.toString();
    }

    /**
     * Appends zero filled numeric string for two digit numbers.
     * @param sb current formatted buffer
     * @param number the digit to format
     * @return updated formatted string buffer
     */
    private static final StringBuffer appendTwoDigits(StringBuffer sb, int number) {
        if (number < 10) {
            sb.append('0');
        }
        return sb.append(number);
    }

    /**
     * Appends zero filled numeric string for four digit numbers.
     * @param sb current formatted buffer
     * @param number the digit to format
     * @return updated formatted string buffer
     */
    private static final StringBuffer appendFourDigits(StringBuffer sb, int number) {
        if (number >= 0 && number < 1000) {
            sb.append('0');
            if (number < 100) {
                sb.append('0');
            }
            if (number < 10) {
                sb.append('0');
            }
        }
        return sb.append(number);
    }
}

Related

  1. calendarToString(Calendar c, String format)
  2. calendarToString(Calendar cal)
  3. calendarToString(Calendar cal)
  4. calendarToString(Calendar cal, String format)
  5. calendarToString(Calendar calendar)
  6. calendarToString(Calendar calendar)
  7. calendarToString(Calendar calendar, String template)
  8. calendarToString(Calendar date, boolean transformToLocalTime)
  9. CalendarToString(Calendar fecha, String formato)