Guys, here is the source for a fully functional date document
to use it with a JTextField do this:
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
JTextField1.setDocument(new DateDocument(JTextField1, df));
Source code
______________________________________________
import javax.swing.text.*;
import java.awt.Font;
import java.awt.Toolkit;
import java.text.*;
import java.util.*;
public class DateDocument extends PlainDocument {
public static final
String INIT_STRING = "__/__/____";
private int sep1 = 2, sep2 = 5;
private JTextComponent textComponent;
private int newOffset, yearVal = 0;
private DateFormat df;
public DateDocument(JTextComponent tc, DateFormat dateFormat) {
df = dateFormat;
textComponent = tc;
try {
insertString(0, INIT_STRING, null);
}
catch(Exception ex) {
ex.printStackTrace();
}
}
private boolean resetDateByMonth(int dd, int mm, AttributeSet attributeSet) throws Exception {
int maxVal = getDaysInMonth(mm, yearVal);
if(dd > maxVal) {
super.remove(3, 2);
super.insertString(3, Integer.toString(maxVal), attributeSet);
return (true);
}
return (false);
}
private void resetDateByYear(int mm, int dd, int yy, AttributeSet attributeSet) throws Exception {
if((mm == 2) && ((yy % 4) != 0) && (dd == 29)) {
super.remove(4, 1);
super.insertString(4, "8", attributeSet);
}
}
public void insertString(int offset, String s, AttributeSet attributeSet)
throws BadLocationException
{
if (s==null | | (s.trim().equals("") && offset == 0))
{
super.remove(0, getLength());
super.insertString(0, INIT_STRING, attributeSet);
textComponent.setCaretPosition(0);
return;
}
if(s.equals(INIT_STRING))
{
super.insertString(offset, s, attributeSet);
}
else
{
if (s.length()>1)
{
try
{
df.parse(s);
super.remove(0, getLength());
super.insertString(0, s, attributeSet);
return ;
}
catch(Exception ex) {
Toolkit.getDefaultToolkit().beep();
return ;
}
}
try {
Integer.parseInt(s);
}
catch(Exception ex) {
Toolkit.getDefaultToolkit().beep();
return ;
}
newOffset = offset;
int value = Integer.parseInt(s);
try {
yearVal = Integer.parseInt(getText(6, 4));
}
catch(Exception e) {
}
if(newOffset == 0) {
try {
if(value > 1) {
return ;
}
int m2 = Integer.parseInt(getText(1, 1));
if((value == 1) && (m2 > 2)) {
super.remove(1, 1);
super.insertString(1, "0", attributeSet);
m2 = 0;
}
int dd = Integer.parseInt(getText(3, 2));
resetDateByMonth(dd, (value * 10 + m2), attributeSet);
}
catch(Exception e) {
}
}
else {
if(newOffset == 1) {
try {
int m1 = Integer.parseInt(getText(0, 1));
if((value > 2) && (m1 == 1)) {
s = "2";
}
int dd = Integer.parseInt(getText(3, 2));
resetDateByMonth(dd, (m1 * 10 + value), attributeSet);
}
catch(Exception e) {
}
}
else {
if((newOffset == 2) | | (newOffset == 3)) {
try {
if(value > 3) {
return ;
}
int mm = Integer.parseInt(getText(0, 2));
int d2 = Integer.parseInt(getText(4, 1));
if(resetDateByMonth((value * 10 + d2), mm, attributeSet)) {
textComponent.setCaretPosition(4);
return ;
}
}
catch(Exception e) {
}
}
else {
if(newOffset == 4) {
try {
int d1 = Integer.parseInt(getText(3, 1));
if((d1 == 3) && (value > 1)) {
return ;
}
int mm = Integer.parseInt(getText(0, 2));
if(resetDateByMonth((d1 * 10 + value), mm, attributeSet)) {
textComponent.setCaretPosition(6);
return ;
}
}
catch(Exception e) {
}
}
else {
if((newOffset == 5) | | (newOffset == 6)) {
try {
if((value > 2) | | (value < 1)) {<br /> return ;<br /> }<br /> int y1 = Integer.parseInt(getText(7, 1));<br /> if((value == 2) && (y1 > 0)) {
super.remove(7, 1);
super.insertString(7, "0", attributeSet);
textComponent.setCaretPosition(8);
}
else {
if((value == 1) && (y1 < 9)) {
super.remove(7, 1);
super.insertString(7, "9", attributeSet);
textComponent.setCaretPosition(8);
}
}
int mm = Integer.parseInt(getText(0, 2));
int dd = Integer.parseInt(getText(3, 2));
int yh = Integer.parseInt(getText(7, 3));
resetDateByYear(mm, dd, (value * 1000 + yh), attributeSet);
}
catch(Exception e) {
}
}
else {
if(newOffset == 7) {
try {
int y1 = Integer.parseInt(getText(6, 1));
if((y1 == 1) && (value < 9)) {
s = "9";
}
else {
if((y1 == 2) && (value > 0)) {
s = "0";
}
}
int mm = Integer.parseInt(getText(0, 2));
int dd = Integer.parseInt(getText(3, 2));
int yh = Integer.parseInt(getText(8, 2));
resetDateByYear(mm, dd, (y1 * 1000 + value * 100 + yh), attributeSet);
}
catch(Exception e) {
}
}
else {
if((newOffset == 8) | | (newOffset == 9)) {
try {
int yh = Integer.parseInt(getText(6, 2));
int mm = Integer.parseInt(getText(0, 2));
int dd = Integer.parseInt(getText(3, 2));
int y3 = 0, y4 = 0;
if(newOffset == 8) {
y3 = value;
y4 = Integer.parseInt(getText(9, 1));
}
else {
y3 = Integer.parseInt(getText(8, 1));
y4 = value;
}
resetDateByYear(mm, dd, (yh * 100 + y3 * 10 + y4), attributeSet);
}
catch(Exception e) {
}
}
}
}
}
}
}
}
if(atSeparator(offset)) {
newOffset++;
textComponent.setCaretPosition(newOffset);
}
super.remove(newOffset, 1);
super.insertString(newOffset, s, attributeSet);
}
}
public void remove(int offset, int length) throws BadLocationException {
if (length == 1)
{
super.remove(offset, 1);
super.insertString(offset, ""+ INIT_STRING.charAt(offset) , null);
}
textComponent.setCaretPosition(offset);
/*
if(atSeparator(offset)) {
textComponent.setCaretPosition(offset - 1);
}
else {
textComponent.setCaretPosition(offset);
}
*/
}
private boolean atSeparator(int offset) {
return offset == sep1 | | offset == sep2;
}
private int getDaysInMonth(int monthValue, int yy) {
switch(monthValue) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return (31);
case 2:
{
if((yy % 4) == 0) {
return (29);
}
else {
return (28);
}
}
case 4:
case 6:
case 9:
case 11:
return (30);
}
return (31);
}
}
Anand