how to make SimpleDateFormat("yyyy-MM-dd") enforce 4 digit years?

J

Jeff Bender

I want to show an error if a user enters a date with less than 4
digits.

I have this:
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
but format.parse("123-10-12") doesn't throw a parse exception. How do
I make a SDF that will throw an exception in that case?
 
D

Daniel Pitts

I want to show an error if a user enters a date with less than 4
digits.

I have this:
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
but format.parse("123-10-12") doesn't throw a parse exception. How do
I make a SDF that will throw an exception in that case?

I don't actually see the benefit.
If you REALLY care, you can test a regex.

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

public class Test {
public static void main(String[] args) throws ParseException {
String dateString = "123-10-12";
Date date = parseDate(dateString);
System.out.println(date);

}

private static Date parseDate(String dateString) throws
ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
if (!dateString.matches("[0-9]{4,4}-[0-9]+-[0-9]+")) {
throw new IllegalArgumentException("Not a valid date");
}

return format.parse(dateString);
}
}
 
J

Joshua Cranmer

Daniel said:
if (!dateString.matches("[0-9]{4,4}-[0-9]+-[0-9]+")) {
Shouldn't it be "\\d{4}-\\d{2}-\\d{2}" (ignoring 28/29/30/31 days in the
month part)?
 
D

Daniel Pitts

Daniel said:
if (!dateString.matches("[0-9]{4,4}-[0-9]+-[0-9]+")) {

Shouldn't it be "\\d{4}-\\d{2}-\\d{2}" (ignoring 28/29/30/31 days in the
month part)?

I was being pedantic about the OPs question.
He specifically asked about 3 digit dates. not 1 digit month/day.
 
A

Alan Krueger

Jeff said:
I want to show an error if a user enters a date with less than 4
digits.

I have this:
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
but format.parse("123-10-12") doesn't throw a parse exception. How do
I make a SDF that will throw an exception in that case?

It's debatable whether that's a valid Gregorian date, being that it lies
in the year 123, before the origination date of the Gregorian calendar.
However, strictly, that date string follows that format.

If you want those placeholders to be exactly the number of digits you
specify, then you'll want to use a regular expression, as suggested in a
separate response.

If however you want to limit the parsed dates to years you consider
valid, just use the Calendar class and the Date you get back and perform
supplemental validation on the year.
 
J

Jeff Bender

I want to show an error if a user enters a date with less than 4
digits.

I have this:
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
but format.parse("123-10-12") doesn't throw a parse exception. How do
I make a SDF that will throw an exception in that case?

Dan,Josh,Alan,

Thanks for the help. I went with the regex way, but might change to
the GregorianCalendar way in the future.

Thanks.
 
D

Dr J R Stockton

In comp.lang.java.programmer message said:
It's debatable whether that's a valid Gregorian date, being that it
lies in the year 123, before the origination date of the Gregorian
calendar.

It is not a valid ISO 8601 date, since (IIRC) the standard requires a
four-digit year. But "0123-10-12" would be fine.

The proleptic Gregorian Calendar extends back as far as one likes before
1582 (but be careful about whether the year before the year before AD 1
is BC 2 or -1). See via sig.

Javascript is not Java. But the Javascript date algorithms which I have
in <URL:http://www.merlyn.demon.co.uk/js=dates.htm> /ff./ might well be
adaptable to Java.

If Java lacks built-in support for ISO week numbers, consider adapting
the javascript on my site; it is better than MS's bug-fix. WARNING : In
Win98..Vista, VBscript's DatePart gets the week number of 2007-12-31
(and other dates) wrong. If using MS's or other libraries for ISO WN, I
suggest a careful check. See <URL:http://www.merlyn.demon.co.uk/vb=date
s.htm> /ff./
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top