datetimedemo.java
来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 958 行 · 第 1/2 页
JAVA
958 行
dateStyleMenu= new Choice();
dateStyleMenu.addItemListener(this);
dateStyleMenu.addItem("Full");
dateStyleMenu.addItem("Long");
dateStyleMenu.addItem("Default");
dateStyleMenu.addItem("Short");
dateStylePanel.add("loc",dateStyleLabel);
dateStylePanel.add(dateStyleMenu);
Panel timeStylePanel=new Panel();
timeStylePanel.setLayout(new GridLayout(2,1));
timeStyleLabel=new Label("Time Style");
timeStyleLabel.setFont(Utility.labelFont);
timeStyleMenu= new Choice();
timeStyleMenu.addItemListener(this);
timeStyleMenu.addItem("Full");
timeStyleMenu.addItem("Long");
timeStyleMenu.addItem("Default");
timeStyleMenu.addItem("Short");
timeStylePanel.add("loc",timeStyleLabel);
timeStylePanel.add(timeStyleMenu);
Panel dtStylePanel = new Panel();
dtStylePanel.setLayout(new GridLayout(1,2,20,0));
dtStylePanel.add(dateStylePanel);
dtStylePanel.add(timeStylePanel);
formatPanel.add(formatButtons);
formatPanel.add(dtStylePanel);
Utility.fixGrid(formatPanel,1);
Panel localesFormatPanel = new Panel();
localesFormatPanel.add(localePanel);
localesFormatPanel.add(formatPanel);
Utility.fixGrid(localesFormatPanel,1);
add(localesFormatPanel);
Panel copyrightPanel = new Panel();
addWithFont (copyrightPanel,new Label(copyrightString, Label.LEFT),
Utility.creditFont);
addWithFont (copyrightPanel,new Label(copyrightString2, Label.LEFT),
Utility.creditFont);
Utility.fixGrid(copyrightPanel,1);
add(copyrightPanel);
//}}
}
/* ActionListener method */
public void actionPerformed(ActionEvent e) {
if (e.getSource() == up){
dateFieldChanged(true);
} else if (e.getSource() == down) {
dateFieldChanged(false);
}
}
/* ItemListener method */
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == localeMenu) {
selectedLocaleMenu();
} else if (e.getSource() == dateStyleMenu) {
selectedDateStyleMenu();
} else if (e.getSource() == timeStyleMenu) {
selectedTimeStyleMenu();
} else if (e.getSource() == cityMenu) {
cityChanged();
} else if (e.getSource() == getRoll) {
clickedGetRoll();
} else if (e.getSource() == getAdd) {
clickedGetAdd();
} else if (e.getSource() == getLocalized) {
isLocalized = getLocalized.getState();
handleNewFormat();
} else if (e.getSource() == getValidationMode) {
validationMode = getValidationMode.getState();
validationModeChanged();
} else if (e.getSource() == getDateInstance) {
clickedGetDateFormat();
} else if (e.getSource() == getTimeInstance) {
clickedGetTimeFormat();
} else if (e.getSource() == getDateTimeInstance) {
clickedGetDateTimeFormat();
}
}
/* KeyListener methods */
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
if (e.getSource() == patternText) {
e.consume();
patternTextChanged();
} else if (e.getSource() == inputText) {
e.consume();
formatText();
} else if (e.getSource() == outputText) {
e.consume();
parseText();
} else if (e.getSource() == millisText) {
e.consume();
millisChanged();
}
}
public void keyTyped(KeyEvent e) {
}
/* Window Listener methods */
public void windowClosed(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
setVisible(false);
dispose();
if (applet != null) {
applet.demoClosed();
} else System.exit(0);
}
/**
* This function is called when users select a new time and/or date
* format pattern, or a new locale.
*/
public void setFormatFromLocale(boolean localChanged) {
int localeIndex = localeMenu.getSelectedIndex();
int dateStyleIndex = dateStyleMenu.getSelectedIndex()
+ DateFormat.FULL;
int timeStyleIndex = timeStyleMenu.getSelectedIndex()
+ DateFormat.FULL;
if (localChanged)
// Find the locale corresponding to the selected menu item
curLocale = locales[localeIndex];
if (getDateInstance.getState()) {
format =
(SimpleDateFormat) DateFormat.getDateInstance(dateStyleIndex,
curLocale);
} else if (getTimeInstance.getState()) {
format =
(SimpleDateFormat) DateFormat.getTimeInstance(timeStyleIndex,
curLocale);
} else {
format =
(SimpleDateFormat) DateFormat.getDateTimeInstance(dateStyleIndex,
timeStyleIndex,
curLocale);
}
patternText.setText( format.toPattern() );
if (!localChanged)
{
// locale not changed, only pattern format is changed.
setMillisText();
millisFormat();
}
else // change to selecting a rep. city based on new locale selected
{
cityMenu.select(kAdjCityIndex[localeIndex]);
cityChanged();
}
}
/**
* This function is called when users change the pattern text.
*/
public void setFormatFromPattern() {
String timePattern = patternText.getText();
format.applyPattern(timePattern);
millisFormat();
millisParse();
}
private boolean add = false;
/**
* This function is called when the "Roll" radio button is selected.
*/
public void clickedGetRoll() {
add=false;
}
/**
* This function is called when the "Add" radio button is selected.
*/
public void clickedGetAdd() {
add=true;
}
/**
* This function is called when the "Date Format" radio button is selected.
*/
public void clickedGetDateFormat() {
setFormatFromLocale(false);
}
/**
* This function is called when the "Time Format" radio button is selected.
*/
public void clickedGetTimeFormat() {
setFormatFromLocale(false);
}
/**
* This function is called when the "Date and Time Format" radio button
* is selected.
*/
public void clickedGetDateTimeFormat() {
setFormatFromLocale(false);
}
/**
* This function is called when a new locale is selected.
*/
public void selectedLocaleMenu() {
setFormatFromLocale(true);
}
/**
* This function is called when a new Date (Format) Style is selected.
*/
public void selectedDateStyleMenu() {
setFormatFromLocale(false);
}
/**
* This function is called when a new Time (Format) Style is selected.
*/
public void selectedTimeStyleMenu() {
setFormatFromLocale(false);
}
/**
* Store the current time in milliseconds.
*/
long time = System.currentTimeMillis();
/**
* This function is called when it is necessary to parse the time
* string in the "1.0 Date" text field.
*/
public void formatText() {
String leftString = inputText.getText();
if (leftString.length() == 0)
{
errorText("Error: no input to format!");
return;
}
try {
// intentional use of deprecated method
time = Date.parse(leftString);
}
catch (Exception e) {
outputText.setText("ERROR");
errorText("Exception: Date.parse: "+leftString);
return;
}
setMillisText();
millisFormat();
}
/**
* This function is called when it is necessary to parse the time
* string in the "New Date" text field.
*/
public void parseText() {
String rightString = outputText.getText();
ParsePosition status = new ParsePosition(0);
if (rightString.length() == 0)
{
errorText("Error: no input to parse!");
return;
}
try {
time = format.parse(rightString, status).getTime();
}
catch (Exception e) {
inputText.setText("ERROR");
errorText("Exception: parse: "+rightString);
return;
}
setMillisText();
millisParse();
}
/**
* This function is called when it is necessary to format the time
* in the "Millis" text field.
*/
public void millisFormat() {
String out = "";
try {
out = format.format(new Date(time));
}
catch (Exception e) {
outputText.setText("ERROR");
errorText("Exception: format: "+time);
return;
}
outputText.setText( out );
errorText("Formatted...");
}
/**
* This function is called when it is necessary to display the time
* value parsed using GMT string in the "1.0 Date" text field.
*/
public void millisParse() {
String input = "";
try {
// intentional use of deprecated method
input = new Date(time).toGMTString();
}
catch (Exception e) {
inputText.setText("ERROR");
errorText("Exception: in toGMTString: "+time);
return;
}
inputText.setText( input );
errorText("Parsed...");
}
/**
* This function is called when the time value in the "Millis" text field
* is changed. The new time value will be formatted and displayed in both
* "New Date" and "1.0 Date" text fields.
*/
public void millisChanged() {
String millisString = millisText.getText();
try {
time = Long.parseLong(millisString);
}
catch (Exception e) {
errorText("Exception: Bad value for millis. Must be Long");
return;
}
millisFormat();
millisParse();
errorText("Millis changed...");
}
/**
* This function is called when it is necessary to display the time
* value in milliseconds in the "Millis" text field.
*/
public void setMillisText() {
millisText.setText(Long.toString(time));
}
/**
* This function is called when users change the pattern text.
*/
public void patternTextChanged() {
setFormatFromPattern();
}
/**
* This function is called when users select a new representative city.
*/
public void cityChanged() {
int index = cityMenu.getSelectedIndex();
SimpleTimeZone timeZone = new SimpleTimeZone(kZoneOffsets[index],
kZoneIDs[index]);
timeZone.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY,
2 * millisPerHour);
timeZone.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
2 * millisPerHour);
format.setTimeZone(timeZone);
millisFormat();
millisParse();
}
private boolean addMode() {
return add;
}
/**
* This function is called when users select a new time field
* to add or roll its value.
*/
public void dateFieldChanged(boolean up) {
String d = dateMenu.getSelectedItem();
byte field = 0;
if (d.equals("Year")) {
field = (byte) Calendar.YEAR;
} else
if (d.equals("Month")) {
field = (byte) Calendar.MONTH;
} else
if (d.equals("Day of Month")) {
field = (byte) Calendar.DATE;
} else
if (d.equals("Hour of Day")) {
field = (byte) Calendar.HOUR_OF_DAY;
} else
if (d.equals("Minute")) {
field = (byte) Calendar.MINUTE;
} else
if (d.equals("Second")) {
field = (byte) Calendar.SECOND;
} else
if (d.equals("Millisecond")) {
field = (byte) Calendar.MILLISECOND;
}
format.getCalendar().setTime(new Date(time));
// format.getCalendar().computeFields();
if (up) {
if (addMode()) {
format.getCalendar().add(field, 1);
} else {
format.getCalendar().roll(field, true);
}
} else {
if (addMode()) {
format.getCalendar().add(field, -1);
} else {
format.getCalendar().roll(field, false);
}
}
time = format.getCalendar().getTime().getTime();
setMillisText();
millisFormat();
millisParse();
}
/**
* Print out the error message while debugging this program.
*/
public void errorText(String s)
{
if (DEBUG)
{
System.out.println(s);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?