取得時(shí)間用
創(chuàng)新互聯(lián)歡迎來電:18980820575,為您提供成都網(wǎng)站建設(shè)網(wǎng)頁設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù),創(chuàng)新互聯(lián)網(wǎng)頁制作領(lǐng)域10余年,包括木制涼亭等多個(gè)方面擁有豐富設(shè)計(jì)經(jīng)驗(yàn),選擇創(chuàng)新互聯(lián),為網(wǎng)站錦上添花!
java.util.Calendar或者java.util.Date
在控件上控制時(shí)間用
javax.swing.Timer
下面給個(gè)例子:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class ClockTest extends JFrame {
public ClockTest() {
super("Timer Demo");
setSize(300, 100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
ClockLabel clock = new ClockLabel();
getContentPane().add(clock, BorderLayout.NORTH);
}
public static void main(String args[]) {
ClockTest ct = new ClockTest();
ct.setVisible(true);
}
}
class ClockLabel extends JLabel implements ActionListener {
public ClockLabel() {
super((Calendar.getInstance().getTime()).toString());
Timer t = new Timer(1000, this);
t.start();
}
public void actionPerformed(ActionEvent ae) {
setText(Calendar.getInstance().getTime().toString());
}
}
swingx你可以到maven庫里面搜索Jar包(google "maven repos",出現(xiàn)很多maven庫,隨便找一個(gè),搜索swingx,找最新的下載。如果你使用maven管理項(xiàng)目,那更好辦了,把swingx放入依賴?yán)锩婕纯桑?/p>
JXDatepicker的使用方法:
// JXDatePicker提供了編輯器供用戶直接輸入日期,也提供了右側(cè)按鈕,點(diǎn)擊按鈕出現(xiàn)日期選擇期供用戶選擇日期。
JXDatePicker datePicker = new JXDatePicker();
JXDatePicker datePicker1 = new JXDatePicker(new java.util.Date());
java.util.Date date = datePicker.getDate(); // 用戶輸入或選擇日期后得到日期值
java.util.Date date1 = datePicker1.getDate(); // 用戶輸入或選擇日期后得到日期值
//當(dāng)然,你也可以指定用戶輸入日期的格式。
JXDatePicker datePicker2 = new JXDatePicker();
datePicker2.setFormats("yyyy.M.d", "yyyy-M-d");//可同時(shí)設(shè)置多種格式。setFormats(String...format)
JXMonthView的使用方法:
JXMonthView monthView = new JXMonthView();
monthView.setUnselectableDates(new java.util.Date());// 設(shè)置多個(gè)不可選擇的日期,setUnselectableDates(java.util.Date ... date)
Setjava.util.Date unselectable = monthView.getSelectionModel().getUnselectableDates();// 獲得當(dāng)前有哪些不可選擇日期
monthView.setFlaggedDates(date1,date2)// 設(shè)置多個(gè)被標(biāo)記了的日期
Setjava.util.Date flaggedDates = monthView.getFlaggedDates();// 獲得被標(biāo)記的日期
Calendar calendar = monthView.getCalendar();// 獲得日歷對象
還有一些可set,get的屬性羅列如下:
selectionMode
traversable
firstDayOfWeek
還有更多用法,看API吧。如果使用Maven,你可以自動(dòng)下載javadoc和javasource。
String str = String.format("%tD", new Data());
這個(gè)只能用于格式化一個(gè)日期。
如果你要限制輸入日期滿足給定的格式,我覺得至少有兩種解決方案:
1,使用日期控件
2,采用正則表達(dá)式判斷輸入的值是否滿足給定的格式。
import java.util.Date;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.awt.Color;
import java.awt.Font;
import java.awt.Point;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.border.LineBorder;
/**
* @company:NEUSOFT
* @Title:日期選擇控件
* @Description:在原有基礎(chǔ)上修改了以下內(nèi)容:
* 1. 將容器由Frame改為了Dialog,以便在基于對話框的程序中也能夠使用
* 2. 將最小日期由1980改為了1950,考慮到目前球員的出生日期可能早于1980年
* 3. 將初始顯示格式設(shè)置為 yyyy年MM月dd日 格式,原有的小時(shí)去掉了,不適合于出生日期字段
*/
public class DateChooserJButton extends JButton {
private DateChooser dateChooser = null;
private String preLabel = "";
public DateChooserJButton() {
this(getNowDate());
}
public DateChooserJButton(SimpleDateFormat df, String dateString) {
this();
setText(df, dateString);
}
public DateChooserJButton(Date date) {
this("", date);
}
public DateChooserJButton(String preLabel, Date date) {
if (preLabel != null)
this.preLabel = preLabel;
setDate(date);
setBorder(null);
setCursor(new Cursor(Cursor.HAND_CURSOR));
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (dateChooser == null)
dateChooser = new DateChooser();
Point p = getLocationOnScreen();
p.y = p.y + 30;
dateChooser.showDateChooser(p);
}
});
}
private static Date getNowDate() {
return Calendar.getInstance().getTime();
}
private static SimpleDateFormat getDefaultDateFormat() {
return new SimpleDateFormat("yyyy年MM月dd日");
}
// 覆蓋父類的方法
public void setText(String s) {
Date date;
try {
date = getDefaultDateFormat().parse(s);
} catch (ParseException e) {
date = getNowDate();
}
setDate(date);
}
public void setText(SimpleDateFormat df, String s) {
Date date;
try {
date = df.parse(s);
} catch (ParseException e) {
date = getNowDate();
}
setDate(date);
}
public void setDate(Date date) {
super.setText(preLabel + getDefaultDateFormat().format(date));
}
public Date getDate() {
String dateString = this.getText().substring(preLabel.length());
try {
return getDefaultDateFormat().parse(dateString);
} catch (ParseException e) {
return getNowDate();
}
}
public String getDateString()
{
Date birth =getDate();
DateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
return formatDate.format(birth).toString();
//return this.getText().substring(preLabel.length());
}
// 覆蓋父類的方法使之無效
//public void addActionListener(ActionListener listener) {
//}
private class DateChooser extends JPanel implements ActionListener,
ChangeListener {
int startYear = 1950; // 默認(rèn)【最小】顯示年份
int lastYear = 2050; // 默認(rèn)【最大】顯示年份
int width = 200; // 界面寬度
int height = 200; // 界面高度
Color backGroundColor = Color.gray; // 底色
// 月歷表格配色----------------//
Color palletTableColor = Color.white; // 日歷表底色
Color todayBackColor = Color.orange; // 今天背景色
Color weekFontColor = Color.blue; // 星期文字色
Color dateFontColor = Color.black; // 日期文字色
Color weekendFontColor = Color.red; // 周末文字色
// 控制條配色------------------//
Color controlLineColor = Color.pink; // 控制條底色
Color controlTextColor = Color.white; // 控制條標(biāo)簽文字色
Color rbFontColor = Color.white; // RoundBox文字色
Color rbBorderColor = Color.red; // RoundBox邊框色
Color rbButtonColor = Color.pink; // RoundBox按鈕色
Color rbBtFontColor = Color.red; // RoundBox按鈕文字色
JDialog dialog;
JSpinner yearSpin;
JSpinner monthSpin;
JSpinner hourSpin;
JButton[][] daysButton = new JButton[6][7];
DateChooser() {
setLayout(new BorderLayout());
setBorder(new LineBorder(backGroundColor, 2));
setBackground(backGroundColor);
JPanel topYearAndMonth = createYearAndMonthPanal();
add(topYearAndMonth, BorderLayout.NORTH);
JPanel centerWeekAndDay = createWeekAndDayPanal();
add(centerWeekAndDay, BorderLayout.CENTER);
}
private JPanel createYearAndMonthPanal() {
Calendar c = getCalendar();
int currentYear = c.get(Calendar.YEAR);
int currentMonth = c.get(Calendar.MONTH) + 1;
int currentHour = c.get(Calendar.HOUR_OF_DAY);
JPanel result = new JPanel();
result.setLayout(new FlowLayout());
result.setBackground(controlLineColor);
yearSpin = new JSpinner(new SpinnerNumberModel(currentYear,
startYear, lastYear, 1));
yearSpin.setPreferredSize(new Dimension(48, 20));
yearSpin.setName("Year");
yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####"));
yearSpin.addChangeListener(this);
result.add(yearSpin);
JLabel yearLabel = new JLabel("年");
yearLabel.setForeground(controlTextColor);
result.add(yearLabel);
monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1,
12, 1));
monthSpin.setPreferredSize(new Dimension(35, 20));
monthSpin.setName("Month");
monthSpin.addChangeListener(this);
result.add(monthSpin);
JLabel monthLabel = new JLabel("月");
monthLabel.setForeground(controlTextColor);
result.add(monthLabel);
hourSpin = new JSpinner(new SpinnerNumberModel(currentHour, 0, 23,
1));
hourSpin.setPreferredSize(new Dimension(35, 20));
hourSpin.setName("Hour");
hourSpin.addChangeListener(this);
result.add(hourSpin);
JLabel hourLabel = new JLabel("時(shí)");
hourLabel.setForeground(controlTextColor);
result.add(hourLabel);
return result;
}
private JPanel createWeekAndDayPanal() {
String colname[] = { "日", "一", "二", "三", "四", "五", "六" };
JPanel result = new JPanel();
// 設(shè)置固定字體,以免調(diào)用環(huán)境改變影響界面美觀
result.setFont(new Font("宋體", Font.PLAIN, 12));
result.setLayout(new GridLayout(7, 7));
result.setBackground(Color.white);
JLabel cell;
for (int i = 0; i 7; i++) {
cell = new JLabel(colname[i]);
cell.setHorizontalAlignment(JLabel.RIGHT);
if (i == 0 || i == 6)
cell.setForeground(weekendFontColor);
else
cell.setForeground(weekFontColor);
result.add(cell);
}
int actionCommandId = 0;
for (int i = 0; i 6; i++)
for (int j = 0; j 7; j++) {
JButton numberButton = new JButton();
numberButton.setBorder(null);
numberButton.setHorizontalAlignment(SwingConstants.RIGHT);
numberButton.setActionCommand(String
.valueOf(actionCommandId));
numberButton.addActionListener(this);
numberButton.setBackground(palletTableColor);
numberButton.setForeground(dateFontColor);
if (j == 0 || j == 6)
numberButton.setForeground(weekendFontColor);
else
numberButton.setForeground(dateFontColor);
daysButton[i][j] = numberButton;
result.add(numberButton);
actionCommandId++;
}
return result;
}
private JDialog createDialog(JDialog owner) {
JDialog result = new JDialog(owner, "日期時(shí)間選擇", true);
result.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
result.getContentPane().add(this, BorderLayout.CENTER);
result.pack();
result.setSize(width, height);
return result;
}
void showDateChooser(Point position) {
JDialog owner = (JDialog) SwingUtilities
.getWindowAncestor(DateChooserJButton.this);
if (dialog == null || dialog.getOwner() != owner)
dialog = createDialog(owner);
dialog.setLocation(getAppropriateLocation(owner, position));
flushWeekAndDay();
dialog.setVisible(true);
}
Point getAppropriateLocation(JDialog owner, Point position) {
Point result = new Point(position);
Point p = owner.getLocation();
int offsetX = (position.x + width) - (p.x + owner.getWidth());
int offsetY = (position.y + height) - (p.y + owner.getHeight());
if (offsetX 0) {
result.x -= offsetX;
}
if (offsetY 0) {
result.y -= offsetY;
}
return result;
}
private Calendar getCalendar() {
Calendar result = Calendar.getInstance();
result.setTime(getDate());
return result;
}
private int getSelectedYear() {
return ((Integer) yearSpin.getValue()).intValue();
}
private int getSelectedMonth() {
return ((Integer) monthSpin.getValue()).intValue();
}
private int getSelectedHour() {
return ((Integer) hourSpin.getValue()).intValue();
}
private void dayColorUpdate(boolean isOldDay) {
Calendar c = getCalendar();
int day = c.get(Calendar.DAY_OF_MONTH);
c.set(Calendar.DAY_OF_MONTH, 1);
int actionCommandId = day - 2 + c.get(Calendar.DAY_OF_WEEK);
int i = actionCommandId / 7;
int j = actionCommandId % 7;
if (isOldDay)
daysButton[i][j].setForeground(dateFontColor);
else
daysButton[i][j].setForeground(todayBackColor);
}
private void flushWeekAndDay() {
Calendar c = getCalendar();
c.set(Calendar.DAY_OF_MONTH, 1);
int maxDayNo = c.getActualMaximum(Calendar.DAY_OF_MONTH);
int dayNo = 2 - c.get(Calendar.DAY_OF_WEEK);
for (int i = 0; i 6; i++) {
for (int j = 0; j 7; j++) {
String s = "";
if (dayNo = 1 dayNo = maxDayNo)
s = String.valueOf(dayNo);
daysButton[i][j].setText(s);
dayNo++;
}
}
dayColorUpdate(false);
}
public void stateChanged(ChangeEvent e) {
JSpinner source = (JSpinner) e.getSource();
Calendar c = getCalendar();
if (source.getName().equals("Hour")) {
c.set(Calendar.HOUR_OF_DAY, getSelectedHour());
setDate(c.getTime());
return;
}
dayColorUpdate(true);
if (source.getName().equals("Year"))
c.set(Calendar.YEAR, getSelectedYear());
else
// (source.getName().equals("Month"))
c.set(Calendar.MONTH, getSelectedMonth() - 1);
setDate(c.getTime());
flushWeekAndDay();
}
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource();
if (source.getText().length() == 0)
return;
dayColorUpdate(true);
source.setForeground(todayBackColor);
int newDay = Integer.parseInt(source.getText());
Calendar c = getCalendar();
c.set(Calendar.DAY_OF_MONTH, newDay);
setDate(c.getTime());
}
}
}
這是一個(gè)專門的選日期的類 ,你看看完了調(diào)用就行了
名稱欄目:Java日期控件代碼 jsp 日期控件
標(biāo)題路徑:http://jinyejixie.com/article0/dodocoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、外貿(mào)建站、網(wǎng)站設(shè)計(jì)公司、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)