Java Date To String Example

This example will show you how to use SimpleDateFormat to convert a Java.util.Date object to formated String and how to convert a Java date format String back to Java.util.Date object.

Java Date To String

  1. Initiate a SimpleDateFormat object with the required format.
  2. Use SimpleDateFormat.format() method to convert the java.util.Date object to the formated String.
    	private static String javaDateToString(Date dateObj, String dateFormatString)	{		String dateStr = "";		try		{			if(dateObj!=null && dateFormatString!=null)			{				// Create a DateFormat object.				DateFormat df = new SimpleDateFormat(dateFormatString);				// Use DateFormat.format() method to get the result.				dateStr = df.format(dateObj);			}		}catch(Exception ex)		{			ex.printStackTrace();		}finally		{			System.out.println("Formated Date String is " + dateStr);			return dateStr;		}	}

Java String To Date

  1. Use provided date format string to create a SimpleDateFormat object.
  2. Use SimpleDateFormat.parse() method to get the java.util.Date object.
    	private static Date javaStringToDate(String dateStr, String dateFormatString)	{		Date retDate = null;		try		{			if(dateStr!=null && dateFormatString!=null)			{				// Create a DateFormat object.				DateFormat df = new SimpleDateFormat(dateFormatString);				// Use DateFormat.parse() method to get the date object.				retDate = df.parse(dateStr);			}		}catch(Exception ex)		{			ex.printStackTrace();		}finally		{			System.out.println("The Date value for " + dateStr + " parsed out by format " + dateFormatString + " is " + retDate);			return retDate;		}	}
  3. DateStringFormatMapDTO.java
    /* This is a DTO class which is used to link a string to it's date format. * */public class DateStringFormatMapDTO {	// For example "07-26-2017".	private String dateString = "";	// For example "MM-dd-yyyy".	private String dateStringFormat = "";	public String getDateString() {		return dateString;	}	public void setDateString(String dateString) {		this.dateString = dateString;	}	public String getDateStringFormat() {		return dateStringFormat;	}	public void setDateStringFormat(String dateStringFormat) {		this.dateStringFormat = dateStringFormat;	}}

Java Code To Test Above Method

	public static void main(String[] args) {		JavaDateStringConverter.testJavaDateToString();		System.out.println("-------------------------");		JavaDateStringConverter.testJavaStringToDate();	}	private static void testJavaDateToString()	{		// Get current java.util.Date object.		Date currDate = new Date();		List<String> dateFormateStrList = new ArrayList<String>();		dateFormateStrList.add("dd-MMM-yyyy");		dateFormateStrList.add("dd-MM-yyyy HH:mm:ss");		dateFormateStrList.add("dd/MM/yyyy");		dateFormateStrList.add("MM dd, yyyy");		dateFormateStrList.add("E, MMM dd yyyy HH:mm:ss");		dateFormateStrList.add("E, MMM dd yyyy");		for(String dateFormatStr : dateFormateStrList)		{			JavaDateStringConverter.javaDateToString(currDate, dateFormatStr);			}	}	private static void testJavaStringToDate()	{		List<DateStringFormatMapDTO> datStringFormatMapDtoList = new ArrayList<DateStringFormatMapDTO>();		DateStringFormatMapDTO dsfmDto1 = new DateStringFormatMapDTO();		dsfmDto1.setDateString("25-07-2017");		dsfmDto1.setDateStringFormat("dd-MM-yyyy");		datStringFormatMapDtoList.add(dsfmDto1);		DateStringFormatMapDTO dsfmDto2 = new DateStringFormatMapDTO();		dsfmDto2.setDateString("25-07-2017 11:16:19");		dsfmDto2.setDateStringFormat("dd-MM-yyyy HH:mm:ss");		datStringFormatMapDtoList.add(dsfmDto2);		DateStringFormatMapDTO dsfmDto3 = new DateStringFormatMapDTO();		dsfmDto3.setDateString("25/07/2017");		dsfmDto3.setDateStringFormat("dd/MM/yyyy");		datStringFormatMapDtoList.add(dsfmDto3);		DateStringFormatMapDTO dsfmDto4 = new DateStringFormatMapDTO();		dsfmDto4.setDateString("07 25, 2017");		dsfmDto4.setDateStringFormat("MM dd, yyyy");		datStringFormatMapDtoList.add(dsfmDto4);		DateStringFormatMapDTO dsfmDto5 = new DateStringFormatMapDTO();		dsfmDto5.setDateString("Tue, Jul 25 2017 11:16:19");		dsfmDto5.setDateStringFormat("E, MMM dd yyyy HH:mm:ss");		datStringFormatMapDtoList.add(dsfmDto5);		DateStringFormatMapDTO dsfmDto6 = new DateStringFormatMapDTO();		dsfmDto6.setDateString("Tue, Jul 25 2017");		dsfmDto6.setDateStringFormat("E, MMM dd yyyy");		datStringFormatMapDtoList.add(dsfmDto6);		for(DateStringFormatMapDTO dateStringFormatMapDto : datStringFormatMapDtoList)		{			JavaDateStringConverter.javaStringToDate(dateStringFormatMapDto.getDateString(), dateStringFormatMapDto.getDateStringFormat());			}	}

Download “JavaDateToStringExample.zip” JavaDateToStringExample.zip – Downloaded 149 times – 1 KB

  • 5