Date and SimpleDateFormat

By | November 28, 2016

http://stackoverflow.com/questions/1459656/how-to-get-the-current-time-in-yyyy-mm-dd-hhmisec-millisecond-format-in-java

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

 

Parsing back to Date from String..

http://stackoverflow.com/questions/9431927/how-to-convert-date-tostring-back-to-date

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);
		sdf.setLenient(false);

		try {

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

		} catch (ParseException e) {

			e.printStackTrace();
			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 *