多線程定時器時間段3秒執行一次 1 Timer timer = new Timer(); 2 timer.schedule(new TimerTask() { 3 // 在run方法中的語句就是定時任務執行時運行的語句。 4 public void run() { 5 System.out.prin ...
多線程定時器時間段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