【Java GUI 日期时间选择工具】

目录

  • java GUI 日期时间选择工具
    • 效果图
    • 直接上代码

java GUI 日期时间选择工具

这是一个用于java图形化界面的日期时间选择工具,仅适用于学习使用,不适合实际开发。

里面有些内容没用,但是没有删除,可以根据自己需求拓展
1.可以只展示日期,可以只展示时间,可以同时显示
2.TimeChangeUtils类是一个JLabel标签,直接展示时间
3.MyDate类是日期相关类,MyTime是时间相关类

效果图

TimeChangeUtils
TimeChangeUtils效果图
点击点击设置时间
【Java GUI 日期时间选择工具】_第1张图片
点击确认
确认效果
只有日期模式
日期模式

只有时间模式
时间模式

直接上代码


import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TimeChangeUtils extends JLabel{
	//年月日时分秒的下拉列表
	private JComboBox<Integer> yearBox,monthBox,dayBox,hourBox,minuteBox,secondBox;
	//年月日时分秒标签
	private JLabel yearJLabel,monthJLabel,dayJLabel,hourJLabel,minuteJLabel,secondJLabel;
	//操作按钮
	JButton querenButton,quxiaoButton;
	//时间字符串
	private String returnString;
	//显示容器
	private JDialog jDialog;
	//获取当前时间
	private MyDate nowDate;
	
	private Date returnDate;
	//默认时间构造方式
	private String string="yyyy-MM-dd HH:mm:ss";
	
	private MyDate date;
	private MyTime time;
	
	public static final int JUST_DATE=0,JUST_TIME=1,ALL=2;
	
	
	public TimeChangeUtils(int type) {
		super();
		news(type);
		
	}
	
	private void news(int type) {
		jDialog=new JDialog();
		jDialog.setBounds(this.getX(), this.getY(), 250, 150);
		switch (type) {
		case JUST_DATE:{
			jDialog.setLayout(new GridLayout(2,1));
			date=new MyDate();
			jDialog.add(date);
			break;
		}
		case JUST_TIME:{
			jDialog.setLayout(new GridLayout(2,1));
			time=new MyTime();
			jDialog.add(time);
			break;
		}
		case ALL:{
			jDialog.setLayout(new GridLayout(3,1));
			date=new MyDate();
			time=new MyTime();
			jDialog.add(date);
			jDialog.add(time);
			break;
		}

		default:{
			System.out.println("类型错误!");
			break;
		}
		}
		
		
		JPanel j=new JPanel();
		j.setLayout(new GridLayout(1,2));
		querenButton=new JButton("确认");
		quxiaoButton=new JButton("取消");
		j.add(quxiaoButton);
		j.add(querenButton);
		jDialog.add(j);
		
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				super.mouseClicked(e);
				jDialog.setVisible(true);
			}
		});
		querenButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				setText(getDateTime());
				jDialog.setVisible(false);
			}
		});
		quxiaoButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				jDialog.setVisible(false);
			}
		});
	}
	
	
	public void setText(Date date) {
		String dd="",tt="",ands;
		if(this.date!=null) {
			this.date.setDate(date);
			dd="yyyy-MM-dd";
		}
		if(this.time!=null) {
			this.time.setTime(date);
			tt="HH:mm:ss";
		}
		if(dd!=""&&tt!="") {
			ands=dd+" "+tt;
		}else {
			ands=dd+tt;
		}
		System.out.println("标签显示格式: "+ands);
		System.out.println("显示结果: "+getText(ands));
		String string=getText(ands);
		setText(string);
	}
	
	public void setText(String string,String geshi) {
		try {
			setText(new SimpleDateFormat(geshi).parse(string));
		} catch (ParseException e) {
			e.printStackTrace();
			try {
				setText(new SimpleDateFormat(this.string).parse(string));
			} catch (ParseException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	public Date getDateTime() {
		
		String d = "",t="",ands;
		String geshi,dd="",tt="";
		if(date!=null) {
			d=date.getDate("yyyyMMdd");
			dd="yyyyMMdd";
		}
		if(time!=null) {
			t=time.getTime("HHmmss");
			tt="HHmmss";
		}
			ands=d+t;
			geshi=dd+tt;
		
			
		System.out.println("格式 :"+geshi+"时间 :"+ands);
		try {
			Date ddd=new SimpleDateFormat(geshi).parse(ands);
			return  ddd;
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		
	}
	
	public String getText(String geshi) {
		System.out.println("时间为:"+new SimpleDateFormat(geshi).format(getDateTime()));
		return new SimpleDateFormat(geshi).format(getDateTime());
	}
	

	//日期选择工具类
	class MyDate extends JPanel{
		//默认起始年份
		int startYear=2010;
		//默认年数
		int yearLenght=30;
		
		public MyDate() {
			//初始化组件和数据
			news();
			//赋值
			setDayItem();
			//监听
			listener();
		}
		
		/**
		 * 指定起始年份和年份长度的构造方法
		 * @param year 起始年份
		 * @param yearLenght 长度
		 */
		public MyDate(int year,int yearLenght) {
			startYear=year;
			this.yearLenght=yearLenght;
			//初始化组件和数据
			news();
			//赋值
			setDayItem();
			//监听
			listener();
		}
		/**
		 * 获取字符串类型当前显示时间
		 * @param string 转换格式
		 * @return
		 */
		public String getDate(String string) {
			Date date=getDate();
			if(date==null) {
				return null;
			}
			return new SimpleDateFormat(string).format(date);
		}
		
		/**
		 * 返回Date类型时间
		 * @return
		 */
		public Date getDate() {
			try {
				String dString=yearBox.getSelectedItem()+"-"
						+monthBox.getSelectedItem()+"-"+dayBox.getSelectedItem();
			Date date=new SimpleDateFormat("yyyy-MM-dd").parse(dString);
					return date;
			} catch (Exception e) {
				return null;
			}
			
		}
		
		/**
		 * 
		 * @param time 格式为yyyy_MM_dd "_"可变
		 * @param geshi 连接字符的样式
		 */
		public void setDate(String time,String geshi) {
			System.out.println(time);
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy"+geshi+"MM"+geshi+"dd");
			try {
				setDate(simpleDateFormat.parse(time));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			
		}
		
		/**
		 * 
		 * @param date
		 */
		public void setDate(Date date) {
			int y,m,d;
			Calendar calendar=Calendar.getInstance();
			calendar.setTime(date);
			y=calendar.get(Calendar.YEAR);
			m=calendar.get(Calendar.MONTH)+1;
			d=calendar.get(Calendar.DAY_OF_MONTH);
			//起始年份大于现有时间值
			if(startYear>y) {
				System.out.println("起始年份大于现有年份!");
			}else if (startYear+yearLenght<y) {
				System.out.println("最大年份小于现有年份!");
			} else {
				yearBox.setSelectedItem(y);
				monthBox.setSelectedItem(m);
				//此时要刷新一遍日期列的数据
				setDayItem();
				dayBox.setSelectedItem(d);
				System.out.println("y:"+y+" m:"+m+" d:"+d);
			}
			
		}	
		
		/**
		 * 年月日参数添加日期
		 * @param yyyy
		 * @param mm
		 * @param dd
		 */
		public void setDate(int yyyy,int mm,int dd) {
			String string=yyyy+"-"+mm+"-"+dd;
			try {
				Date date=new SimpleDateFormat("yyyy-MM-dd").parse(string);
				setDate(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		
		
		private void setDayItem() {
			int y=(int) yearBox.getSelectedItem();
			int m=(int) monthBox.getSelectedItem();
			//获取当前天数的数量
			int l=dayBox.getItemCount();
			System.out.println("当前天数为:"+l);
			//大月
			if(m==1||m==3||m==5||m==7||m==8||m==10||m==12) {
				//根据情况添加
				switch (l) {
				case 28:{
					dayBox.addItem(29);
				}
				case 29:{
					dayBox.addItem(30);
				}
				case 30:{
					dayBox.addItem(31);
					break;
				}
				case 31:{
					System.out.println("天数不变");
					break;
				}
				default:
					System.out.println("天数错误!");
					break;
				}
				//小月
			}else if(m==4||m==6||m==9||m==11) {
				//根据情况添加
				switch (l) {
				case 28:{
					dayBox.addItem(29);
				}
				case 29:{
					dayBox.addItem(30);
					break;
				}
				case 30:{
					System.out.println("天数不变");
					break;
				}
				case 31:{
					dayBox.removeItemAt(30);
					break;
				}
				default:
					System.out.println("天数错误!");
					break;
				}
				//二月要分是否闰月
			}else {
				//闰年29天
				if(y%4==0) {
					//根据情况添加
					switch (l) {
					case 31:{
						System.out.println(dayBox.getItemCount()-1);
						dayBox.removeItemAt(30);
					}
					case 30:{
						dayBox.removeItemAt(29);
					}
					case 29:{
						System.out.println("天数不变");
						break;
					}
					case 28:{
						dayBox.addItem(29);
						break;
					}
					default:
						System.out.println("天数错误!");
						break;
					}
				//平年28天
				}else {
					//根据情况添加
					switch (l) {
					case 31:{
						dayBox.removeItemAt(30);
					}
					case 30:{
						dayBox.removeItemAt(29);
					}
					case 29:{
						dayBox.removeItemAt(28);
					}
					case 28:{
						System.out.println("天数不变");
						break;
					}
					default:
						System.out.println("天数错误!");
						break;
					}
				}
			}
			
		}

		/**
		 * 年月日内容改变的监听,因为大小月和年份的不同,每月的天数也有所区别
		 */
		private void listener() {
			ActionListener a=new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					setDayItem();
				}
			};
			yearBox.addActionListener(a);
			monthBox.addActionListener(a);
			
		}
		/**
		 * 初始化年月日的组件
		 */
		private void news() {
			//设置三等分网格布局
			setLayout(new GridLayout(1,3));
			JPanel jPanelL,jPanelC,jPanelR;
			jPanelL=new JPanel();
			jPanelC=new JPanel();
			jPanelR=new JPanel();
			jPanelL.setLayout(new BorderLayout());
			jPanelC.setLayout(new BorderLayout());
			jPanelR.setLayout(new BorderLayout());
			add(jPanelL);
			add(jPanelC);
			add(jPanelR);
			yearBox=new JComboBox<Integer>();
			monthBox=new JComboBox<Integer>();
			dayBox=new JComboBox<Integer>();
			yearJLabel=new JLabel("年",JLabel.LEFT);
			monthJLabel=new JLabel("月",JLabel.LEFT);
			dayJLabel=new JLabel("日",JLabel.LEFT);
			
			jPanelL.add(yearBox,BorderLayout.CENTER);
			jPanelL.add(yearJLabel,BorderLayout.EAST);
			jPanelC.add(monthBox,BorderLayout.CENTER);
			jPanelC.add(monthJLabel,BorderLayout.EAST);
			jPanelR.add(dayBox,BorderLayout.CENTER);
			jPanelR.add(dayJLabel,BorderLayout.EAST);
			newshuju();
		}
		
		/**
		 * 初始化年月日的集合的值
		 */
		private void newshuju() {
			//年赋值
			for(int i=0;i<=yearLenght;i++) {
				yearBox.addItem(startYear+i);
			}
			//月赋值
			for(int i=1;i<13;i++) {
				monthBox.addItem(i);
			}
			//日赋值
			for(int i=1;i<29;i++) {
				dayBox.addItem(i);
			}
		}
	}
	
	
	//时间选择工具类
	class MyTime extends JPanel{
		
		public MyTime() {
			//初始化组件
			news();
		}

		private void news(){
			//设置三等分网格布局
			setLayout(new GridLayout(1,3));
			JPanel jPanelL,jPanelC,jPanelR;
			jPanelL=new JPanel();
			jPanelC=new JPanel();
			jPanelR=new JPanel();
			jPanelL.setLayout(new BorderLayout());
			jPanelC.setLayout(new BorderLayout());
			jPanelR.setLayout(new BorderLayout());
			add(jPanelL);
			add(jPanelC);
			add(jPanelR);
			hourBox=new JComboBox<Integer>();
			minuteBox=new JComboBox<Integer>();
			secondBox=new JComboBox<Integer>();
			hourJLabel=new JLabel("时",JLabel.LEFT);
			minuteJLabel=new JLabel("分",JLabel.LEFT);
			secondJLabel=new JLabel("秒",JLabel.LEFT);
			
			jPanelL.add(hourBox,BorderLayout.CENTER);
			jPanelL.add(hourJLabel,BorderLayout.EAST);
			jPanelC.add(minuteBox,BorderLayout.CENTER);
			jPanelC.add(minuteJLabel,BorderLayout.EAST);
			jPanelR.add(secondBox,BorderLayout.CENTER);
			jPanelR.add(secondJLabel,BorderLayout.EAST);
			
			for(int i=0;i<24;i++) {
				hourBox.addItem(i);
			}
			for(int i=0;i<60;i++) {
				minuteBox.addItem(i);
				secondBox.addItem(i);
			}
		}
		
		public String getTime(String geshi) {
			Date date=new Date();
			Calendar calendar=Calendar.getInstance();
			calendar.setTime(date);
			int h,m,s;
			h=(int) hourBox.getSelectedItem();
			m=(int) minuteBox.getSelectedItem();
			s=(int) secondBox.getSelectedItem();
			calendar.set(0, 0, 0, h, m, s);
			date.setTime(calendar.getTimeInMillis());
			try {
				return new SimpleDateFormat(geshi).format(date);
			} catch (Exception e) {
				return null;
			}
			
		}

		public void setTime(Date date) {
			Calendar calendar=Calendar.getInstance();
			calendar.setTime(date);
			hourBox.setSelectedItem(calendar.get(Calendar.HOUR_OF_DAY));
			minuteBox.setSelectedItem(calendar.get(Calendar.MINUTE));
			secondBox.setSelectedItem(calendar.get(Calendar.SECOND));
		}
		
	}
	
	
	public static void main(String[] args) {
		TimeChangeUtils data=new TimeChangeUtils(TimeChangeUtils.JUST_DATE);
		data.setSize(250,150);
		data.setText(new Date());
		System.out.println("获取时间:"+data.getDateTime());
		data.setVisible(true);
		JFrame j=new JFrame();
		j.setSize(50,50);
		JPanel p=new JPanel(new BorderLayout()); 
		data.setText("点击设置时间");
		p.add(data);
		j.add(p);
		j.setVisible(true);
		
	}
}

你可能感兴趣的:(java,eclipse)