Java Date to String dateToString(Date date, String format)

Here you can find the source of dateToString(Date date, String format)

Description

date To String

License

Open Source License

Declaration

private static String dateToString(Date date, String format) 

Method Source Code

//package com.java2s;
/**//from w w  w  .  ja v  a  2s  .  c o  m
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
 *
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the License). You may not use this file except in
 * compliance with the License.
 *
 * You can obtain a copy of the License at
 * https://opensso.dev.java.net/public/CDDLv1.0.html or
 * opensso/legal/CDDLv1.0.txt
 * See the License for the specific language governing
 * permission and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL
 * Header Notice in each file and include the License file
 * at opensso/legal/CDDLv1.0.txt.
 * If applicable, add the following below the CDDL Header,
 * with the fields enclosed by brackets [] replaced by
 * your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * $Id: DateUtils.java,v 1.2 2008-06-25 05:53:00 qcheng Exp $
 *
 */

import java.text.MessageFormat;

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

public class Main {
    private static final String UTC_DATE_FORMAT = "{0}-{1}-{2}T{3}:{4}:{5}";
    private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone("UTC");

    /**
     * Returns <code>yyyy-MM-dd HH:mm:ss</code> String representation of a
     * date.
     * 
     * @param date Date object.
     */
    public static String dateToString(Date date) {
        return dateToString(date, UTC_DATE_FORMAT);
    }

    private static String dateToString(Date date, String format) {
        GregorianCalendar cal = new GregorianCalendar(UTC_TIME_ZONE);
        cal.setTime(date);
        String[] params = new String[6];

        params[0] = formatInteger(cal.get(Calendar.YEAR), 4);
        params[1] = formatInteger(cal.get(Calendar.MONTH) + 1, 2);
        params[2] = formatInteger(cal.get(Calendar.DAY_OF_MONTH), 2);
        params[3] = formatInteger(cal.get(Calendar.HOUR_OF_DAY), 2);
        params[4] = formatInteger(cal.get(Calendar.MINUTE), 2);
        params[5] = formatInteger(cal.get(Calendar.SECOND), 2);

        return MessageFormat.format(format, (Object[]) params);
    }

    private static String formatInteger(int value, int length) {
        String val = Integer.toString(value);
        int diff = length - val.length();

        for (int i = 0; i < diff; i++) {
            val = "0" + val;
        }

        return val;
    }
}

Related

  1. dateToString(Date date, String dateFormat)
  2. dateToString(Date date, String formartStr)
  3. dateToString(Date date, String format)
  4. dateToString(Date date, String format)
  5. dateToString(Date date, String format)
  6. dateToString(Date date, String format)
  7. dateToString(Date date, String format)
  8. dateToString(Date date, String formatStr)
  9. DateToString(Date date, String parttern)