日期选择的java代码,日期选择的java代码是什么
java中的日期时间如何选择控件
swingx你可以到maven库里面搜索Jar包(google "maven repos",出现很多maven库,随便找一个,搜索swingx,找最新的下载。如果你使用maven管理项目,那更好办了,把swingx放入依赖里面即可)。
创新互联公司是一家专业提供安宁企业网站建设,专注与成都做网站、网站制作、成都外贸网站建设、H5开发、小程序制作等业务。10年已为安宁众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。
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日历代码,怎么做?
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实现一个时间选择
script
var currentDate = new Date();
if(currentDate.getHour=10||currentDate.getHour=22){
document.forms[0].submit();//提交表单
} else{
alert("请在10点至22点之间提交数据");
return;
}
/script
求Java代码 找到将来的日期
对于对着课本自学的新手而言,此时还没有接触到数组,所以,我给出利用switch嵌套的代码如下,结果是一样的,
package demo2_2;
import java.util.Scanner;
public class YuCe_2 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("输入数字: ");
int iTodays = sc.nextInt();
System.out.println("输入另一个数字: ");
int iElap = sc.nextInt();
int iNextDay = (iElap + iTodays) % 7;
switch (iNextDay) {
case 0:
switch(iTodays) {
case 0: System.out.println("今天周日" + iElap + " 天后周日");break;
case 1: System.out.println("今天周一" + iElap + " 天后周日");break;
case 2: System.out.println("今天周二" + iElap + " 天后周日");break;
case 3: System.out.println("今天周三" + iElap + " 天后周日");break;
case 4: System.out.println("今天周四" + iElap + " 天后周日");break;
case 5: System.out.println("今天周五" + iElap + " 天后周日");break;
case 6: System.out.println("今天周六" + iElap + " 天后周日");break;
}
break;
case 1:
switch(iTodays) {
case 0: System.out.println("今天周日" + iElap + " 天后周一");break;
case 1: System.out.println("今天周一" + iElap + " 天后周一");break;
case 2: System.out.println("今天周二" + iElap + " 天后周一");break;
case 3: System.out.println("今天周三" + iElap + " 天后周一");break;
case 4: System.out.println("今天周四" + iElap + " 天后周一");break;
case 5: System.out.println("今天周五" + iElap + " 天后周一");break;
case 6: System.out.println("今天周六" + iElap + " 天后周一");break;
}
break;
case 2:
switch(iTodays) {
case 0: System.out.println("今天周日" + iElap + " 天后周二");break;
case 1: System.out.println("今天周一" + iElap + " 天后周二");break;
case 2: System.out.println("今天周二" + iElap + " 天后周二");break;
case 3: System.out.println("今天周三" + iElap + " 天后周二");break;
case 4: System.out.println("今天周四" + iElap + " 天后周二");break;
case 5: System.out.println("今天周五" + iElap + " 天后周二");break;
case 6: System.out.println("今天周六" + iElap + " 天后周二");break;
}
break;
case 3:
switch(iTodays) {
case 0: System.out.println("今天周日" + iElap + " 天后周三");break;
case 1: System.out.println("今天周一" + iElap + " 天后周三");break;
case 2: System.out.println("今天周二" + iElap + " 天后周三");break;
case 3: System.out.println("今天周三" + iElap + " 天后周三");break;
case 4: System.out.println("今天周四" + iElap + " 天后周三");break;
case 5: System.out.println("今天周五" + iElap + " 天后周三");break;
case 6: System.out.println("今天周六" + iElap + " 天后周三");break;
}
break;
case 4:
switch(iTodays) {
case 0: System.out.println("今天周日" + iElap + " 天后周四");break;
case 1: System.out.println("今天周一" + iElap + " 天后周四");break;
case 2: System.out.println("今天周二" + iElap + " 天后周四");break;
case 3: System.out.println("今天周三" + iElap + " 天后周四");break;
case 4: System.out.println("今天周四" + iElap + " 天后周四");break;
case 5: System.out.println("今天周五" + iElap + " 天后周四");break;
case 6: System.out.println("今天周六" + iElap + " 天后周四");break;
}
break;
case 5:
switch(iTodays) {
case 0: System.out.println("今天周日" + iElap + " 天后周五");break;
case 1: System.out.println("今天周一" + iElap + " 天后周五");break;
case 2: System.out.println("今天周二" + iElap + " 天后周五");break;
case 3: System.out.println("今天周三" + iElap + " 天后周五");break;
case 4: System.out.println("今天周四" + iElap + " 天后周五");break;
case 5: System.out.println("今天周五" + iElap + " 天后周五");break;
case 6: System.out.println("今天周六" + iElap + " 天后周五");break;
}
break;
case 6:
switch(iTodays) {
case 0: System.out.println("今天周日" + iElap + " 天后周六");break;
case 1: System.out.println("今天周一" + iElap + " 天后周六");break;
case 2: System.out.println("今天周二" + iElap + " 天后周六");break;
case 3: System.out.println("今天周三" + iElap + " 天后周六");break;
case 4: System.out.println("今天周四" + iElap + " 天后周六");break;
case 5: System.out.println("今天周五" + iElap + " 天后周六");break;
case 6: System.out.println("今天周六" + iElap + " 天后周六");break;
}
break;
}
}
}
编写日期类java程序
public class Dath {
public int year;
public int month,month2;
public int date;
boolean flag=false;
int sumdays;
public Dath(int year,int month,int date){
if(year0year10000){
this.year=year;
this.month=month;
this.date=date;
if(year%400==0||year%4==0year%100!=0){ //判断是不是闰年
flag=true;month2=29;
}
else{
flag=false;month2=28;
}
if(month==1|month==3|month==5|month==7|month==8|month==10|month==12){
if(1date|date31)
System.out.println(" 日期错误,请重新输入。");
}else if(month==4|month==6|month==9|month==11){
if(1date|date30)
System.out.println(" 日期错误,请重新输入。");
}else if(month==2){
if(flag=true){
if(date29|date1)
System.out.println(" 日期错误,请重新输入。");
}else{
if(date28|date1)
System.out.println("日期错误,请重新输入。");
}
}else{
System.out.println(" 月份错误,请重新输入。");
}
switch(month){
case 1:sumdays=date+31;break;
case 2:sumdays=date+month2+31;break;
case 3:sumdays=date+month2+62;break;
case 4:sumdays=date+month2+92;break;
case 5:sumdays=date+month2+123;break;
case 6:sumdays=date+month2+153;break;
case 7:sumdays=date+month2+184;break;
case 8:sumdays=date+month2+215;break;
case 9:sumdays=date+month2+245;break;
case 10:sumdays=date+month2+276;break;
case 11:sumdays=date+month2+306;break;
case 12:sumdays=date+month2+337;break;
}
System.out.println("今天是"+year+"年的第"+sumdays+"天~");
}else{
System.out.println("年份错误,请重新输入。");
}
}
public static void main(String[] args) {
new Dath(78,13,100);
}
}
关于java的日期的小程序 求代码 不会
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Demo6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个日期:");
String d1 = sc.nextLine();
while (!isFormat(d1)) {
System.out.println("日期格式不正确,请重新输入:");
d1 = sc.nextLine();
}
System.out.println("请输入第二个日期");
String d2 = sc.nextLine();
while (!isFormat(d2)) {
System.out.println("日期格式不正确,请重新输入:");
d2 = sc.nextLine();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date1 = sdf.parse(d1);
Date date2 = sdf.parse(d2);
System.out.println((date1.getTime() - date2.getTime()) / 24 / 1000 / 60 / 60);
} catch (ParseException e) {
e.printStackTrace();
}
}
public static boolean isFormat(String date) {
if (date == null || date.trim().length() = 0) {
return false;
}
String pat = "^[0-9]{4}[-]([0][0-9]|[1][0,1,2])[-]([0,1,2][0-9]|[3][0,1])$";
Pattern p = Pattern.compile(pat);
Matcher m = p.matcher(date);
return m.matches();
}
}
文章标题:日期选择的java代码,日期选择的java代码是什么
文章转载:http://hbruida.cn/article/dsgdjee.html