Java Date Format Change convertDate(String date, String sourceFormat, String destinationFormat)

Here you can find the source of convertDate(String date, String sourceFormat, String destinationFormat)

Description

convert the input date from source to destination format

License

Open Source License

Parameter

Parameter Description
date a parameter
sourceFormat a parameter
destinationFormat a parameter

Exception

Parameter Description

Return

date in the destination format

Declaration

public static String convertDate(String date, String sourceFormat, String destinationFormat)
        throws java.text.ParseException 

Method Source Code

//package com.java2s;
/**/*from   w  ww .  j  ava2  s. co m*/
 * Copyright (C) 2010-2014 Think Big Analytics, 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. See accompanying LICENSE file.
 */

import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    /**
    * convert the input date from source to destination format
    * @param date
    * @param sourceFormat
    * @param destinationFormat
    * @return date in the destination format
    * @throws java.text.ParseException
    */
    public static String convertDate(String date, String sourceFormat, String destinationFormat)
            throws java.text.ParseException {
        String returnDateString = "";

        SimpleDateFormat formatter = new SimpleDateFormat(sourceFormat);
        Date returnDate = formatter.parse(date);

        formatter = new SimpleDateFormat(destinationFormat);
        returnDateString = formatter.format(returnDate);

        return returnDateString;
    }
}

Related

  1. changeDateFormat(String dateString, String srcFormat, String destFormat)
  2. changeDateFormatPattern(final DateFormat dateFormat, String regex, String replacement)
  3. changeDateTimeZone(String date_GMT)
  4. changeDateValue(Date firstDate, Date secondDate)
  5. convertDate(String date, String[] formats)
  6. convertDate(String dateIn, String fromDateFormat, String toDateFormat)
  7. convertDate(String inPattern, String outPattern, String date)
  8. convertDate(String inPattern, String outPattern, String date)