Java Date Format formatDate(Date date)

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

Description

Returns a localized short/medium date time string.

License

Open Source License

Parameter

Parameter Description
date The date to convert into a string

Return

localized short/medium date time string

Declaration

public static String formatDate(Date date) 

Method Source Code


//package com.java2s;
/*// w  ww. j  a  va  2 s  .  c o  m
 * Copyright 2004 - 2013 Wayne Grant
 *           2013 - 2016 Kai Kramer
 *
 * This file is part of KeyStore Explorer.
 *
 * KeyStore Explorer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * KeyStore Explorer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with KeyStore Explorer.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    /**
     * Returns a localized short/medium date time string.
     *
     * @param date The date to convert into a string
     * @return localized short/medium date time string
     */
    public static String formatDate(Date date) {
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM);

        if (dateFormat instanceof SimpleDateFormat) {
            SimpleDateFormat sdf = (SimpleDateFormat) dateFormat;
            // we want short date format but with 4 digit year
            sdf.applyPattern(sdf.toPattern().replaceAll("y+", "yyyy").concat(" z"));
        }

        return dateFormat.format(date);
    }
}

Related

  1. formatDate(Date date)
  2. formatDate(Date date)
  3. formatDate(Date date)
  4. formatDate(Date date)
  5. formatDate(Date date)
  6. formatDate(Date date)
  7. formatDate(Date date)
  8. formatDate(Date date)
  9. formatDate(Date date)