Java Calendar Format formatDateTimeUTC(Calendar datetime)

Here you can find the source of formatDateTimeUTC(Calendar datetime)

Description

Format a date-time into UTC format.

License

Open Source License

Parameter

Parameter Description
datetime is the date-time to be formatted

Return

the UTC representation of the give date-time

Declaration

public static String formatDateTimeUTC(Calendar datetime) 

Method Source Code


//package com.java2s;
/*//from   w  ww .j  a v  a  2s  .c  o  m
 * Funambol is a mobile platform developed by Funambol, Inc. 
 * Copyright (C) 2003 - 2007 Funambol, Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation with the addition of the following permission 
 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 
 * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
 * 
 * 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 Affero General Public License 
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 * 
 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 
 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Powered by Funambol" logo. If the display of the logo is not reasonably 
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Powered by Funambol".
 */

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    private Calendar cal;

    /**
     * Format a date-time into UTC format. For example the following string
     * 19980119T070000Z represents 19th Jan 1998 at 07:00:00 in the GMT timezone
     *
     * @param datetime is the date-time to be formatted
     *
     * @return the UTC representation of the give date-time 
     */
    public static String formatDateTimeUTC(Calendar datetime) {
        String ret = "";
        int tt = 0;
        ret += datetime.get(Calendar.YEAR);

        tt = datetime.get(Calendar.MONTH) + 1;
        ret += tt < 10 ? "0" + tt : "" + tt;

        tt = datetime.get(Calendar.DAY_OF_MONTH);
        ret += (tt < 10 ? "0" + tt : "" + tt) + "T";

        tt = datetime.get(Calendar.HOUR_OF_DAY);
        ret += tt < 10 ? "0" + tt : "" + tt;

        tt = datetime.get(Calendar.MINUTE);
        ret += tt < 10 ? "0" + tt : "" + tt;

        tt = datetime.get(Calendar.SECOND);
        ret += (tt < 10 ? "0" + tt : "" + tt) + "Z";
        return ret;
    }

    /**
     * Format a date-time into UTC format. For example the following string
     * 19980119T070000Z represents 19th Jan 1998 at 07:00:00 in the GMT timezone
     *
     * @param date is the date to be formatted. This value will be interpreted
     * as relative to the default timezone 
     *
     * @return the UTC representation of the give date-time 
     */
    public static String formatDateTimeUTC(Date date) {
        Calendar datetime = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        datetime.setTime(date);
        return formatDateTimeUTC(datetime);
    }

    /**
     * Format a date-time into UTC format. For example the following string
     * 19980119T070000Z represents 19th Jan 1998 at 07:00:00 in the GMT timezone
     *
     * @param date is the date to be formatted. This value will be interpreted
     * as relative to the default timezone 
     *
     * @return the UTC representation of the give date-time 
     */
    public static String formatDateTimeUTC(long date) {
        Date d = new Date(date);
        return formatDateTimeUTC(d);
    }

    /** Set a new time */
    public void setTime(Date d) {
        cal.setTime(d);
    }
}

Related

  1. formatDate(Calendar date)
  2. formatDate(Calendar date, int precision)
  3. FormatDate(Calendar p_date, String p_separator)
  4. formatDate(String formats, Calendar date)
  5. formatDateTime(Calendar cal)
  6. formatDay(java.util.Calendar d)
  7. formatDuration(Calendar t1, Calendar t2, boolean precise)
  8. formatRfc3339Calendar(Calendar cal)
  9. formatTime(Calendar cal, StringBuffer sb)