Java Hour Format formatDate(Date dateString)

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

Description

This function converts the current date string to appropirate database string representation

License

Open Source License

Parameter

Parameter Description
dateString a parameter

Declaration

public static String formatDate(Date dateString) 

Method Source Code

//package com.java2s;
/**/*  www.j av a2  s . co  m*/
 * Created on Aug 9, 2004
 *
 * Copyright 2005 by Arysys Technologies (P) Ltd.,
 * #3,Shop line,
 * Sasmira Marg,
 * Worli,Mumbai 400 025
 * India
 *
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Arysys Technologies (P) Ltd. ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Arysys Technologies (P) Ltd.
 *
 */

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static String formatDate(String dateString, String FORMAT) {
        try {
            if (dateString.trim().length() < 12) {
                dateString += " 00:00:00";
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(dateString);
            sdf.applyPattern(FORMAT);
            return sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String formatDate(String dateString) {
        try {
            if (dateString.trim().length() < 12) {
                dateString += " 00:00:00";
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(dateString);
            sdf.applyPattern("dd-MMM-yyyy HH:mm");
            return sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * This function converts the current date string to appropirate database string representation 
     * @param dateString
     * @return
     */
    public static String formatDate(Date dateString) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat();
            sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
            return sdf.format(dateString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String formatDate(Date dateString, String FORMAT) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat();
            sdf.applyPattern(FORMAT);
            return sdf.format(dateString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}

Related

  1. formatDate(Date date, String pattern)
  2. formatDate(Date date, String pattern)
  3. formatDate(Date date, String timeFormat, String timeZone)
  4. formatDate(Date date, StringBuffer buffer)
  5. formatDate(Date date, TimeZone tz)
  6. formatDate(Date src, String formatPattern)
  7. formatDate(final Date date, final String pattern)
  8. formatDate(final Date date, String pattern)
  9. formatDate(final String date)