⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datefield.java

📁 有关j2me的很好的例子可以研究一下
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/* * @(#)DateField.java	1.98 01/09/17 * Copyright (c) 1999-2001 Sun Microsystems, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information").  You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */package javax.microedition.lcdui;import java.util.Calendar;import java.util.Date;import java.util.TimeZone;import com.sun.midp.lcdui.Resource;/** * A DateField is an editable component for presenting date and time (calendar) * information that may be placed into a Form. Value for this field can be * initially set or left unset. If value is not set then the UI for the field * shows this clearly. The field value for "not initialized state" is not valid * value and <code>getDate()</code> for this state returns null. * <p> * Instance of a DateField can be configured to accept date or time information * or both of them. This input mode configuration is done by DATE, TIME or * DATE_TIME static fields of this class. DATE input mode allows to set only * date information and TIME only time information (hours, minutes). DATE_TIME * allows to set both clock time and date values. * <p> * In TIME input mode the date components of Date object * must be set to the "zero epoch" value of January 1, 1970. * <p> * Calendar calculations in this field are based on default locale and defined * time zone. Because of the calculations and different input modes date object * may not contain same millisecond value when set to this field and get back * from this field. */public class DateField extends Item {    /**     * Flag to signal the clock representation uses AM and PM notation     */    private static final boolean CLOCK_USES_AM_PM = true;    /**     * <P>Input mode for date information (day, month, year). With     * this mode this     * DateField presents and allows only to modify date value. The time     * information of date object is ignored.</P>     *     * <P>Value 1 is assigned to DATE.</P>     */    public static final int DATE = 1;    /**     * <P>Input mode for time information (hours and minutes). With     * this mode this     * DateField presents and allows only to modify time. The date components     * should be set to the "zero epoch" value of January 1, 1970 and     * should not be     * accessed.</P>     *     * <P>Value 2 is assigned to TIME.</P>     */    public static final int TIME = 2;    /**     * <P>Input mode for date (day, month, year) and time (minutes, hours)     * information. With this mode this DateField presents and allows to modify     * both time and date information.</P>     *     * <P>Value 3 is assigned to DATE_TIME.</P>     */    public static final int DATE_TIME = 3;    /**     * Creates a DateField object with the specified label and mode. This call     * is identical to <code>DateField(label, mode, null)</code>.     *     * @param label item label     * @param mode the input mode, one of DATE, TIME or DATE_TIME     * @exception IllegalArgumentException if the input mode's value is invalid     */    public DateField(String label, int mode) {        this(label, mode, null);    }    /**     * Creates a date field in which calendar calculations are based on specific     * TimeZone object and the default calendaring system for the current     * locale.     * The value of the DateField is initially in the "uninitialized" state.     * If timeZone is null, the system's default time zone is used.     *     * @param label item label     * @param mode the input mode, one of DATE, TIME or DATE_TIME     * @param timeZone a specific time zone, or null for the default time zone     * @exception IllegalArgumentException if the input mode's value is invalid     */    public DateField(String label, int mode, java.util.TimeZone timeZone) {        super(label, 3);        synchronized (Display.LCDUILock) {            if ((mode != DATE) && (mode != TIME) && (mode != DATE_TIME)) {                throw new IllegalArgumentException("Invalid input mode");            }            this.mode = mode;            if (timeZone == null) {                timeZone = TimeZone.getDefault();            }            this.currentDate = Calendar.getInstance(timeZone);            layouts[1] = new StringLayout(                toString((mode == TIME) ? TIME : DATE), Screen.CONTENT_FONT);            layouts[2] = new StringLayout(                (mode == DATE_TIME) ? toString(TIME) : null,                Screen.CONTENT_FONT);        } // synchronized    }    /**     * Returns date value of this field. Returned value is null if     * field value is     * not initialized. The date object is constructed according the rules of     * locale specific calendaring system and defined time zone.     *     * In TIME mode field the date components are set to the "zero     * epoch" value of January 1, 1970. If a date object that presents time     * beyond one day from this "zero epoch" then this field is in "not     * initialized" state and this method returns null.     *     * In DATE mode field the time component of the calendar is set to zero when     * constructing the date object.     *     * @return date object representing time or date depending on input mode     * @see #setDate     */    public java.util.Date getDate() {        synchronized (Display.LCDUILock) {            // NOTE:             // defensive copy of the Date object is necessary             // because CLDC's Calendar returns a reference to an internal,             // shared Date object.  See bugID: 4479408.            return (initialized ?                 new java.util.Date(currentDate.getTime().getTime()) : null);        } // synchronized    }    /**     * Sets a new value for this field. Null can be passed to set the field     * state to "not initialized" state. The input mode of this field defines     * what components of passed Date object is used.<p>     *     * In TIME input mode the date components must be set to the "zero     * epoch" value of January 1, 1970. If a date object that presents time     * beyond one day then this field is in "not initialized" state.     * In TIME input mode the date component of Date object is ignored and time     * component is used to precision of minutes.<p>     *     * In DATE input mode the time component of Date object is ignored.<p>     *     * In DATE_TIME input mode the date and time component of Date are used but     * only to precision of minutes.     *     * @param date new value for this field     * @see #getDate     */    public void setDate(java.util.Date date) {        synchronized (Display.LCDUILock) {            if (date == null) {                initialized = false;            } else {                currentDate.setTime(date);                if (mode == TIME) {                    // NOTE:                    // It is unclear from the spec what should happen                     // when DateField with TIME mode is set to                    // a value that is on a day other than 1/1/1970.                    //                    // Two possible interpretations of the spec are:                    // 1. DateField is put into the "uninitialized" state; or                    // 2. The time portion of the DateField is set to the                     //    time-of-day portion of the Date object passed in,                     //    and the date portion of the DateField is set                     //    to 1/1/1970.                    //                    // Currently we are using the first approach.                    initialized =                        (currentDate.get(Calendar.YEAR)  == 1970) &&                        (currentDate.get(Calendar.MONTH) == Calendar.JANUARY)                        && (currentDate.get(Calendar.DATE)   == 1);                } else {                    // Currently spec does not prohibit from losing                    // irrelevant for that mode information                    // so we always zero out hours and minutes                    // NOTE: the specification doesn't prohibit                     // the loss of information irrelevant to                     // the current input mode, so we always zero out the                    // hours and minutes.                    if (mode == DATE) {                        currentDate.set(Calendar.HOUR, 0);                        currentDate.set(Calendar.MINUTE, 0);                    }                    initialized = true;                }                // always ignore seconds and milliseconds                currentDate.set(Calendar.SECOND, 0);                currentDate.set(Calendar.MILLISECOND, 0);            }            contentChanged();        } // synchronized    }    /**     * Gets input mode for this date field. Valid input modes are     * DATE, TIME and DATE_TIME.     *     * @return input mode of this field     * @see #setInputMode     */    public int getInputMode() {        // SYNC NOTE: return of atomic value, no locking necessary            return mode;    }    /**     * Set input mode for this date field. Valid input modes are     * DATE, TIME and DATE_TIME.     *     * @param mode the input mode, must be one of DATE, TIME or DATE_TIME     * @exception IllegalArgumentException if an invalid value is specified     * @see #getInputMode     */    public void setInputMode(int mode) {        if ((mode != DATE) && (mode != TIME) && (mode != DATE_TIME)) {            throw new IllegalArgumentException("Invalid input mode");        }        synchronized (Display.LCDUILock) {            if (this.mode != mode) {		int oldMode = this.mode;                this.mode = mode;                // While the input mode is changed                // some irrelevant values for new mode could be lost.                 // Currently that is allowed by the spec.                // So for TIME mode we make sure that time is set                // on a zero epoch date                // and for DATE mode we zero out hours and minutes                if (mode == TIME) {                    currentDate.set(Calendar.YEAR, 1970);                    currentDate.set(Calendar.MONTH, Calendar.JANUARY);                    currentDate.set(Calendar.DATE, 1);                } else if (mode == DATE) {                    currentDate.set(Calendar.HOUR, 0);                    currentDate.set(Calendar.MINUTE, 0);                }		// set layouts to correct date/time information		// propogate height changes to the form height changed		int deltaHeight = ((StringLayout)layouts[1]).setString(                                  toString(mode == TIME ? TIME : DATE));		if (mode == DATE_TIME) {		    deltaHeight += ((StringLayout)layouts[2]).setString(                                     toString(TIME));		} else if (oldMode == DATE_TIME) {		    deltaHeight += ((StringLayout)layouts[2]).setString(                                     null);		    // if both (DATE and TIME) fields were present and		    // highlight was on the TIME field and		    // move the highlight to be on the DATE since		    // TIME does not exist any more with the mode change		    if (hasFocus() && highlight == 2) {			highlight = 1;		    }		}				height += deltaHeight;		contentChanged(0, 0, deltaHeight);            }        } // synchronized    }    /**     * Sets the label of the Item. If label is null, specifies that this     * item has no label.     * @param label the label string     */    public void setLabel(String label) {        synchronized (Display.LCDUILock) {            super.setLabel(label);            int deltaHeight = ((StringLayout)layouts[0]).setString(label);            height += deltaHeight;            contentChanged(0, 0, deltaHeight);        }    }    // package private    /**     * Called from the Date Editor to save the selected Date.     *     * @param date The Date object to which current date should be set.     */    void saveDate(java.util.Date date) {        initialized = true;                currentDate.setTime(date);        contentChanged();    }    /**     * Paint this DateField     *     * @param g The Graphics object to paint to     */    void paint(Graphics g) {        boolean inverted = hasFocus();        if (layouts != null) {            int translatedY = 0;            for (int i = 0; i < layouts.length; i++) {                int h = layouts[i].getHeight();                layouts[i].paint(g, true,                    inverted && (i > 0) && (i == highlight));                g.translate(0, h);                translatedY += h;            }            g.translate(0, -translatedY);        }    }    /**     * Get the am/pm text given a Calandar     *     * @param calendar The Calendar object to retrieve the time from     * @return String The am/pm text based on the time in the calendar     */    static String ampmString(Calendar calendar) {        int hour   = calendar.get(Calendar.HOUR_OF_DAY);        int minute = calendar.get(Calendar.MINUTE);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -