首页 > 编程语言 > 详细

将 java 改写成 beanshell 的经验之谈

时间:2014-04-27 12:49:57      阅读:421      评论:0      收藏:0      [点我收藏+]
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) {
         e.printStackTrace();
      }
		 
      return localTimeStr ;
   }

   run()
   {  
      Calendar cal = new GregorianCalendar();
      zone = cal.getTimeZone().getID();
      print(zone);
      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)
      {  
         e.printStackTrace();
      }
   }

   return this;
}

timetest = TimeTest();
timetest.run();



将 java 改写成 beanshell 的经验之谈,布布扣,bubuko.com

将 java 改写成 beanshell 的经验之谈

原文:http://blog.csdn.net/belldeep/article/details/24573283

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!