2

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/

MacMac
  • 34,294
  • 55
  • 151
  • 222
XP1
  • 6,910
  • 8
  • 54
  • 61
  • Why are you using a JavaScript parsing library instead of a Java one - say, [Joda Time](http://joda-time.sourceforge.net/)? – Matt Ball Apr 08 '11 at 14:01
  • Doesn't Joda Time require date format to parse? DateJS doesn't require date formats. – XP1 Apr 08 '11 at 14:16
  • No matter what library you use, if you don't specify a date format, then the library can only take a best-guess attempt at parsing the date. Case in point, the string `"3/12/1998"` in your code is ambiguous, and could parse to March 12, 1998 or December 3, 1998, depending on the locale. – Matt Ball Apr 08 '11 at 14:20
  • That is not a problem because I only need to specify "date.js", which is US only. DateJS supports over 150 locales: simply specify "date-en-US.js", "date-de-DE.js", "date-fr-FR.js", etc... DateJS is easier for me because I don't have to manually generate a list of possible date formats. – XP1 Apr 08 '11 at 14:25
  • Fair enough. My gut reaction is just that there's got to be a better solution than running JavaScript in Java - it feels like a major WTF. Did you see [this question](http://stackoverflow.com/questions/3389348/parse-any-date-in-java)? – Matt Ball Apr 08 '11 at 14:28

2 Answers2

1

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.

Ralph
  • 118,862
  • 56
  • 287
  • 383
  • I tried SimpleDateFormat, but it is restricted to the date format. – XP1 Apr 08 '11 at 14:18
  • @XP1 what do you mean by: "is restricted to the date format"? - Did I understand the question wrong: you want to parse a date? – Ralph Apr 08 '11 at 14:26
  • If the date format pattern does not match, then I have to find the correct date format pattern. DateJS detects the date automatically. In my question, I wanted to know the best way, or if there is another way, to call the JavaScript method Date.parse() from DateJS in Java without calling eval(). – XP1 Apr 08 '11 at 14:48
1

SimpleDateFormat is overkill here. Regular DateFormat is more than sufficient:

import java.text.DateFormat;

public class Demo
{
    public static void main(String[] args) throws Exception
    {
        System.out.println(DateFormat.getDateInstance(DateFormat.SHORT).parse("3/12/1998").toString());
    }
}

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

Community
  • 1
  • 1
Powerlord
  • 87,612
  • 17
  • 125
  • 175