Powered by Ajaxy
Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Java: How to run JavaScript and get the result by using Java

Java: How to run JavaScript and get the result by using Java
{filelink=943}

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class JavaScriptToJava {
  public static void main(String args[]) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("javascript");
    try {
      double val = (Double) engine.eval("var date = new Date();"+"date.getDay()");
      String jour="";
      int num=(int)val;
      switch(num)
      {
      	case 1:
      		jour="lundi";
      		break;
      	case 2:
      		jour="Mardi";
      		break;
      	case 3:
      		jour="Mercredi";
      		break;
      	case 4:
      		jour="jeudi";
      		break;
      	case 5:
      		jour="Vendredi";
      		break;
      }
      System.out.println("Day Of week(Num): "+num);
      System.out.println("Day Of week(name): "+jour);
    } catch (ScriptException e) {
      System.err.println(e);
    }
  }
}

Users who found this page were searching for:

  • perl absolute value
  • python loop hash keys
  • java get socket address port
  • replace image in JLabel Java

No comments yet.

Leave a Comment