Java日期控件代码 jsp 日期控件

Java 如何用控件显示时间

取得时间用

创新互联欢迎来电:18980820575,为您提供成都网站建设网页设计及定制高端网站建设服务,创新互联网页制作领域10余年,包括木制凉亭等多个方面拥有丰富设计经验,选择创新互联,为网站锦上添花!

java.util.Calendar或者java.util.Date

在控件上控制时间用

javax.swing.Timer

下面给个例子:

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());

}

}

java中的日期时间如何选择控件

swingx你可以到maven库里面搜索Jar包(google "maven repos",出现很多maven库,随便找一个,搜索swingx,找最新的下载。如果你使用maven管理项目,那更好办了,把swingx放入依赖里面即可)。

JXDatepicker的使用方法:

// JXDatePicker提供了编辑器供用户直接输入日期,也提供了右侧按钮,点击按钮出现日期选择期供用户选择日期。

JXDatePicker datePicker = new JXDatePicker();

JXDatePicker datePicker1 = new JXDatePicker(new java.util.Date());

java.util.Date date = datePicker.getDate(); // 用户输入或选择日期后得到日期值

java.util.Date date1 = datePicker1.getDate(); // 用户输入或选择日期后得到日期值

//当然,你也可以指定用户输入日期的格式。

JXDatePicker datePicker2 = new JXDatePicker();

datePicker2.setFormats("yyyy.M.d", "yyyy-M-d");//可同时设置多种格式。setFormats(String...format)

JXMonthView的使用方法:

JXMonthView monthView = new JXMonthView();

monthView.setUnselectableDates(new java.util.Date());// 设置多个不可选择的日期,setUnselectableDates(java.util.Date ... date)

Setjava.util.Date unselectable = monthView.getSelectionModel().getUnselectableDates();// 获得当前有哪些不可选择日期

monthView.setFlaggedDates(date1,date2)// 设置多个被标记了的日期

Setjava.util.Date flaggedDates = monthView.getFlaggedDates();// 获得被标记的日期

Calendar calendar = monthView.getCalendar();// 获得日历对象

还有一些可set,get的属性罗列如下:

selectionMode

traversable

firstDayOfWeek

还有更多用法,看API吧。如果使用Maven,你可以自动下载javadoc和javasource。

在JAVA中怎样编写代码使文本框要以日期格式输入(YYYY-MM-DD)?

String str = String.format("%tD", new Data());

这个只能用于格式化一个日期。

如果你要限制输入日期满足给定的格式,我觉得至少有两种解决方案:

1,使用日期控件

2,采用正则表达式判断输入的值是否满足给定的格式。

JAVA日历代码,怎么做?

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:在原有基础上修改了以下内容:

* 1. 将容器由Frame改为了Dialog,以便在基于对话框的程序中也能够使用

* 2. 将最小日期由1980改为了1950,考虑到目前球员的出生日期可能早于1980年

* 3. 将初始显示格式设置为 yyyy年MM月dd日 格式,原有的小时去掉了,不适合于出生日期字段

*/

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; // 默认【最小】显示年份

int lastYear = 2050; // 默认【最大】显示年份

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; // 控制条标签文字色

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("时");

hourLabel.setForeground(controlTextColor);

result.add(hourLabel);

return result;

}

private JPanel createWeekAndDayPanal() {

String colname[] = { "日", "一", "二", "三", "四", "五", "六" };

JPanel result = new JPanel();

// 设置固定字体,以免调用环境改变影响界面美观

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, "日期时间选择", 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());

}

}

}

这是一个专门的选日期的类 ,你看看完了调用就行了


文章名称:Java日期控件代码 jsp 日期控件
URL地址:http://ybzwz.com/article/dodocoo.html