我如何调用 DateJS? Java 中的 Date.parse() ?
如何在Java中调用DateJS的Date.parse()?
这就是我正在使用的:
import javax.script.*;
public class Demo
{
public static void main(String[] args) throws Exception
{
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("JavaScript");
scriptEngine.eval(new java.io.FileReader("date.js"));
System.out.println(scriptEngine.eval("Date.parse(\"3/12/1998\").toString();"));
}
}
输出:
run:
Thu Mar 12 1998 00:00:00 GMT-0500 (EST)
BUILD SUCCESSFUL (total time: 0 seconds)
Is there a better way to call Date.parse() in Java?
日期JS: http://www.datejs.com/
How do I call DateJS' Date.parse() in Java?
This is what I am using:
import javax.script.*;
public class Demo
{
public static void main(String[] args) throws Exception
{
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("JavaScript");
scriptEngine.eval(new java.io.FileReader("date.js"));
System.out.println(scriptEngine.eval("Date.parse(\"3/12/1998\").toString();"));
}
}
Output:
run:
Thu Mar 12 1998 00:00:00 GMT-0500 (EST)
BUILD SUCCESSFUL (total time: 0 seconds)
Is there a better way to call Date.parse() in Java?
DateJS:
http://www.datejs.com/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SimpleDateFormat
在这里太过分了。常规DateFormat
就足够了:输出
Thu Mar 12 00:00:00 EST 1998
当您不知道用户输入的日期格式时,就会出现问题。 Java 的常见日期/时间库似乎都没有任何解析这些的方法......至少内置的和 JodaTime 没有。
您可能对问题 PHP's strtotime() in Java 的接受答案有一些运气
SimpleDateFormat
is overkill here. RegularDateFormat
is more than sufficient:Outputs
Thu Mar 12 00:00:00 EST 1998
The problem comes up when you don't know what date format the user is entering. None of Java's common date/time libraries seem to have any way of parsing those... at least the built-in and JodaTime ones don't.
You may have some luck with the accepted answer to the question PHP's strtotime() in Java
怎么样:
Date date new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").parse(outputFromFile);
yyyy.MM.dd.HH.mm.ss 是不是正确的模式,它只是一个示例
您可以编写一个日期格式模式列表并尝试解析它们,直到没有出现异常。
What about:
Date date new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").parse(outputFromFile);
yyyy.MM.dd.HH.mm.ss is not the correct, pattern, it is only an example
You could write a list of date format patterns and try to parse them until one did not rise an exception.