/** * sorts the date into a prerequisite format based on the index. * * <p>currently does not work corretly with javascript method calls, if this method was commented * in, it would be called, even for calls to printd(String,Scritable) <br> * returns a javascript string. */ public Object printd(final int index, final org.mozilla.javascript.Scriptable obj) { if (DebugDefaultJavascript) { System.out.println("JpedalDefaultJavascript.printd(int,Scriptable)"); } Calendar date = null; if (org.mozilla.javascript.NativeJavaObject.canConvert(obj, Calendar.class)) { date = (java.util.Calendar) org.mozilla.javascript.Context.jsToJava(obj, Calendar.class); } if (date == null) { org.mozilla.javascript.Context.throwAsScriptRuntimeEx(new RuntimeException("Not a Date()")); } // <start-demo><end-demo> final SimpleDateFormat df = new SimpleDateFormat(format[index]); return context.newObject(scope, "String", new Object[] {df.format(date)}); }
/** * sorts the date into the string format specified * * <p>longMonth = "mmmm", shortMonth = "mmm", lead0NumberedMonth = "mm", nonLead0NumberedMonth = * "m", longDay = "dddd", shortDay= "ddd", lead0NumberedDay = "dd", nonLead0NumberedDay = "d", * longYear = "yyyy", shortYear = "yy", lead024Hour = "HH", nonLead024Hour = "H", lead012Hour = * "hh", nonLead012Hour ="h", lead0Mins = "MM", nonLead0Mins = "M", lead0Secs = "ss", nonLead0Secs * = "s", amPm = "tt", singleDigitAmPm = "t", escapeChar = "\"; <br> * returns a javascript string. */ public Object printd(final String format, final org.mozilla.javascript.Scriptable obj) { if (DebugDefaultJavascript) { System.out.println("JpedalDefaultJavascript.printd(String,Scriptable)"); } // get the Date class from the Scriptable value passed in as // Rhino does not allow NativeDate to be used externally, even though this is what is passed // here. Date jsDate = null; if (org.mozilla.javascript.NativeJavaObject.canConvert(obj, Date.class)) { jsDate = (java.util.Date) org.mozilla.javascript.Context.jsToJava(obj, Date.class); } if (jsDate == null) { org.mozilla.javascript.Context.throwAsScriptRuntimeEx(new RuntimeException("Not a Date()")); } Calendar date = Calendar.getInstance(); date.setTime(jsDate); // jsDate = null;//delete the variable so it does not get used // <start-demo><end-demo> // replace the javascript format string with a java format string to produce the required // output. // we need to do this as some chars represent different types of data in each final char[] formatVal = format.toCharArray(); final StringBuffer retVal = new StringBuffer(); for (int i = 0; i < formatVal.length; i++) { if (formatVal[i] == 'm') { // replace JS month with java month retVal.append('M'); } else if (formatVal[i] == 't') { if (formatVal[i + 1] == 't') { // if tt replace with char for AM/PM retVal.append('a'); i++; } else { // single t special case, replace with either A/P if (date.get(Calendar.HOUR_OF_DAY) > 12) { retVal.append("'P'"); } else { retVal.append("'A'"); } } } else if (formatVal[i] == 'M') { // replace JS min for java min retVal.append('m'); } else if (formatVal[i] == 'd' && formatVal[i + 1] == 'd' && formatVal[i + 2] == 'd') { // if word for day is needed then change to java word of day if (formatVal[i + 3] == 'd') { // long JS day replaced with long java day retVal.append("EEEE"); i += 3; } else { // short JS day replaced with short java day retVal.append("EEE"); i += 2; } } else if (formatVal[i] == '\\') { retVal.append('\''); retVal.append(formatVal[++i]); retVal.append('\''); } else { retVal.append(formatVal[i]); } } // create the formatter object with the required format final SimpleDateFormat df = new SimpleDateFormat(new String(retVal)); final String newDate = df.format(date.getTime()); if (DebugDefaultJavascript) { System.out.println("returning String=" + newDate); } return context.newObject(scope, "String", new Object[] {newDate}); }