Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with SimpleDateFormat 1

Status
Not open for further replies.

rvy2k

Programmer
Jun 29, 2001
39
0
0
US
Hi,

I have this problem with the code below using JFormattedTextField with SimpleDateFormat. I can't insert a 1 on the month field or a 3 on the day field.

Thanks for your help!

///////////////////////////////////////////////////////////////////////
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;

public class Test {

public static void main(String[] args) {

SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
format.setLenient(false);

JFormattedTextField ftf = new JFormattedTextField(format);

((DateFormatter) ftf.getFormatter()).setAllowsInvalid(false);
((DateFormatter) ftf.getFormatter()).setOverwriteMode(true);
ftf.setValue(new Date());

Frame f = new Frame();
f.add(ftf);
f.setVisible(true);
}
}
 
Well actually, you can. The problem is that the resulting month or day cannot be invalid.

So, if the field starts with

09/03/2002

You cannot type a 1, because that would generate month 19, which is invalid.

Also in the day field, you cannot type a 3, because that would generate day 33, which is invalid.

To make things more complex, if the day value is 31, you cannot enter month 11, since that month only has 30 days.

In order to fill in 11/30/2002, for the month you would need to change the 9 into a 1, and then the 0 into a 1. For the day you can change the 3 into a 0 and the 0 into a 3.

Pieter
 
Pieter,

Thank you so much for the explanation. That makes a lot of sense now!
 
i have a last question:
how come the year field is not limited to only 4 digits?
i went up to 7 digits.

thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top