Format input Date Time control Jieformatteddatebox

Source: Internet
Author: User
Tags date format end gettext integer return string format stringbuffer
Control
Format input Date Time control

can use Jieformatteddatebox box = new Jieformatteddatebox ("yyyy mm month DD Day hh point mm minute ss seconds");

You can also use jieformatteddatebox box = new Jieformatteddatebox ("HH:mm:ss") to enter the time

You can also use JSpinner spinner = new JSpinner (); Jieformatteddatebox fdb = new Jieformatteddatebox (format); Spinner.setmodel (FDB); Spinner.seteditor (FDB);



Package Org.jie.ui.formattedbox;

Import Java.sql.timestamp;import Java.text.SimpleDateFormat;

Import Java.awt.event.focusevent;import Java.awt.event.focuslistener;import Javax.swing.JFormattedTextField; Import Javax.swing.spinnermodel;import Javax.swing.swingconstants;import Javax.swing.swingutilities;import Javax.swing.event.changelistener;import Javax.swing.event.documentevent;import Javax.swing.event.documentlistener;import Javax.swing.text.defaultformatterfactory;import Javax.swing.text.MaskFormatter;

public class Jieformatteddatebox extends Jformattedtextfield implements spinnermodel{

Private String format;    Private SimpleDateFormat TimeFormat; Private Timestamp time;

public void SetTime (Timestamp time) {if (time!= null) SetValue (time);    Public Timestamp GetTime () {return time; }

public static string replace (string src,string replacesrc,string replacewith) {if (src = null) return null;    StringBuffer sb = new StringBuffer ();    int start = 0;    int end = Src.indexof (REPLACESRC);      while (end >= 0) {sb.append (src.substring (start,end));      Start = End+replacesrc.length ();      End = Src.indexof (Replacesrc,start);    Sb.append (replacewith);    } sb.append (Src.substring (start));  return sb.tostring (); }

Public Jieformatteddatebox (String format) {super ();        This.format = format;        TimeFormat = new SimpleDateFormat (format); Time = new Timestamp (System.currenttimemillis ());

try{String mask = format;            for (int i=0;i<alais.length;i++) {mask = replace (mask,alais[i],formats[i]);            } maskformatter MF = new Maskformatter (mask);            Mf.setplaceholdercharacter ('_');        Setformatterfactory (New Defaultformatterfactory (MF));        }catch (Exception e) {e.printstacktrace (); }

        SetText (Timeformat.format (time));         this.getdocument (). Adddocumentlistener (New Documentlistener () {             public void Insertupdate (Documentevent e) {                 checktime ();            }            public void Removeupdate (documentevent e) {                 checktime ();           }             public void Changedupdate (Documentevent e) {}        });        This.sethorizontalalignment ( Swingconstants.right);         This.addfocuslistener (New Focuslistener () {             public void focusgained (FocusEvent e) {}             public void Focuslost (FocusEvent e) {                 SetValue (Time);            }       });   }

    void Checktime () {        try{             String Text = GetText ();             if (text = null | | text.equals (""))                  return;            time = New Timestamp (Timeformat.parse (text). GetTime ());       }catch (Exception ex) {             try{                 Swingutilities.invokelater (New Runnable () {                     Public void Run () {                & nbsp;       SetValue (Time);                    }                });            }catch (Exception e) {}       }

}



    public Object GetValue () {        return This.gettext ();    }    public void SetValue (Object value) {        time = (Timestamp) value;        int index = this.getcaretposition ();         if (time = null) {          This.settext ("");        }else          This.settext (Timeformat.format (time));        this.setcaretposition (index); /keep caret position   }    public Object getnextvalue () {         return new Timestamp (Time.gettime () +getincrease (This.getcaretposition (), format,time));    }    public  Object getpreviousvalue () {        return new Timestamp (Time.gettime ()-getdecrease (This.getcaretposition (), format,time));    }    public void Addchangelistener (ChangeListener l) {}    public void Removechangelistener (ChangeListener l) {}

Static string[] Alais = {"yyyy", "MM", "DD", "HH", "hh", "MM", "SS"};    static string[] formats = {"#", "# #", "# #", "# #", "# #", "# #", "# #"};    Static long[] times = {0,0,86400000,3600000,3600000,60000,1000}; public static final SimpleDateFormat Fulltimeformat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");

    public static long getdecrease (int caret,string format,timestamp time) {         if (Testalias (format,caret,0)) {             try{                String Timestr = (Integer.parseint (new SimpleDateFormat ("yyyy"). Format (time)-1)                                    + "-" + New SimpleDateFormat ("Mm-dd HH:mm:ss"). Format (time);                 return Time.gettime ()- Fulltimeformat.parse (TIMESTR). GetTime ();           } catch (Exception e) {                Return1000;           }        }        Else if (Testalias (format,caret,1)) {             try{                 SimpleDateFormat ym = new SimpleDateFormat ("yyyy-mm");                 Long thismonth = Ym.parse (Ym.format (Time)). GetTime ();                 Long Lastmonth = Ym.parse (Ym.format (New Timestamp (thismonth-2*times[2))). GetTime ();

                int days = (int) ( Thismonth-lastmonth)/times[2]);                 int thisdays = Integer.parseint (new SimpleDateFormat ("DD"). Format (time);                 return (Thismonth-lastmonth) + ((Thisdays > Days)? (thisdays-days): 0) *times[2]);           }catch ( Exception e) {                return 1000;           }        }        for (int i=2;i<alais.length;i++) {             if (Testalias (format,caret,i)) {                 return times[i];            }       }        return 1000;   }

    public static long getincrease (int caret,string format,timestamp time) {         if (Testalias (format,caret,0)) {             try{                String Timestr = (Integer.parseint (new SimpleDateFormat ("yyyy"). Format (time) + 1)                                    + "-" + New SimpleDateFormat ("Mm-dd HH:mm:ss"). Format (time);                 return Fulltimeformat.parse (TIMESTR). GetTime ()-Time.gettime ();           }catch ( Exception e) {                return1000;           }        }        Else if (Testalias (format,caret,1)) {             try{                 SimpleDateFormat ym = new SimpleDateFormat ("yyyy-mm");                 Long thismonth = Ym.parse (Ym.format (Time)). GetTime ();                 Long Nextmonth = Ym.parse (Ym.format (New Timestamp (thismonth+31*times[2))). GetTime ();                 Long nextnextmonth = Ym.parse (Ym.format (New Timestamp (nextmonth+31 *TIMES[2])). GetTime ();

                int days = (int) ( Nextnextmonth-nextmonth)/times[2]);                 int thisdays = Integer.parseint (new SimpleDateFormat ("DD"). Format (time);                 return (Nextmonth-thismonth)-(( Thisdays > Days)? (thisdays-days): 0) *times[2]);           }catch ( Exception e) {                return 1000;           }        }        for (int i=2;i<alais.length;i++) {             if (Testalias (format,caret,i)) {                 return times[i];            }       }        return 1000;   }

private static Boolean Testalias (String format,int caret,int index) {for (int i=format.indexof (alais[index)); i>=0                ; I=format.indexof (alais[index],i+1)) {if (caret >= i && caret <= i+alais[index].length ())        return true;    return false; }

}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.