• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

JFormattedTextField and Date

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all.
I have created JFormattedTextField with the mask (##/##/##). In this field the Date will be inserted by users. For example let first two digits will be month. The question is how I can restrict input first number if it more than 12?

Thanks in advance.
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
This isn't a mask, but for dates you could use:

DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
JFormattedTextField txtDate = new JFormattedTextField(df);
txtDate.setColumns(10);

the default behavior with this is that if you give months greater than 12, it adds that many months to the date:

so typing 13/22/2003 gets changed to 1/22/2004

to stop this, use
df.setLenient(false)
 
Serghei Jelauc
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot. But one thing still nonunderstandable.
This is my code:


if month is for example 07 and I want to input "12" the JFormattedTextField doesn't give me to do that, because when I input first digit "1" it interprets it as 17.
How to be in that case?

Thanks in advance
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic