Java Date Format Change convertDate(String inPattern, String outPattern, String date)

Here you can find the source of convertDate(String inPattern, String outPattern, String date)

Description

Parses a date string according to a specified input pattern, and formats the date back to a string according to a specified output pattern.

License

Open Source License

Parameter

Parameter Description
inPattern The SimpleDateFormat pattern to use for parsing the inbound date string (e.g. "yyyyMMddHHmmss").
outPattern The SimpleDateFormat pattern to use for formatting the outbound date string (e.g. "yyyyMMddHHmmss").
date The date string to convert.

Exception

Parameter Description
Exception an exception

Return

The converted date string.

Declaration

public static String convertDate(String inPattern, String outPattern,
        String date) throws Exception 

Method Source Code

//package com.java2s;
/*//from www  .j a  va 2  s.c  o  m
 * Copyright (c) Mirth Corporation. All rights reserved.
 * 
 * http://www.mirthcorp.com
 * 
 * The software in this package is published under the terms of the MPL license a copy of which has
 * been included with this distribution in the LICENSE.txt file.
 */

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

public class Main {
    /**
     * Parses a date string according to a specified input pattern, and formats the date back to a
     * string according to a specified output pattern.
     * 
     * @param inPattern
     *            The SimpleDateFormat pattern to use for parsing the inbound date string (e.g.
     *            "yyyyMMddHHmmss").
     * @param outPattern
     *            The SimpleDateFormat pattern to use for formatting the outbound date string (e.g.
     *            "yyyyMMddHHmmss").
     * @param date
     *            The date string to convert.
     * @return The converted date string.
     * @throws Exception
     */
    public static String convertDate(String inPattern, String outPattern,
            String date) throws Exception {
        Date newDate = getDate(inPattern, date);
        return formatDate(outPattern, newDate);
    }

    /**
     * Parses a date string according to the specified pattern and returns a java.util.Date object.
     * 
     * @param pattern
     *            The SimpleDateFormat pattern to use (e.g. "yyyyMMddHHmmss").
     * @param date
     *            The date string to parse.
     * @return A java.util.Date object representing the parsed date.
     * @throws Exception
     */
    public static Date getDate(String pattern, String date)
            throws Exception {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        return formatter.parse(date);
    }

    /**
     * Formats a java.util.Date object into a string according to a specified pattern.
     * 
     * @param pattern
     *            The SimpleDateFormat pattern to use (e.g. "yyyyMMddHHmmss").
     * @param date
     *            The java.util.Date object to format.
     * @return The formatted date string.
     */
    public static String formatDate(String pattern, Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        return formatter.format(date);
    }
}

Related

  1. changeDateValue(Date firstDate, Date secondDate)
  2. convertDate(String date, String sourceFormat, String destinationFormat)
  3. convertDate(String date, String[] formats)
  4. convertDate(String dateIn, String fromDateFormat, String toDateFormat)
  5. convertDate(String inPattern, String outPattern, String date)
  6. convertDate(String str)
  7. convertDate(String value)
  8. convertDateFormat(String date, SimpleDateFormat formatBefore, SimpleDateFormat formatAfter)
  9. convertDateFormat(String sourceDate, String sourceDateFormat, String destDateFormat)