zoukankan      html  css  js  c++  java
  • 将 java 改写成 beanshell 的经验之谈

    下面经验仅仅针对 bsh for android 而谈, PC 上 beanshell 无需这样改。

    public class TimeTest  改写为闭包: TimeTest() 

    闭包末尾添加语句 return this;

    public static void main(String[] args) 改写为: run()

    最后添加
    timetest = TimeTest(); // call 闭包 == new TimeTest()

    timetest.run();

    比如: TimeTest.bsh

    import java.io.*;
    import java.net.*;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.*;
    
    TimeTest()
    {
       private static DateFormat fmt = new SimpleDateFormat("yy-MM-dd HH:mm:ss") ;
     
       /** UTC -> local Time 
        */
       public static String getLocalTimeFromUTC(String UTCTime){
          java.util.Date UTCDate = null ;
          String localTimeStr = null ;
          try {
             UTCDate = fmt.parse(UTCTime);
             fmt.setTimeZone(TimeZone.getTimeZone("GMT+8")) ;
             localTimeStr = fmt.format(UTCDate) ;
          } catch (ParseException e) {
             print(e);
          }
          return localTimeStr ;
       }
    
       run()
       {  
          Calendar cal = new GregorianCalendar();
          zone = cal.getTimeZone().getID();
          print(zone);
          Date date1 = new Date();
          time1 = fmt.format(date1);
          print("my 20"+time1);
          try
          {  
             Socket s = new Socket("time.nist.gov", 13);
             try
             {
                InputStream inStream = s.getInputStream();
                Scanner in = new Scanner(inStream);
                
                while (in.hasNextLine())
                {  
                   String line = in.nextLine();
                   // print(line);
                   if (line.length() >24){                  
                      String UTC = line.substring(6,24);
                      print("UTC  "+UTC);
                      print("CN 20"+getLocalTimeFromUTC(UTC));
                   }
                }
             }
             finally
             {
                s.close();
             }
          }
          catch (IOException e)
          {  
             print(e);
          }
       }
    
       return this;
    }
    
    timetest = TimeTest();
    timetest.run();
    



  • 相关阅读:
    软件工程讲义 3 两人合作(2) 要会做汉堡包
    创新 王屋村的魔方们
    IT 行业的创新 创新的迷思 (56)
    现代软件工程讲义 12 绩效管理
    校园招聘 比较容易的面试题
    技能的反面 魔方和模仿
    现代软件工程讲义 2 工程师的能力评估和发展
    计算机知识体系的三个部分
    不鸣则已 一鸣惊人
    京城名流的聚会
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/9975181.html
Copyright © 2011-2022 走看看