Date and SimpleDateFormat

By | November 28, 2016

date.toString() would do if wanted to have a SimpleDateFormat(yyyy-MM-dd HH mm ss SSS)


Parsing back to Date from String..

Date d = new Date();
long l = d.getTime();
Date theSameDate = new Date(l);

Date d = new Date();
String s = d.toString;
Date theSameDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(s);


Validating the formatted date..

SimpleDateFormat sdf = new SimpleDateFormat(dateFromat);

		try {

			//if not valid, it will throw ParseException
			Date date = sdf.parse(dateToValidate);

		} catch (ParseException e) {

			return false;
How to check if date is valid in Java
Date and Calendar examples here..



Manipulating directly the date object for adding days would result in weird behavior during day light savings change..

Leave a Reply

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