Multi-thread timer is executed once every 3 seconds
1 Timer timer = new Timer (); 2 timer.schedule ( new TimerTask () { 3 // The statement in the run method is the statement that runs when the scheduled task is executed. 4 public void run () { 5 System.out.println ("11232" ); 6 7 try { 8 // Call method 9 new ClientAxis2TestCurAllData (); 10 } catch (Exception e) { 11 // TODO Auto-generated catch block 12 e.printStackTrace (); 13 } 14 15 } 16 // Indicates to start execution after 3 seconds, and execute once every 2 seconds 17 }, 0, 3000);
Multi-threaded timer is executed once every day at 0:00
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 (); // The time for the first execution of the scheduled task 13 // If the time for the first execution of the scheduled task is less than the current time 14 // At this time, the timing of the first execution Add one day to the task time so that this task will be executed at the next time point. If you do not add a day, the task will be executed immediately. 15 if (date.before ( new Date ())) { 16 date = this .addDay (date, 1 ); 17 } 18 Timer timer = new Timer (); 19 // Method called 20 Task task = new Task ( ); 21 // Schedule the specified task to start repeated fixed delay execution at the specified time. twenty two 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 match time within time period
1 SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss" ); 2 3 4 5 // Start time 6 Calendar begin = Calendar.getInstance (); 7 begin.setTime ( new Date ()); 8 begin.set (Calendar.MINUTE, begin.get (Calendar.MINUTE)-30 ); 9 10 11 // Compare time 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 }
Regular expression to get data between String strings
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 * Regular expression matches the content between two specified strings 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); // Matching pattern 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 * return a single string, if there are more than one match, return the first one, method Same as 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