From java to beanshell

Source: Internet
Author: User

Public class TimeTest is rewritten as a closure: TimeTest ()
Add the return this statement at the end of the closure;

Public static void main (String [] args) is rewritten as: run ()

Last added
Timetest = TimeTest (); // call closure = new TimeTest ()

Timetest. run ();

Example: 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();



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.