posted 23 years ago
Dear Friend,
I've written a piece of code on my own to enter date in the JTextField in the format ("mm/dd/yyyy") I'm sending my code below. Hope this will satisfy your requirements. You can change my code if you Like.
JTextField jtextField = new JTextField();
Use "jtextfield.setDocument(new DateDocument(...));"
import javax.swing.text.*;
import java.awt.Font;
class DateDocument extends PlainDocument {
public static String initString = "mm/dd/yyyy";
private int sep1 = 2, sep2 = 5;
private JTextComponent textComponent;
private int newOffset, yearVal = 0;
public DateDocument(JTextComponent tc) {
textComponent = tc;
textComponent.setFont(new Font("TimesRoman", Font.BOLD, 14));
try {
insertString(0, initString, null);
initString = "mm/dd/yyyy";
}
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.equals(initString)) {
super.insertString(offset, s, attributeSet);
}
else {
try {
Integer.parseInt(s);
}
catch(Exception ex) {
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 /> 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(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);
}
}