Java Date String Format formatDate(String date, int format)

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

Description

Reads a particular date format then translates it to another format.

License

Open Source License

Parameter

Parameter Description
date the date string
format the date format

Return

the translated string

Declaration

public static String formatDate(String date, int format) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005-2012 Synopsys, Incorporated
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  w w w .  ja va  2 s. c  o  m*/
 * Synopsys, Inc - Initial implementation 
 *******************************************************************************/

public class Main {
    private static String[] months = new String[] { "January", "February", "March", "April", "May", "June", "July",
            "August", "September", "October", "November", "December" };

    /**
     * Reads a particular date format then translates it to another format. The only format implemented so far is
     * "MM-DD-YYYY" which can have anything as a delimiter. It also parses based on the delimiter, so it can really be
     * M-D-YY even.
     * @param date the date string
     * @param format the date format
     * @return the translated string
     */
    public static String formatDate(String date, int format) {
        String retVal = null;

        if (date == null || date.length() == 0)
            return "unknown";

        String[] strs = date.split("-");
        if (strs.length != 3)
            return "unknown";
        int month = Integer.parseInt(strs[0]);
        int day = Integer.parseInt(strs[1]);
        int year = Integer.parseInt(strs[2]);
        retVal = months[month - 1] + " " + day + ", " + year;
        return retVal;
    }
}

Related

  1. FormatDate(String aDate)
  2. formatDate(String date)
  3. formatDate(String date)
  4. formatDate(String date)
  5. formatDate(String date)
  6. formatDate(String dateString, String delimiter)
  7. formatDate(String fieldName)
  8. FormatDate(String strDate, char DateSeparator)
  9. formatDate(String unformattedDate, String format)