Android Date to String Convert formatDate(Date date, String format)

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

Description

Formats date to the string with specific time date format.

License

Open Source License

Parameter

Parameter Description
date Date to format.
format Format of result string.

Return

Formatted date string.

Declaration

public static String formatDate(Date date, String format) 

Method Source Code

//package com.java2s;
/**/*from  w ww .  java  2 s . c  o  m*/
 * Copyright ? Microsoft Open Technologies, Inc.
 *
 * All Rights Reserved
 *
 * Licensed 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
 *
 * THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
 * ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A
 * PARTICULAR PURPOSE, MERCHANTABILITY OR NON-INFRINGEMENT.
 *
 * See the Apache License, Version 2.0 for the specific language
 * governing permissions and limitations under the License.
 */

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    /**
     * Default time date format.
     */
    private static final String DEFAULT_TIME_DATE_FORMAT = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZ";

    /**
     * Formats date to the string with default time date format.
     * 
     * @param time Date to format.
     * 
     * @return Result string.
     */
    public static String formatDate(Date time) {
        SimpleDateFormat dateformat = new SimpleDateFormat(
                DEFAULT_TIME_DATE_FORMAT);
        return dateformat.format(time);
    }

    /**
     * Formats date to the string with specific time date format.
     * 
     * @param date Date to format.
     * @param format Format of result string.
     * 
     * @return Formatted date string.
     */
    public static String formatDate(Date date, String format) {
        return new SimpleDateFormat(format).format(date);
    }
}

Related

  1. convertToDateStamp(Date date)
  2. dateTimeToString(Date date)
  3. dateToIsoDateString(@NotNull Date date)
  4. dateToIsoDateString(@Nullable Date date, @NotNull DateFallback fallback)
  5. formatDate(String format, Date date)
  6. formatDate(final Date date)
  7. getCurrentDateStr()
  8. getCurrentDateStrs()