return date in specific format. For example old Format can be "yyyy-MM-dd hh:mm:ss.SSS" and new Format can be "MM/dd/yyyy" - Java java.util

Java examples for java.util:Date Format

Description

return date in specific format. For example old Format can be "yyyy-MM-dd hh:mm:ss.SSS" and new Format can be "MM/dd/yyyy"

Demo Code


//package com.java2s;

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Main {


    /**/*from ww w .j  a  v a 2 s .  com*/
     * This method will return date in specific format.For example oldFormat can
     * be "yyyy-MM-dd hh:mm:ss.SSS" and new Format can be "MM/dd/yyyy"
     * 
     * @param date
     *            - date in String
     * @param oldFormat
     *            - oldFormat in String
     * @param newFormat
     *            - oldFormat in String
     * @return - date String in new Format
     */

    public static String getDateInNewFormat(String date, String oldFormat,
            String newFormat) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(oldFormat);
        java.util.Date d1 = sdf.parse(date);
        sdf.applyPattern(newFormat);
        String newdate = sdf.format(d1);
        return newdate;
    }
}

Related Tutorials