多线程定时器时间段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 Listlists = (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 getSubUtil(String soap,String rgex){ 62 List list = new ArrayList (); 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