home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 20.6 KB | 687 lines |
- /*
- * @(#Tstamp.java
- *
- * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
- *
- */
-
-
- package symantec.itools.db.awt;
- import java.util.*;
- import java.awt.*;
- import java.sql.Timestamp;
-
- /**
- * A Tstamp component is a text field that accepts and displays text in common
- * date/time formats. It parses the text so that the date and time is available
- * numericly as well.
- */
- public class Tstamp extends java.awt.TextField{
-
- /**
- * The day of the month.
- */
- protected int day=1;
- /**
- * The month of the year. 0 = January.
- */
- protected int month=0;
- /**
- * The year.
- */
- protected int year=0;
- /**
- * The hour of the day. 13 = 1 PM.
- */
- protected int hour=0;
- /**
- * The minute of the hour.
- */
- protected int minute=0;
- /**
- * The second of the minute.
- */
- protected int second=0;
- /**
- * The nanosecond of the second.
- */
- protected int nanosec=0;
-
- /**
- * The current data display format.
- * @see #getDisplayFormat
- * @see #setDisplayFormat
- */
- protected String m_DisplayFormat="";
- /**
- * The current data entry format.
- * @see #getEntryFormat
- * @see #setEntryFormat
- */
- protected String m_EntryFormat="";
-
- private String DAY[]={"DD","D%","D+","D*","TH"};
- private String MON[]={"MM","M%","M+","M*"};
- private String YEA[]={"YY","Y*"};
- private String HOU[]={"HH","H%","hh","h%","AM"};
- private String MIN[]={"mm","m%"};
- private String SEC[]={"ss","s%"};
- private String NAN[]={"n"};
- private String nMON[]={"Jan.","Feb.","Mar.","Apr.","May ","Jun.","Jul.","Aug.","Sep.","Oct.","Nov.","Dec."};
- private String NMON[]={"January","February","March","April","May","June","July","August","September",
- "October","November","December"};
- private String nDAY[]={"Sun.","Mon.","Tue.","Wed.","Thu.","Fri.","Sat."};
- private String NDAY[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
-
- String WAYS[]={"DDMMYY",
- "D% M% YY",
- "D%/M%/Y*",
- "Y*-MM-DD",
- "DD/MM/YY",
- "D%-M+-YY",
- "D% M* Y*",
- "D+ D% M+ Y*",
- "D* D% M+ Y*",
- "M* D% YY",
- "D* M* D% Y*",
- "<< M%.D%.YY >>",
- "Today is D* the D% th of M* Y*",
- "DD -- MM -- Y*"};
- // CONSTRUCTORS
-
- /**
- * Constructs a Tstamp with the given date.
- * @param date the initial date displayed
- */
- public Tstamp(Date date)
- {
- this.setTstamp(date);
- }
-
- /**
- * Constructs a Tstamp with the given date/time.
- * @param tstamp the initial date and time displayed
- */
- public Tstamp( java.sql.Timestamp tstamp)
- {
- nanosec=tstamp.getNanos();
- this.setTstamp(tstamp);
- }
-
- /**
- * Constructs a default Tstamp.
- */
- public Tstamp()
- {
- this(new Date());
- }
-
- /**
- * Constructs a Tstamp with the given date.
- * @param date the initial date displayed
- */
- public Tstamp(String date)
- {
- this.readDate(date,m_EntryFormat);
- }
-
- /**
- * Constructs a Tstamp with the given date in the given format.
- * @param date the initial date displayed
- * @param format the format the date is in
- */
- public Tstamp(String date,String format)
- {
- this.readDate(date,format);
- }
-
-
- // SETTERS
-
- /**
- * Sets the value to the given date.
- * @param date the new date
- */
- public void setTstamp(String date)
- {
- this.readDate(date,m_EntryFormat);
- }
-
- /**
- * Sets the value to the given date in the given format.
- * @param date the new date
- * @param format the format the date is in
- */
- public void setTstamp(String date, String format)
- {
- this.readDate(date,format);
- }
-
- /**
- * Sets the value to the given date.
- * @param date the new date
- */
- public void setTstamp(Date date)
- {
- day=date.getDate();
- month=date.getMonth();
- year=date.getYear()+1900;
- hour=date.getHours();
- minute=date.getMinutes();
- second=date.getSeconds();
- }
-
- /**
- * Sets the text entry format to the same as the display format.
- * @param date the new date
- */
- public void setinequalout()
- {
- setEntryFormat(getDisplayFormat());
- }
-
- /**
- * Sets the display and input format.
- * @param dispfmt the new display and entry format
- * @see #getDisplayFormat
- */
- public void setDisplayFormat(String dispfmt)
- {
- m_DisplayFormat = dispfmt;
- setinequalout();
- }
-
- /**
- * Sets the entry format.
- * @param entfmt the new entry format
- * @see #getEntryFormat
- */
- public void setEntryFormat(String entfmt)
- {
- m_EntryFormat = entfmt;
- }
-
-
- // GETTERS
-
- /**
- * Returns the current entry format.
- * <p>
- * The entry format defines the order the user must follow to enter a valid
- * timestamp.
- * <p>
- * It is a can either be a String containing ôMö, öDö ,öYö, öhö,
- * ömö, ösö and ônö or a string using the values for a display format.
- * @see #setEntryFormat
- * @see #getDisplayFormat
- */
- public String getEntryFormat()
- {
- return (m_EntryFormat);
- }
-
- /**
- * Returns the current display format.
- * <p>
- * The display format defines the format in which the date, time, or
- * timestamp values are displayed.
- * Text phrases may also be entered as long as they do not contain any of
- * the valid values.
- * <p>
- * <b>Example:</b><p>
- * ôThe date: D* M* D%TH, Y* H%:mm:ss AM
- * <p>
- * will print
- * <p>
- * ôThe date: Friday October 12th , 1973 2:34:54 PM
- * <p>
- * <b>Note:</b> If the display format values do not include all the data elements
- * provided by the input data, only the values specified in the format will display.
- * <p>
- * For example, a display format of ôMM/DDö displays ô07/01ö even though the
- * complete data may be ô07/01/1996ö.
- * <p>
- * <table>
- * <tr><th>Value</th>
- * <th>Description</th></tr>
- * <tr><td><code>DD</code></td>
- * <td>Day - 2 digits [01-31]</td></tr>
- * <tr><td><code>D%</code></td>
- * <td>Day - 1 or 2 digits [1-31]</td></tr>
- * <tr><td><code>D+</code></td>
- * <td>Day - name of the day abbreviated [Mon.-Sun.]</td></tr>
- * <tr><td><code>D*</code></td>
- * <td>Day - name of the day complete [Monday-Sunday]</td></tr>
- * <tr><td><code>TH</code></td>
- * <td>puts the [ st-nd-rd-th ] element</td></tr>
- * <tr><td><code>MM</code></td>
- * <td>Month - 2 digits [01-12]</td></tr>
- * <tr><td><code>M%</code></td>
- * <td>Month - 1 to 2 digits [1-12]</td></tr>
- * <tr><td><code>M+</code></td>
- * <td>Month - name of the month abbreviated [Jan.-Dec.]</td></tr>
- * <tr><td><code>M*</code></td>
- * <td>Month - complete name of the month [January-December]</td></tr>
- * <tr><td><code>YY</code></td>
- * <td>Year - 2 digits</td></tr>
- * <tr><td><code>Y*</code></td>
- * <td>Year - 4 digits</td></tr>
- * <tr><td><code>hh</code></td>
- * <td>Hour - 2 digits</td></tr>
- * <tr><td><code>h%</code></td>
- * <td>Hour - 1 or 2 digits</td></tr>
- * <tr><td><code>HH</code></td>
- * <td>Hour - 2 digits, 12 hour clock</td></tr>
- * <tr><td><code>H%</code></td>
- * <td>Hour - 1 or 2 digits, 12 hour clock</td></tr>
- * <tr><td><code>AM</code></td>
- * <td>adds a AM or PM symbol</td></tr>
- * <tr><td><code>mm</code></td>
- * <td>Minutes - 2 digits</td></tr>
- * <tr><td><code>m%</code></td>
- * <td>Minutes - 1 or 2 digits</td></tr>
- * <tr><td><code>ss</code></td>
- * <td>Seconds - 2 digits</td></tr>
- * <tr><td><code>s%</code></td>
- * <td>Seconds - 1 or 2 digits</td></tr>
- * <tr><td><code>n1,n2àn9</code></td>
- * <td>fractions of a second, number specifies the number of digits.</td></tr>
- * </table>
- * @see #setDisplayFormat
- */
- public String getDisplayFormat()
- {
- return (m_DisplayFormat);
- }
-
- /**
- * Returns the current date/time as a string.
- */
- public String getTstamp()
- {
- return(this.exTstamp(m_DisplayFormat));
- }
-
- /**
- * Returns the current date/time as a string formatted in the specified way.
- * The parameter value specifies one of:
- * <table>
- * <tr><th>Value</th>
- * <th>Format (see getDisplayFormat)</th></tr>
- * <tr><td><code>0</code></td>
- * <td><code>DDMMYY</code></td></tr>
- * <tr><td><code>1</code></td>
- * <td><code>D% M% YY</code></td></tr>
- * <tr><td><code>2</code></td>
- * <td><code>D%/M%/Y*</code></td></tr>
- * <tr><td><code>3</code></td>
- * <td><code>Y*-MM-DD</code></td></tr>
- * <tr><td><code>4</code></td>
- * <td><code>DD/MM/YY</code></td></tr>
- * <tr><td><code>5</code></td>
- * <td><code>D%-M+-YY</code></td></tr>
- * <tr><td><code>6</code></td>
- * <td><code>D% M* Y*</code></td></tr>
- * <tr><td><code>7</code></td>
- * <td><code>D+ D% M+ Y*</code></td></tr>
- * <tr><td><code>8</code></td>
- * <td><code>D* D% M+ Y*</code></td></tr>
- * <tr><td><code>9</code></td>
- * <td><code>M* D% YY</code></td></tr>
- * <tr><td><code>10</code></td>
- * <td><code>D* M* D% Y*</code></td></tr>
- * <tr><td><code>11</code></td>
- * <td><code><< M%.D%.YY >></code></td></tr>
- * <tr><td><code>12</code></td>
- * <td><code>Today is D* the D% th of M* Y*</code></td></tr>
- * <tr><td><code>13</code></td>
- * <td><code>DD -- MM -- Y*</code></td></tr>
- * </table>
- * @param num the way to format the text
- * @see #getDisplayFormat
- */
- public String getTstamp(int num)
- {
- return(this.exTstamp(WAYS[num]));
- }
-
- /**
- * Returns the current date/time as a string formatted in the specified way.
- * @param exform the display format to use
- * @see #getDisplayFormat
- */
- public String getTstamp(String exform)
- {
- return(this.exTstamp(exform));
- }
-
-
- /**
- * Updates the text displayed using the current date/time value and display
- * format.
- * @see #getDisplayFormat
- */
- public void printTstamp()
- {setText(getTstamp(m_DisplayFormat));}
-
- /**
- * Returns the current Tstamp value as a Date.
- * @exception IllegalArgumentException
- * if the date value is out of range (<1970 or >2038)
- */
- public Date getDate()throws IllegalArgumentException
- {
-
- return(new Date(year-1900,month,day,hour,minute,second));
- }
-
- /**
- * Returns the current Tstamp value as a Timestamp.
- * @exception IllegalArgumentException
- * if the date value is out of range (<1970 or >2038)
- */
- public Timestamp getTimestamp()throws IllegalArgumentException
- {
- /*if(1900>year)
- {
- throw new IllegalArgumentException("Year out of range for Timestamp");
- }*/
- return(new Timestamp(year-1900,month,day,hour,minute,second,nanosec));
- }
-
-
-
- /**
- * Internal utility method.
- */
- protected void raiseException(Exception ex)
- {
- System.out.println("Exception class [" + ex.getClass().getName() + "]");
- System.out.println("Exception message [" + ex.getMessage() + "]");
- Thread.dumpStack();
- }
-
- /**
- * Utility method that parses the given date using the specified format.
- * @see #getEntryFormat
- */
- protected void readDate(String date,String format)
- {
- boolean douze=false;
- String tformat="";
- String sub="";
- int value=0;
- int pos=0;
- int act=0;
- int elem=0;
- int count=0;
- char actFormatChar;
- date+=" ";
-
- if(format.length()>2)
- {
- for(int x=0;x<format.length()-1;x++)
- {
- try{sub=format.substring(x,x+2);
- }
- catch(StringIndexOutOfBoundsException e)
- {
- raiseException(e);
- }
- if(sub.equals("DD")||sub.equals("D%")) tformat+="D";
- else if(sub.equals("MM")||sub.equals("M%")||sub.equals("M+")||sub.equals("M*")) tformat+="M";
- else if(sub.equals("YY")||sub.equals("Y*")) tformat+="Y";
- else if(sub.equals("HH")||sub.equals("H%")||sub.equals("hh")||sub.equals("h%")) tformat+="h";
- else if(sub.equals("m%")||sub.equals("mm")) tformat+="m";
- else if(sub.equals("s%")||sub.equals("ss")) tformat+="s";
- else if(sub.charAt(0)=='n')tformat+="n";
- }
- if(tformat.length()>1) format=tformat;
- }
-
- while (elem<format.length() && pos<date.length()){
-
- actFormatChar=format.charAt(elem);
- if(act==0)
- {
- while('0'<=date.charAt(pos) && date.charAt(pos)<='9')
- {
- value=value*10+date.charAt(pos)-'0';
- pos++;count++;act=1;
-
- if((actFormatChar=='M' ||actFormatChar=='D'||actFormatChar=='h'
- ||actFormatChar=='m'||actFormatChar=='s')&&(count>1))break;
- if((actFormatChar=='Y')&&(count>3))break;
- }
-
- if(actFormatChar=='n')
- {
- for(int j=0;j<9-count;j++)
- {
- value=value*10;
- }
- }
-
- if('0'>date.charAt(pos) || date.charAt(pos)>'9')
- {
- for(int x=0;x<nMON.length;x++)
- {
- if(nMON[x].regionMatches(true,0,date,pos,3) && actFormatChar=='M')
- {
- value=x+1;
- pos++;act=1;
- }
- }
- if("PM".regionMatches(true,0,date,pos,2))
- {
- douze=true;
- }
- pos++;
- }
- }
-
- if(actFormatChar=='M') month=value-1;
- if(actFormatChar=='D') day=value;
- if(actFormatChar=='Y')
- {
- year=value;
- if (year<101)year+=1900;
- }
- if(actFormatChar=='h') hour=value;
- if(actFormatChar=='m') minute=value;
- if(actFormatChar=='s') second=value;
- if(actFormatChar=='n') nanosec=value;
- if (douze && hour<12)hour=hour+12;
-
- if(act!=0){elem++;count=0;act=0;value=0;}
-
- }
- while(pos<date.length())
- {
- if("PM".regionMatches(true,0,date,pos,2))
- {
- if(hour<12)hour+=12;
- }
- pos++;
- }
- if(month>11) month=11;
- if(month<0) month=0;
- if (month==1 && day>=29)
- {
- if(calculday(29,1,year)==calculday(1,2,year))
- {
- day=28;
- }
- else
- {
- day=29;
- }
- }
- else if(day>(moffset[month+1]-moffset[month])) day=(moffset[month+1]-moffset[month]);
- if(day<1) day=1;
- if(hour>23) hour=23;
- if(hour<0) hour=0;
- if(minute>59) minute=59;
- if(minute<0) minute=0;
- if(second>59) second=59;
- if(second<0) second=0;
-
- }
-
- /**
- * Returns the current date/time as a string formatted in the specified way.
- * @see #getDisplayFormat
- */
- protected String exTstamp(String exform)
- {
- int pos=0;
- int actpos=pos;
- int amhour=hour;
- if(hour>11)amhour=hour-12;
- int nansize=0;
- String snan="";
- String output="";
- exform+=" ";
- while (pos<exform.length())
- {
- actpos=pos;
- //DAY[]={"DD","D%","D+","D*","TH"};
- for(int x=0;x<DAY.length;x++)
- {
- if(DAY[x].regionMatches(0,exform,pos,DAY[x].length()))
- {
- pos+=DAY[x].length()-1;
- if(x==0)
- {
- if(day<10)output+="0";
- output+=Integer.toString(day);
- }
- else if(x==1)output+=Integer.toString(day);
- else if(x==2)output+=nDAY[calculday(day,month,year)];
- else if(x==3)output+=NDAY[calculday(day,month,year)];
- else if(x==4)
- {
- int lastDigitOfDay=day%10;
- if(day<10 ||day>13)
- {
- if(lastDigitOfDay==1)output+="st";
- else if(lastDigitOfDay==2)output+="nd";
- else if(lastDigitOfDay==3)output+="rd";
- else output+="th";
- }
- else output+="th";
- }
- }
- }
- //MON[]={"MM","M%","M+","M*"};
- for(int x=0;x<MON.length;x++)
- {
- if(MON[x].regionMatches(0,exform,pos,MON[x].length()))
- {
- pos+=MON[x].length()-1;
- if(x==0)
- {
- if(month+1<10)output+="0";
- output+=Integer.toString(month+1);
- }
- else if(x==1)output+=Integer.toString(month+1);
- else if(x==2)output+=nMON[month];
- else if(x==3)output+=NMON[month];
- }
- }
- //YEA[]={"YY","Y*"};
- for(int x=0;x<YEA.length;x++)
- {
- if(YEA[x].regionMatches(0,exform,pos,YEA[x].length()))
- {
- pos+=YEA[x].length()-1;
- if(x==0)output+=Integer.toString(year-(year/100)*100);
- else if(x==1)output+=Integer.toString(year);
- }
- }
- //HOU[]={"HH","H%","hh","h%","AM"};
- for(int x=0;x<HOU.length;x++)
- {
- if(HOU[x].regionMatches(0,exform,pos,HOU[x].length()))
- {
- pos+=HOU[x].length()-1;
- if(x==0)
- {
- if(amhour<10)output+="0";
- output+=Integer.toString(amhour);
- }
- else if(x==1)output+=Integer.toString(amhour);
- else if(x==2)
- {
- if(hour<10)output+="0";
- output+=Integer.toString(hour);
- }
- else if(x==3)output+=Integer.toString(hour);
- else if(x==4)
- {
- if(hour>11)output+="PM";
- if(hour<12)output+="AM";
- }
- }
- }
- //MIN[]={"mm","m%"};
- for(int x=0;x<MIN.length;x++)
- {
- if(MIN[x].regionMatches(0,exform,pos,MIN[x].length()))
- {
- pos+=MIN[x].length()-1;
- if(x==0)
- {
- if(minute<10)output+="0";
- output+=Integer.toString(minute);
- }
- else if(x==1)output+=Integer.toString(minute);
- }
- }
- //SEC[]={"ss","s%"};
- for(int x=0;x<SEC.length;x++)
- {
- if(SEC[x].regionMatches(0,exform,pos,SEC[x].length()))
- {
- pos+=SEC[x].length()-1;
- if(x==0)
- {
- if(second<10)output+="0";
- output+=Integer.toString(second);
- }
- else if(x==1)output+=Integer.toString(second);
- }
- }
- //NAN[]={"n"};
- for(int x=0;x<NAN.length;x++)
- {
- if(NAN[x].regionMatches(0,exform,pos,NAN[x].length()))
- {
- pos+=NAN[x].length();
- nansize=(int)(exform.charAt(pos)-'0');
- snan=Integer.toString(nanosec+1000000000);
- if(x==0)output+=snan.substring(1,nansize+1);
- }
- }
- if(actpos==pos)output+=exform.charAt(pos);
- pos++;
- }
- return(output);
- }
-
- private int moffset[]={0,31,59,90,120,151,181,212,243,273,304,334,365};
-
- /**
- * Returns the day of the week given the day, month, and year.
- * @see #getDisplayFormat
- */
- public int calculday(int day,int month,int year)
- {
- Date date=new Date(year-1900,month,day);
- return (date.getDay());
- }
-
- }
-
-
-