Java Calendar to String toString(Calendar calendar)

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

Description

Default calendar to string conversion.

License

Apache License

Parameter

Parameter Description
calendar calendar

Return

string representation

Declaration

public static String toString(Calendar calendar) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import java.util.TimeZone;

public class Main {
    public static final DateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("EEE, MM/dd/yyyy, hh:mm:ss a, z");

    /**//from  w  w w .  j  a v a2  s .  c om
     * Default calendar to string conversion.
     * 
     * @param calendar calendar
     * @return string representation
     */
    public static String toString(Calendar calendar) {
        return toString(calendar, DEFAULT_DATE_FORMAT);
    }

    /**
     * Convert date and time zone to a default string representation.
     * 
     * @param date date
     * @param zone time zone
     * @return string representation
     */
    public static String toString(Date date, TimeZone zone) {
        return toString(date, zone, DEFAULT_DATE_FORMAT);
    }

    /**
     * Convert date and time zone to a specified string representation.
     * 
     * @param date date
     * @param zone time zone
     * @param formatter specified format
     * @return string representation
     */
    public static String toString(Date date, TimeZone zone, DateFormat formatter) {
        if (date == null) {
            return null;
        }

        Calendar c = new GregorianCalendar();
        c.setTime(date);

        if (zone != null) {
            c.setTimeZone(zone);
        }

        return toString(c, formatter);
    }

    /**
     * Convert calendar to a string representation.
     * 
     * @param calendar calendar
     * @param formatter formatter
     * @return string representation
     */
    public static synchronized String toString(Calendar calendar, DateFormat formatter) {
        formatter.setCalendar(calendar);

        return formatter.format(calendar.getTime());
    }
}

Related

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