定時執行程序

          關于定時任務,似乎跟時間操作的聯系并不是很大,但是前面既然提到了定時任務,索性在這里一起解決了。
            設置定時任務很簡單,用Timer類就搞定了。
            一、延時執行
            首先,我們定義一個類,給它取個名字叫TimeTask,我們的定時任務,就在這個類的main函數里執行。代碼如下:
            package test;
            import java.util.Timer;
            public class TimeTask {
            public static void main(String[] args){
            Timer timer = new Timer();
            timer.schedule(new Task(), 60 * 1000);
            }
            }
            解釋一下上面的代碼。
            上面的代碼實現了這樣一個功能,當TimeTask程序啟動以后,過一分鐘后執行某項任務。很簡單吧:先new一個Timer對象,然后調用它的schedule方法,這個方法有四個重載的方法,這里我們用其中一個,
            public void schedule(TimerTask task,long delay)
            首先,第一個參數
            第一個參數就是我們要執行的任務。
            這是一個TimerTask對象,確切點說是一個實現TimerTask的類的對象,因為TimerTask是個抽象類。上面的代碼里面,Task就是我們自己定義的實現了TimerTask的類,因為是在同一個包里面,所以沒有顯性的import進來。Task類的代碼如下
            package test;
            import java.util.TimerTask;
            public class Task extends TimerTask {
            public void run(){
            System.out.println("定時任務執行");
            }
            }
            我們的Task必須實現TimerTask的方法run,要執行的任務就在這個run方法里面,這里,我們只讓它往控制臺打一行字。
            第二個參數
            第二個參數是一個long型的值。這是延遲的時間,就是從程序開始以后,再過多少時間來執行定時任務。這個long型的值是毫秒數,所以前面我們的程序里面,過一分鐘后執行用的參數值就是 60 * 1000。
            二、循環執行
            設置定時任務的時候,往往我們需要重復的執行這樣任務,每隔一段時間執行一次,而上面的方法是只執行一次的,這樣就用到了schedule方法的是另一個重載函數
            public void schedule(TimerTask task,long delay,long period)
            前兩個參數就不用說什么了,最后一個參數就是間隔的時間,又是個long型的毫秒數(看來java里涉及到時間的,跟這個long是脫不了干系了),比如我們希望上面的任務從第一次執行后,每個一分鐘執行一次,第三個參數值賦60 * 1000就ok了。
            三、指定執行時間
            既然號稱是定時任務,我們肯定希望由我們來指定任務指定的時間,顯然上面的方法就不中用了,因為我們不知道程序什么時間開始運行,就沒辦法確定需要延時多少。沒關系,schedule四個重載的方法還沒用完呢。用下面這個就OK了:
            public void schedule(TimerTask task,Date time)
            比如,我們希望定時任務2006年7月2日0時0分執行,只要給第二個參數傳一個時間設置為2006年7月2日0時0分的Date對象就可以了。
            有一種情況是,可能我們的程序啟動的時候,已經是2006年7月3日了,這樣的話,程序一啟動,定時任務就開始執行了。
            schedule最后一個重載的方法是
            public void schedule(TimerTask task,Date firstTime,long period)

          posted @ 2007-06-27 11:59 付軒 閱讀(3265) | 評論 (0)編輯 收藏

          List 集合 找出最小值 和 最大 值

          import java.util.*;
          class Peng{  
              public static void main(String args[])
               {        //Double[] num = { 45.1,45.2 };
                        List dlist=new ArrayList();
                        dlist.add(45.1);
                        dlist.add(45.2);
                        dlist.add(110.22);
                        Double max = (Double)dlist.get(0);    
                        Double min = (Double)dlist.get(0);
                      for (int i = 0; i < dlist.size(); i++) {         
                                if (min > (Double)dlist.get(i)) min = (Double)dlist.get(i);  
                                 if (max < (Double)dlist.get(i)) max = (Double)dlist.get(i);       
                         }       
                                  System.out.println("max的值為" + max + "min的值為" + min);   
                }

          posted @ 2007-06-26 20:50 付軒 閱讀(24189) | 評論 (2)編輯 收藏

          替換字符串

           public class StyleSearchAndReplace {
           public static void main(String args[]) {

             String statement = "The question as to whether the jab is"
                 + " superior to the cross has been debated for some time in"
                 + " boxing circles. However, it is my opinion that this"
                 + " false dichotomy misses the point. I call your attention"
                 + " to the fact that the best boxers often use a combination of"
                 + " the two. I call your attention to the fact that Mohammed"
                 + " Ali,the Greatest of the sport of boxing, used both. He had"
                 + " a tremendous jab, yet used his cross effectively, often,"
                 + " and well";

             String newStmt = statement.replaceAll("The question as to whether",
                 "Whether");

             newStmt = newStmt.replaceAll(" of the sport of boxing", "");
             newStmt = newStmt.replaceAll("amount of success", "success");
             newStmt = newStmt.replaceAll("However, it is my opinion that this",
                 "This");

             newStmt = newStmt.replaceAll("a combination of the two", "both");
             newStmt = newStmt.replaceAll("This is in spite of the fact that"
                 + " the", "The");
             newStmt = newStmt.replaceAll("I call your attention to the fact that",
                 "");

             System.out.println("BEFORE:\n" + statement + "\n");
             System.out.println("AFTER:\n" + newStmt);
           }
          }

          posted @ 2007-06-26 16:01 付軒 閱讀(259) | 評論 (0)編輯 收藏

          得到某一天是星期幾

          //獲取yyyy-MM-dd是星期幾
          public static int getWeekdayOfDateTime(String datetime){
             DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
             Calendar c = Calendar.getInstance();  
                try {
            c.setTime(df.parse(datetime));
             } catch (Exception e) {
            e.printStackTrace();
             }
             int weekday = c.get(Calendar.DAY_OF_WEEK)-1;
             return weekday;
          }

          posted @ 2007-06-26 13:25 付軒 閱讀(2636) | 評論 (0)編輯 收藏

          javamail 郵件群發

          package gmailsender;

          import java.security.Security;
          import java.util.Date;
          import java.util.Properties;

          import javax.mail.Authenticator;
          import javax.mail.Message;
          import javax.mail.MessagingException;
          import javax.mail.PasswordAuthentication;
          import javax.mail.Session;
          import javax.mail.Transport;
          import javax.mail.internet.AddressException;
          import javax.mail.internet.InternetAddress;
          import javax.mail.internet.MimeMessage;

          /**
           * 使用Gmail發送郵件
           * @author Winter Lau
           */
          public class Main {

           public static void main(String[] args) throws AddressException, MessagingException {
            Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
            // Get a Properties object
            Properties props = System.getProperties();
            props.setProperty("mail.smtp.host", "smtp.gmail.com");
            props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
            props.setProperty("mail.smtp.socketFactory.fallback", "false");
            props.setProperty("mail.smtp.port", "465");
            props.put("mail.smtp.auth", "true");
            final String username = "username";
            final String password = "password";
            Session session = Session.getDefaultInstance(props, new Authenticator(){
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }});

          // -- Create a new message --
            Message msg = new MimeMessage(session);
           // -- Set the FROM and TO fields --
            String[] gods={"dfgd@yahoo.com.cn","dfgdf@qq.com"};
            int len=gods.length;
            InternetAddress[] address = new InternetAddress[len];
               for (int i = 0; i < gods.length; i++) {
                address[i] = new InternetAddress(gods[i]);
               }
            msg.setFrom(new InternetAddress("fgdfgf@gmail.com"));
            //msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse("dgddfg@qq.com",false));
            msg.setRecipients(Message.RecipientType.TO,address);
            msg.setSubject("woaizhongguo");
            msg.setText("woaizhongguo");
            msg.setSentDate(new Date());
            Transport.send(msg);
            System.out.println("郵件已發送!");
           

          }
          }


           

          posted @ 2007-06-22 19:09 付軒 閱讀(682) | 評論 (0)編輯 收藏

          javamail

          package gmailsender;

          import java.security.Security;
          import java.util.Date;
          import java.util.Properties;

          import javax.mail.Authenticator;
          import javax.mail.Message;
          import javax.mail.MessagingException;
          import javax.mail.PasswordAuthentication;
          import javax.mail.Session;
          import javax.mail.Transport;
          import javax.mail.internet.AddressException;
          import javax.mail.internet.InternetAddress;
          import javax.mail.internet.MimeMessage;

          /**
           * 使用Gmail發送郵件
           * @author Winter Lau
           */
          public class Main {

           public static void main(String[] args) throws AddressException, MessagingException {
            Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
            // Get a Properties object
            Properties props = System.getProperties();
            props.setProperty("mail.smtp.host", "smtp.gmail.com");
            props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
            props.setProperty("mail.smtp.socketFactory.fallback", "false");
            props.setProperty("mail.smtp.port", "465");
            props.setProperty("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.auth", "true");
            final String username = "";
            final String password = "";
            Session session = Session.getDefaultInstance(props, new Authenticator(){
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }});

                 // -- Create a new message --
            Message msg = new MimeMessage(session);

            // -- Set the FROM and TO fields --
            msg.setFrom(new InternetAddress(username + "@gmail.com"));
            msg.setRecipients(Message.RecipientType.TO,
              InternetAddress.parse("fuxuan1986@gmail.com",false));
            msg.setSubject("Hello");
            msg.setText("How are you");
            msg.setSentDate(new Date());
            Transport.send(msg);
            System.out.println("Message sent.");
           }
          }


           

          posted @ 2007-06-22 12:14 付軒 閱讀(199) | 評論 (0)編輯 收藏

          fdg

          fgdfgdfg

          posted @ 2007-06-17 19:48 付軒 閱讀(146) | 評論 (0)編輯 收藏

          僅列出標題
          共2頁: 上一頁 1 2 
          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 龙州县| 建阳市| 蓬安县| 临高县| 启东市| 慈利县| 衡南县| 临潭县| 屏东市| 盈江县| 来凤县| 福州市| 定远县| 报价| 个旧市| 龙岩市| 石景山区| 南木林县| 灌南县| 老河口市| 凤城市| 宁南县| 太仆寺旗| 夹江县| 宁晋县| 民县| 惠东县| 固原市| 根河市| 铁岭县| 微山县| 上饶县| 襄樊市| 郯城县| 依兰县| 房产| 长阳| 贵州省| 威海市| 海安县| 临夏县|