定时任务总结

Timer定时器类

,定时器,功能是在指定的时间间隔内反复触发指定窗口的定时器事件。

TimerTask定时任务类

Timer是一种线程设施,用于安排以后在后台线程中执行的任务。可安排任务执行一次,或者定期重复执行,可以看成一个定时器,可以调度TimerTask。TimerTask是一个抽象类,实现了Runnable接口,所以具备了多线程的能力。

调用Date()函数或使用calendar类返回当前系统日期。

Calendar 与 Date 的转换非常简单:

Calendar calendar = Calendar.getInstance();
// 从一个 Calendar 对象中获取 Date 对象
Date date = calendar.getTime();
// 将 Date 对象反应到一个 Calendar 对象中,
// Calendar/GregorianCalendar 没有构造函数可以接受 Date 对象
// 所以我们必需先获得一个实例,然后设置 Date 对象
calendar.setTime(date);

要想格式化日期,可以使用SimpleDateFormat设置格式。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


public class AlarmClockUI extends JFrame {
	  
       public void initUI(){
    	   this.setTitle("太子叫你起床床");
    	 //this.setLocationRelativeTo(null);
    	   this.setSize(400, 500);
    	   this.setDefaultCloseOperation(3);
    	   this.setResizable(true);
    	   this.setLayout(new FlowLayout());
    	   this.setBackground(new Color(0,0,0));
    	   
    	   SimpleDateFormat sdf = new SimpleDateFormat("YYYY年MM月D日 a hh:mm:ss");//设置显示时间的格式
   		  Date date = new Date();//获取当前时间
   		  String str1 = sdf.format(date);//以字符串存储
   		 
   		   JLabel imgLabel = new JLabel("当前时间为");//把图片添加到标签上
   		   imgLabel.setForeground(new Color(128,0,128));//设置标签颜色
   		   imgLabel.setFont(new Font("楷体",Font.PLAIN,15));//设置标签字体
    	   this.add(imgLabel);//把标签添加到窗体上
    	   
    	   JTextField jtf=new JTextField(18);//创建文本框显示当前时间
    	   jtf.setForeground(new Color(255,204,204));
    	   this.add(jtf);
    	   
    	   CurrentTimeTask ctt=new CurrentTimeTask();
    	   ctt.getJTF(jtf);//传递文本框
    	   
    	   
    	   ImageIcon bgk = new ImageIcon("image/666.jpg");//设置背景图片
   		   JLabel imgLabel66 = new JLabel(bgk);//把图片添加到标签上
   		   this.add(imgLabel66);//把标签加到窗体上
//    	   我还是把这几张违和感图片注解掉好了
//    	   ImageIcon ima1 = new ImageIcon("image/1.png");//设置背景图片
//   		   JLabel imgLabel1 = new JLabel(ima1);//把图片添加到标签上
//   		   this.add(imgLabel1);//把标签加到窗体上
//   		   
//   		   ImageIcon ima2 = new ImageIcon("image/2.png");//设置背景图片
//		   JLabel imgLabel2 = new JLabel(ima2);//把图片添加到标签上
//		   this.add(imgLabel2);//把标签加到窗体上
//		   
//		   ImageIcon ima3 = new ImageIcon("image/3.png");//设置背景图片
//   		   JLabel imgLabel3 = new JLabel(ima3);//把图片添加到标签上
//   		   this.add(imgLabel3);//把标签加到窗体上
   		   
   		   JLabel jlh=new JLabel("你想在几点几分被太子叫醒?");
   		   jlh.setForeground(new Color(128,0,128));
   		   jlh.setFont(new Font("楷体",Font.PLAIN,15));
   		   this.add(jlh);
   		  
   		   
   		   JComboBox<String> jcbh=new JComboBox<String>();//创建设置时的下拉框
   		   jcbh.setPreferredSize(new Dimension(40,20));
   		   for(int i=0;i<24;i++){
   			   jcbh.addItem(i+"");
   		   }
   		   this.add(jcbh);
   		   
   		   //JLabel jlm=new JLabel("你想在");
		   //this.add(jlm);
		   
   		   
   		   JComboBox<String> jcbm=new JComboBox<String>();//创建设置分的下拉框
   		   jcbm.setPreferredSize(new Dimension(40,20));
   		   for(int i=0;i<60;i++){
			   jcbm.addItem(i+"");
		   }
		   this.add(jcbm);
		   
		  // JLabel jls=new JLabel("你想在");
		  // this.add(jls);
		   
		   JComboBox<String> jcbs=new JComboBox<String>();//创建显示秒的下拉框
		   jcbs.setPreferredSize(new Dimension(40,20));
		   for(int i=0;i<60;i++){
   			   jcbs.addItem(i+"");
   		   }
   		   this.add(jcbs);
   		   
   		   ButtonGroup bg=new ButtonGroup();//创建按钮组
   		   
   		   JRadioButton jrb1=new JRadioButton("单次");
   		   
   		   JRadioButton jrb2=new JRadioButton("每天");
   		    jrb1.setSelected(true);
   		    
   		   bg.add(jrb1);
   		   bg.add(jrb2);
   		   
   		    this.add(jcbh);
   		    this.add(jcbm);
   		    this.add(jcbs);
   		 this.add(jrb1);
   		this.add(jrb2);
   		    
   		   JButton jbs=new JButton("开启");//创建按钮
   		 //  JButton jbc=new JButton("关闭");
   		   this.add(jbs);
   		//   this.add(jbc);感觉不需要这个按钮啊。。
   		   
   		  
   		   
   				   
   		   ClockListener l=new ClockListener(jcbh, jcbm, jcbs, jrb1, jrb2);
   		   jbs.addActionListener(l);
   		 //  jbc.addActionListener(l);
   		    
    	   this.setVisible(true);
    	   Timer time=new Timer();//创建定时器对象
          
           time.schedule(ctt, 100, 1000);//每隔1000毫秒也就是1秒刷新一次
    	   
       }
	
