Java Date Parse convertTimeStampToDate(String dateString, String srcFormat, String destFormat)

Here you can find the source of convertTimeStampToDate(String dateString, String srcFormat, String destFormat)

Description

This method converts the string timestamp to a date format by trimming of the time entry.

License

Open Source License

Parameter

Parameter Description
dateString The String to operate on.
srcFormat The date Format contained in the String.
destFormat The format to be converted to.

Return

Formatted Date String if successful, null otherwise.

Declaration

public static String convertTimeStampToDate(String dateString, String srcFormat, String destFormat) 

Method Source Code

//package com.java2s;
/**/*from  w  w w. j av  a  2s.  c  om*/
 * (C) 2010-2011 Alibaba Group Holding Limited.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License 
 * version 2 as published by the Free Software Foundation. 
 * 
 */

import java.text.ParsePosition;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    /**
     * This method converts the string timestamp to a date format by trimming of
     * the time entry. And also returns back with requried format.
     * 
     * @param dateString
     *            The String to operate on.
     *            
     * @param srcFormat
     *            The date Format contained in the String.
     *            
     * @param destFormat
     *            The format to be converted to.
     *            
     * @return 
     *          Formatted Date String if successful, null otherwise.
     * 
     */
    public static String convertTimeStampToDate(String dateString, String srcFormat, String destFormat) {
        if (srcFormat == null || destFormat == null || srcFormat.isEmpty() || destFormat.isEmpty()) {
            return "";
        }

        if (dateString == null || dateString.isEmpty()) {
            return "";
        }
        String[] tmpDate = dateString.split(" ");
        return changeDateFormat(tmpDate[0], srcFormat, destFormat);
    }

    /**
     * Convert the string date to a particular format.
     * 
     * @param   dateString
     *          The String to operate on.
     *            
     * @param   srcFormat
     *          The date Format contained in the String.
     *            
     * @param   destFormat
     *          The format to be converted to.
     *            
     * @return 
     *          Formatted Date String if successful, null otherwise.
     * 
     */
    public static String changeDateFormat(String dateString, String srcFormat, String destFormat) {
        if (srcFormat == null || destFormat == null || srcFormat.isEmpty() || destFormat.isEmpty()) {
            return "";
        }

        // Set the Date Format for the vaules in the column specified
        SimpleDateFormat dateFormat = new SimpleDateFormat(srcFormat);

        if (dateString == null || dateString.isEmpty()) {
            return "";
        }

        // Get date as per the format
        java.util.Date dateVal = dateFormat.parse(dateString, new ParsePosition(0));

        dateFormat = new SimpleDateFormat(destFormat);

        return dateFormat.format(dateVal);
    }

    public static String changeDateFormat(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");

        return dateFormat.format(date);
    }
}

Related

  1. convertStrToDate(String day)
  2. convertStrToDate(String s, String format)
  3. convertStrToDate(String source)
  4. convertTFormatToCDATime(String tDate)
  5. convertTimeInMillisecondsToDate(long timeInMilliseconds)
  6. convertTimestampToDateTime(Long timestamp, Boolean withSeconds)
  7. convertTimeZonesToDate(String fromTimeZone, String toTimeZone, DateTime fromDateTime)
  8. convertUTCDateToLocal(String sDate, TimeZone timezone)
  9. convertXMLDate(String date)