Java Calendar to String toString(Calendar cal)

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

Description

to String

License

Apache License

Declaration

public static String toString(Calendar cal) 

Method Source Code

//package com.java2s;
/*/*from  w  w  w .  j a va2 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.
 */

import java.text.DecimalFormat;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Main {
    private static SimpleDateFormat utcFormat = new SimpleDateFormat(
            "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    private static SimpleDateFormat taiFormat = new SimpleDateFormat(
            "yyyy-MM-dd'T'HH:mm:ss.SSS");
    private static SimpleDateFormat localFormat = new SimpleDateFormat(
            "yyyy-MM-dd'T'HH:mm:ss.SSSZ");

    public static String toString(Calendar cal) {
        String timeZoneId = cal.getTimeZone().getID();
        if (timeZoneId.equals("UTC")) {
            return utcFormat.format(cal.getTime());
        } else if (timeZoneId.equals("TAI")) {
            return taiFormat.format(cal.getTime()) + "-0000"
                    + (cal.getTimeZone().getRawOffset() / 1000);
        } else {
            return localFormat.format(cal.getTime());
        }
    }

    public static String toString(double seconds) {
        return new DecimalFormat("#.000").format(seconds);
    }
}

Related

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