	public static void main(String[] args) {
		AlarmClockUI ac=new AlarmClockUI();
		ac.initUI();
        
	}

}

 

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.util.Calendar;
import java.util.Timer;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;


public class ClockListener implements ActionListener{
	private JComboBox<String> jcbh,jcbm,jcbs;
	private JRadioButton jrb1;
	 private Timer time=new Timer();  
	 private long l;//设置时间和现在时间的时间差
	 private AudioClip music;
	
	public ClockListener( JComboBox<String> jcbh,JComboBox<String> jcbm,JComboBox<String> jcbs,JRadioButton jrb1,JRadioButton jrb2){
		this.jcbh=jcbh;
		this.jcbm=jcbm;
		this.jcbs=jcbs;
		this.jrb1=jrb1;
			}

	
	public void actionPerformed(ActionEvent e) {
		
		JButton jb=(JButton)e.getSource();
		if(e.getActionCommand()=="开启"){
			jb.setText("关闭");
			//把用户设置的时间转化为整数
			int hour=Integer.parseInt(jcbh.getSelectedItem().toString());
			int minute=Integer.parseInt(jcbm.getSelectedItem().toString());
			int second=Integer.parseInt(jcbs.getSelectedItem().toString());
			//用户设置时间
			int stime=hour*3600+minute*60+second;
		//	System.out.println("用户设置的时间为"+hour+"h"+minute+"m"+second+"s");
			//获取当前时间
            Calendar c = Calendar.getInstance();
			
			int h = c.get(Calendar.HOUR_OF_DAY);
			int m=c.get(Calendar.MINUTE);
			int s=c.get(Calendar.SECOND);
			
			//System.out.println("当前为"+h+"h"+m+"m"+s+"s");
			
			int ctime=h*3600+m*60+s;
			if(ctime<stime){//计算时间差
				 l=stime-ctime;
				 
			}
			else{
				l=24*3600-ctime+stime;
			}
			//判断是选的单次还是每周
			
            boolean flag=false;
          
            
			if(jrb1.isSelected()==true){//单次
				flag=true;
				CurrentTimeTask ctt=new CurrentTimeTask();
				time.schedule(ctt, l*1000);
				System.out.println("到时间了!起床了!");
				if(music!=null){try {
				      music=Applet.newAudioClip(new File("image/haha.wav.wav").toURL()); //播放音乐
				      music.loop();  //我设置的是循环播放..这样不起床都不行..
				     } catch (MalformedURLException ex) {
				      ex.printStackTrace();
				     }}
			}
			
		   else{//每天
			   CurrentTimeTask ctt=new CurrentTimeTask();
			   time.schedule(ctt, l*1000, 24*3600);
			   System.out.println("到时间了!起床了!");
			   if(music!=null){try {
				      music=Applet.newAudioClip(new File("image/haha.wav.wav").toURL()); 
				      music.loop();  
				     } catch (MalformedURLException ex) {
				      ex.printStackTrace();
				     }
		   }

	}}
	
else{

jb.setText("开启");
CurrentTimeTask ctt=new CurrentTimeTask();
ctt.cancel();//取消任务
time.purge();
}
	}
}
	

 

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;

import javax.swing.JTextField;


public class CurrentTimeTask extends TimerTask{
	private JTextField jtf;
	
	
	public  void getJTF(JTextField jtf){
		this.jtf=jtf;
	}
	public void run() {
		 SimpleDateFormat sdf = new SimpleDateFormat("YYYY年MM月D日 a hh:mm:ss");
  		  Date date = new Date();
  		  String str = sdf.format(date);
  		  jtf.setText(str);
		try{
			Thread.sleep(1000);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
	}

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值