Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleDateFormat giving wrong date instead of error

I am using following pattern and date

Date : 13-13-2007

Pattern : dd-MM-yyyy

Output: Sun Jan 13 00:00:00 IST 2008 Or 2008-01-13 00:00:00.0

I was expecting exception here. What can i do to generate exception when given date is inproper.

like image 932
Amit Kumar Gupta Avatar asked May 17 '11 09:05

Amit Kumar Gupta


People also ask

How do I change date format in SimpleDateFormat?

Create Simple Date Format // Date Format In Java String pattern = "yyyy-MM-dd"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); According to the example above, the String pattern will be used to format dates, and the output will be shown as "yyyy-MM-dd".

What can I use instead of SimpleDateFormat?

DateTimeFormatter is a replacement for the old SimpleDateFormat that is thread-safe and provides additional functionality.

Is SimpleDateFormat deprecated?

Deprecated. A class for parsing and formatting dates with a given pattern, compatible with the Java 6 API.

How do I convert a date to a specific format?

Select the dates you want to convert, right click to select Format Cells from context menu. 2. In the Format Cells dialog, under Number tab, select Date from Category list, and then select one format you want to convert to from the right section.


1 Answers

Use DateFormat.setLenient(false) to tell the DateFormat/SimpleDateFormat that you want it to be strict.

like image 156
Joachim Sauer Avatar answered Oct 27 '22 00:10

Joachim Sauer