Convert String Date Format Java Example

Convert String Date Format Java Example

Sample example to convert String value to Date object java example:

package com.javahonk;

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

import org.apache.commons.lang3.StringUtils;

public class convertStrToDateFormatJava {

	public static void main(String[] args) {
		System.out.println(convertStrToDateFormat("03/23/2017", "MM/dd/yyyy"));

	}
	
	public static Date convertStrToDateFormat(String date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date returnDate = null;
		try {
			returnDate = StringUtils.isNotBlank(date) ? sdf.parse(date) : null;
		} catch (ParseException e) {			
			e.printStackTrace();
		}
		return returnDate;
	}

}

Reference: Oracle Date

Leave a Reply

Your email address will not be published. Required fields are marked *