Java Date Format formatDate(final Date date)

Here you can find the source of formatDate(final Date date)

Description

format Date

License

Open Source License

Declaration

public static String formatDate(final Date date) 

Method Source Code


//package com.java2s;
/*/*from w  w  w  .  ja va2  s. c  om*/
Copyright 2015 Google 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
    
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.DateFormat;

import java.util.Date;

import java.util.Locale;
import com.google.common.base.Preconditions;

public class Main {
    public static String formatDate(final Date date) {
        Preconditions.checkNotNull(date, "Error: Date argument can't be null.");

        return DateFormat.getDateInstance().format(date);
    }

    /**
     * Formats a date string regarding the given locale.
     * 
     * @param date The date to format.
     * @param type Defines the display type of month
     * @param locale The given locale.
     * 
     * @return The data string.
     */
    public static String formatDate(final Date date, final int type, final Locale locale) {
        Preconditions.checkNotNull(date, "Error: Date argument can't be null.");
        Preconditions.checkNotNull(locale, "Error: Locale argument can't be null.");

        final String s = String.format("%s %s", DateFormat.getDateInstance(type, locale).format(date),
                DateFormat.getTimeInstance(type, locale).format(date));

        return s;
    }
}

Related

  1. formatDate(final Date date)
  2. formatDate(final Date date)
  3. formatDate(final Date date)
  4. formatDate(final Date date)
  5. formatDate(final Date date)
  6. formatDate(final Date date)
  7. formatDate(final java.util.Date date)
  8. formatDate(final java.util.Date date, final Supplier formatSupplier)
  9. formatDate(int day, int month, int year)