Java Date to String dateTimeISO(final java.util.Date date)

Here you can find the source of dateTimeISO(final java.util.Date date)

Description

Returns the given date as an ISO date and time string ( "YYYY-MM-DD HH:MM:SS" )

License

LGPL

Parameter

Parameter Description
date a parameter

Return

the date and time in ISO string format ( or "" if null )

Declaration

public static String dateTimeISO(final java.util.Date date) 

Method Source Code

//package com.java2s;
/**/* ww  w . j  av  a2s.co  m*/
 *  Copyright (C) 2008-2014  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.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.gnu.org/licenses/lgpl.html
 *
 *  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.SimpleDateFormat;
import java.util.Calendar;

public class Main {
    private final static String DATE_ISO_FORMAT = "yyyy-MM-dd";
    private final static String DATE_TIME_ISO_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * Returns the given date as an ISO date and time string ( "YYYY-MM-DD HH:MM:SS" )
     * @param date
     * @return the date and time in ISO string format ( or "" if null )
     */
    public static String dateTimeISO(final java.util.Date date) {
        if (date != null) {
            // return DATE_TIME_ISO_FORMAT.format( date );
            SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_TIME_ISO_FORMAT);
            return dateFormat.format(date);

        } else {
            return "";
        }
    }

    /**
     * Format the given day, month and year 
     * @param iDay the day ( 1 to 31 )
     * @param iMonth the month  ( 1 to 12 )
     * @param iYear the year
     * @param sFormat the format string usable to build a SimpleDateFormat
     * @return the formated date ( using the given format )
     */
    public static String format(final int iDay, final int iMonth, final int iYear, final String sFormat) {
        java.util.Date date = getUtilDate(iDay, iMonth, iYear);
        SimpleDateFormat dateFormat = new SimpleDateFormat(sFormat);
        return dateFormat.format(date);
    }

    /**
     * Format the given day, month and year ( in ISO format ) 
     * @param iDay the day ( 1 to 31 )
     * @param iMonth ( 1 to 12 )
     * @param iYear the year
     * @return the formated date ( using the default ISO format )
     */
    public static String format(final int iDay, final int iMonth, final int iYear) {
        //return DATE_ISO_FORMAT.format( getUtilDate(iDay, iMonth, iYear) );
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_ISO_FORMAT);
        return dateFormat.format(getUtilDate(iDay, iMonth, iYear));
    }

    /**
     * Format the given date using the given format
     * @param date
     * @param sFormat
     * @return the formated date
     * @since 1.0.3
     */
    public static String format(final java.util.Date date, final String sFormat) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(sFormat);
        return dateFormat.format(date);
    }

    /**
     * Returns a standard java.util.Date instance for the given day, month and year
     * @param iDay the day ( 1 to 31 )
     * @param iMonth the month ( 1 to 12 )
     * @param iYear the year
     * @return the resulting date
     */
    public static java.util.Date getUtilDate(final int iDay, final int iMonth, final int iYear) {
        Calendar cal = Calendar.getInstance();
        cal.set(iYear, iMonth - 1, iDay);
        return new java.util.Date(cal.getTimeInMillis());
    }
}

Related

  1. date2StringMD(Date date)
  2. date2StringPattern(Date value, String pattern)
  3. Date2StringSec(Date date)
  4. datetime(Date date)
  5. dateTime2String(Date dateTime, DateFormat dateFormat)
  6. dateTimeToZero(Date date)
  7. dateTo14String(Date date)
  8. dateToBOMCStrDate(Date date)
  9. dateToCalendar(Date fecha, String formato)