当前位置: 移动技术网 > IT编程>软件设计>设计模式 > java多线程定时器和java判断一个时间是否在时间区间内和用正则表达式获取String字符串之间的数据

java多线程定时器和java判断一个时间是否在时间区间内和用正则表达式获取String字符串之间的数据

2020年04月17日  | 移动技术网IT编程  | 我要评论

多线程定时器时间段3秒执行一次

 1 timer timer = new timer();
 2         timer.schedule(new timertask() {
 3             // 在run方法中的语句就是定时任务执行时运行的语句。
 4             public void run() {
 5                 system.out.println("11232");
 6                 
 7                 try {
 8                     //调用方法
 9                     new clientaxis2testcuralldata();
10                 } catch (exception e) {
11                     // todo auto-generated catch block
12                     e.printstacktrace();
13                 }
14                 
15             }
16             // 表示在3秒之后开始执行,并且每2秒执行一次
17         }, 0, 3000);

多线程定时器每天0点执行一次

 1 public static void main(string[] args) {  
 2         new timermanager();    
 3     }  
 4   
 5     //时间间隔(一天)  
 6     private static final long period_day = 24 * 60 * 60 * 1000;  
 7     public timermanager() {  
 8         calendar calendar = calendar.getinstance();  
 9         calendar.set(calendar.hour_of_day, 1); //凌晨1点  
10         calendar.set(calendar.minute, 0);  
11         calendar.set(calendar.second, 0);  
12         date date=calendar.gettime(); //第一次执行定时任务的时间  
13         //如果第一次执行定时任务的时间 小于当前的时间  
14         //此时要在 第一次执行定时任务的时间加一天,以便此任务在下个时间点执行。如果不加一天,任务会立即执行。  
15         if (date.before(new date())) {  
16             date = this.addday(date, 1);  
17         }  
18         timer timer = new timer();
19         //调用的方法
20         task task = new task();  
21         //安排指定的任务在指定的时间开始进行重复的固定延迟执行。  
22         timer.schedule(task,date,period_day);    
23     }  
24     // 增加或减少天数  
25     public date addday(date date, int num) {  
26         calendar startdt = calendar.getinstance();  
27         startdt.settime(date);  
28         startdt.add(calendar.day_of_month, num);  
29         return startdt.gettime();  

java匹配时间段之内的时间

 1 simpledateformat dateformat= new simpledateformat("yyyy-mm-dd hh:mm:ss");
 2                             
 3                             
 4                             
 5                             //开始时间
 6                             calendar begin = calendar.getinstance();
 7                             begin.settime(new date());
 8                             begin.set(calendar.minute, begin.get(calendar.minute) - 30);
 9                            
10                             
11                             //需比较时间
12                             date nowtime = dateformat.parse("2020-04-17 10:12:15");
13                             
14                             //date nowtime = dateformat.parse((new clientaxis2testcuralldata()).getsubutilsimple(content, rgex));
15                             calendar date = calendar.getinstance();
16                             date.settime(nowtime);
17                             
18                             //结束时间
19                             calendar dateend = calendar.getinstance();
20                             dateend.settime(new date());
21                             
22                             
23                             if (date.after(begin) && date.before(dateend)) {
24                                 system.out.println("000000000000000000000000000000000000000000000000000");
25                                 
26                                 
27                             //mysqldemo.add((new clientaxis2testcuralldata()).getsubutilsimple(content, rgex), (new clientaxis2testcuralldata()).getsubutilsimple(content, rgex1), (new clientaxis2testcuralldata()).getsubutilsimple(content, rgex2), (new clientaxis2testcuralldata()).getsubutilsimple(content, rgex3), (new teststringtoxml()).getsubutilsimple(content, rgex4));
28                             system.out.println(i);
29                                 
30                                 
31                             }

 

正则表达式获取string字符串之间的数据

 

 1 package com.xml;
 2 
 3 
 4 import java.util.arraylist;  
 5 import java.util.list;  
 6 import java.util.regex.matcher;  
 7 import java.util.regex.pattern; 
 8 
 9 /**
10  * 正则取中间数
11  * @author lenovo
12  *
13  */
14  
15 public class teststringtoxml {
16     
17        
18     public static void main(string[] args) {
19         string str = "record:data_time2019-12-13 14:37:30.0gateway_logo867726033797152sensor_name1channel_name401value22.0";
20         //string str = "abc3443abcfgjhgabcgfjabc";  
21         string rgex = "data_time(.*?)gateway_logo";
22 
23         string rgex1 = "gateway_logo(.*?)sensor_name";
24         
25         string rgex2 = "sensor_name(.*?)channel_name";
26         
27         string rgex3 = "channel_name(.*?)value";
28         
29         string rgex4 = "value(.*)";
30         
31         
32         system.out.println((new teststringtoxml()).getsubutil(str,rgex)); 
33        
34         list<string> lists = (new teststringtoxml()).getsubutil(str,rgex);
35         for (string string : lists) {
36             system.out.println(string);
37         }
38         
39         system.out.println((new teststringtoxml()).getsubutilsimple(str, rgex));  
40         
41         system.out.println((new teststringtoxml()).getsubutilsimple(str, rgex1)); 
42         
43         
44         system.out.println((new teststringtoxml()).getsubutilsimple(str, rgex2)); 
45         
46         
47         system.out.println((new teststringtoxml()).getsubutilsimple(str, rgex3)); 
48         
49         
50         system.out.println((new teststringtoxml()).getsubutilsimple(str, rgex4)); 
51         
52         
53     }
54     
55     
56     /** 
57      * 正则表达式匹配两个指定字符串中间的内容 
58      * @param soap 
59      * @return 
60      */  
61     public list<string> getsubutil(string soap,string rgex){  
62         list<string> list = new arraylist<string>();  
63         pattern pattern = pattern.compile(rgex);// 匹配的模式  
64         matcher m = pattern.matcher(soap);  
65         while (m.find()) {  
66             int i = 1;  
67             list.add(m.group(i));  
68             i++;  
69         }  
70         return list;  
71     }  
72       
73     /** 
74      * 返回单个字符串,若匹配到多个的话就返回第一个,方法与getsubutil一样 
75      * @param soap 
76      * @param rgex 
77      * @return 
78      */  
79     public string getsubutilsimple(string soap,string rgex){  
80         pattern pattern = pattern.compile(rgex);// 匹配的模式  
81         matcher m = pattern.matcher(soap);  
82         while(m.find()){  
83             return m.group(1);  
84         }  
85         return "";  
86     }  
87 
88     
89     
90     
91     
92     }
93     
94     
95     

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网