Java Calendar to String toString(Calendar cal)

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

Description

Converts a Calendar to its CMISQL string representation.

License

Apache License

Parameter

Parameter Description
cal a Calendar

Return

the CMISQL string representation

Declaration

public static String toString(Calendar cal) 

Method Source Code


//package com.java2s;
/*/*from w  w  w . j  av a 2 s  . c om*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 * Authors:
 *     Florent Guillaume, Nuxeo
 */

import java.util.Calendar;

public class Main {
    /**
     * Converts a Calendar to its CMISQL string representation.
     *
     * @param cal
     *            a {@link Calendar}
     * @return the CMISQL string representation
     */
    public static String toString(Calendar cal) {
        StringBuilder buf = new StringBuilder(28);
        toString(cal, buf);
        return buf.toString();
    }

    /**
     * Converts a Calendar to its CMISQL string representation.
     *
     * @param cal
     *            a {@link Calendar}
     * @param buf
     *            a buffer in which to add the CMISQL string representation
     */
    public static void toString(Calendar cal, StringBuilder buf) {
        buf.append(cal.get(Calendar.YEAR));
        buf.append('-');
        int f = cal.get(Calendar.MONTH);
        if (f < 9) {
            buf.append('0');
        }
        buf.append(f + 1);
        buf.append('-');
        f = cal.get(Calendar.DATE);
        if (f < 10) {
            buf.append('0');
        }
        buf.append(f);
        buf.append('T');
        f = cal.get(Calendar.HOUR_OF_DAY);
        if (f < 10) {
            buf.append('0');
        }
        buf.append(f);
        buf.append(':');
        f = cal.get(Calendar.MINUTE);
        if (f < 10) {
            buf.append('0');
        }
        buf.append(f);
        buf.append(':');
        f = cal.get(Calendar.SECOND);
        if (f < 10) {
            buf.append('0');
        }
        buf.append(f);
        buf.append('.');
        f = cal.get(Calendar.MILLISECOND);
        if (f < 100) {
            buf.append('0');
        }
        if (f < 10) {
            buf.append('0');
        }
        buf.append(f);
        int offset = cal.getTimeZone().getOffset(cal.getTimeInMillis()) / 60000;
        if (offset == 0) {
            buf.append('Z');
        } else {
            char sign;
            if (offset < 0) {
                offset = -offset;
                sign = '-';
            } else {
                sign = '+';
            }
            buf.append(sign);
            f = offset / 60;
            if (f < 10) {
                buf.append('0');
            }
            buf.append(f);
            buf.append(':');
            f = offset % 60;
            if (f < 10) {
                buf.append('0');
            }
            buf.append(f);
        }
    }
}

Related

  1. getDateToString(Calendar argCal)
  2. getString(Calendar calendar)
  3. toString(Calendar c)
  4. toString(Calendar cal)
  5. toString(Calendar cal)
  6. toString(Calendar calendar)
  7. toString(final Calendar calendar, final String format)
  8. toString(GregorianCalendar g1)