Java Date Format ISO toIso8601(@Nullable Calendar calendar)

Here you can find the source of toIso8601(@Nullable Calendar calendar)

Description

Converts a Calendar -object to a ISO-8601 string.

License

Microsoft Public License

Parameter

Parameter Description
calendar the calendar

Return

the ISO-8601 string (for example: '2009-06-30T18:30:00+02:00')

Declaration

@Nullable
public static String toIso8601(@Nullable Calendar calendar) 

Method Source Code

//package com.java2s;
/**************************************************************************
 * <pre>/* w ww.  j  a  va2  s  .c om*/
 *
 * Copyright (c) Unterrainer Informatik OG.
 * This source is subject to the Microsoft Public License.
 *
 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
 * All other rights reserved.
 *
 * (In other words you may copy, use, change and redistribute it without
 * any restrictions except for not suing me because it broke something.)
 *
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
 * PURPOSE.
 *
 * </pre>
 ***************************************************************************/

import java.util.Calendar;
import java.util.Date;
import javax.annotation.Nullable;

public class Main {
    /**
     * Converts a {@link Calendar}-object to a ISO-8601 string.
     *
     * @param calendar the calendar
     * @return the ISO-8601 string (for example: '2009-06-30T18:30:00+02:00')
     */
    @Nullable
    public static String toIso8601(@Nullable Calendar calendar) {
        if (calendar == null) {
            return null;
        }
        return javax.xml.bind.DatatypeConverter.printDateTime(calendar);
    }

    /**
     * Converts a {@link Date}-object to a ISO-8601 string.
     *
     * @param date the date
     * @return the ISO-8601 string (for example: '2009-06-30T18:30:00+02:00')
     */
    @Nullable
    public static String toIso8601(@Nullable Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return toIso8601(c);
    }

    /**
     * Converts a (Unix)-time-stamp of type long to a ISO-8601 string.
     *
     * @param timeStamp the time stamp
     * @return the ISO-8601 string (for example: '2009-06-30T18:30:00+02:00')
     */
    @Nullable
    public static String toIso8601(@Nullable Long timeStamp) {
        if (timeStamp == null) {
            return null;
        }
        Date date = new Date(timeStamp);
        return toIso8601(date);
    }
}

Related

  1. parseIsoDateTime(final String isoDateString, final String dateFormat)
  2. timeToIso8601(long time, boolean includeTime)
  3. toBasicISO8601DateTime(Date value)
  4. toDateIso(Date date)
  5. toISO2616DateFormat(Date date)
  6. toISO8601(Date date)
  7. toISO8601(Date date)
  8. toIso8601(Date date)
  9. toISO8601(Date date)