将 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();



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值