This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JFormattedTextField and Date  RSS feed

 
Serghei Jelauc
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • 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.
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!