글쓴이 :이채욱 1999년 4월 03일 08:47:46
In Reply to: 날짜의 유효 검사와 날짜 증가 구현... posted by 이채욱 on 1999년 4월 01일 18:53:26:
void Dateminus_ActionPerformed(java.awt.event.ActionEvent event)
{
try
{
String text = dateT.getText();
int start = text.indexOf('-');
int last = text.lastIndexOf('-');
String year = text.substring(0,start);
String month = text.substring(start+1,text.indexOf('-',start+1));
String day = text.substring(last+1);
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy.MM.dd");
ParsePosition pos = new ParsePosition(0);
Date current = formatter.parse(year+"."+month+"."+day, pos);
if(Integer.parseInt(month) == current.getMonth()+1)
{
java.util.GregorianCalendar gc = new java.util.GregorianCalendar(Integer.parseInt(year),Integer.parseInt(month)-1,Integer.parseInt(day));
gc.roll(Calendar.DAY_OF_YEAR,-1);
gc.getGregorianChange();
//System.out.println(gc);
//System.out.println(gc.get(Calendar.MONTH));
String a;
a = Integer.toString(gc.get(Calendar.YEAR))+"-";
if (gc.get(Calendar.MONTH) < 9)
a = a + "0" + Integer.toString(gc.get(Calendar.MONTH)+1)+"-";
else
a = a + Integer.toString(gc.get(Calendar.MONTH)+1)+"-";
if (gc.get(Calendar.DAY_OF_MONTH) < 10)
a = a + "0" + Integer.toString(gc.get(Calendar.DAY_OF_MONTH));
else
a = a + Integer.toString(gc.get(Calendar.DAY_OF_MONTH));
dateT.setText(a);
//System.out.println("Sucess -1");
}
else
{
dateT.requestFocus();
//System.out.println("Fail -1");
}
}
catch(NumberFormatException e)
{
dateT.requestFocus();
//System.out.println("Exception -1");
}
}
위의 소스는 제가 dateT라는 텍스트 박스에 "1999-02-27" 과 같은 String을 버튼을 누르면
날짜가 자동 감소하는 것을 구현한 것입니다..
무슨 내용인지 잘 모르겠으면 앞의 내용들을 참조 하십시오.
앞에서 제가 간과한 것은
Calendar,GregorianCalendar 클래스의 달은 0~11까지의 값을 가진다는 것이었읍니다.
저와 같은 문제를 가지고 계신 분들에게 많은 도움되었으면 합니다.
그리고 제 질문에 대답해 주신 분들께 감사 드립니다.
앞으로도 많은 도움 부탁드리며...