Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Jeff Bender - 10 Apr 2007 00:06 GMT
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?
Daniel Pitts - 10 Apr 2007 00:18 GMT
> I want to show an error if a user enters a date with less than 4
> digits.
[quoted text clipped - 3 lines]
> 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);
   }
}
Joshua Cranmer - 10 Apr 2007 02:46 GMT
>         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)?
Daniel Pitts - 10 Apr 2007 06:46 GMT
> >         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.
Alan Krueger - 10 Apr 2007 06:52 GMT
> I want to show an error if a user enters a date with less than 4
> digits.
[quoted text clipped - 3 lines]
> 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.
Dr J R Stockton - 11 Apr 2007 19:02 GMT
>> I want to show an error if a user enters a date with less than 4
>> digits.
[quoted text clipped - 6 lines]
>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./

Signature

(c) John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v6.05   IE 6.
Web  <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

Jeff Bender - 10 Apr 2007 17:18 GMT
> I want to show an error if a user enters a date with less than 4
> digits.
[quoted text clipped - 3 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.