toLocaleDateString not supporting non-english locales

53 views
Skip to first unread message

Ashok Ilapavluri

unread,
Jan 16, 2024, 12:57:47 AM1/16/24
to mozilla-rhino

I am using rhino 1.7.14 (latest version) in my java application as JS engine

The below code is returing: 'January 15, 2024' (is the value of create_date_de2)

options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
};

create_date_de2 = (new Date()).toLocaleDateString('de-DE',options);

In online javascript, if I run:
const options = { year: 'numeric', month: 'long', day: 'numeric' };
console.log((new Date()).toLocaleDateString('de-DE',options))

it is working fine by providing: 15. Januar 2024

Why rhino is not supporting locale? am I missing anything here? do I need to do any configuration ? Mine is windows machine German language pack also installed.

Appriciate any help on this.

Thanks
Ashok.

Ashok Ilapavluri

unread,
Jan 18, 2024, 10:20:47 AM1/18/24
to mozilla-rhino
Guys,

Any help on the below? really appreciate your help.

Thanks
Ashok. 

Stefan Schnell

unread,
Jan 18, 2024, 1:04:52 PM1/18/24
to mozilla-rhino
Hello Ashok,
I do not know why the code do not work with Rhino, but here a workaround to handle your requirement.

var locale = java.util.Locale.of("de", "DE");
var simpleDateFormat = java.text.SimpleDateFormat("d. MMMM YYYY", locale);
print(simpleDateFormat.format(new Date()));


This code delivers what you need: 18. Januar 2024

Best regards
Stefan

Ashok Ilapavluri

unread,
Jan 18, 2024, 10:47:52 PM1/18/24
to mozill...@googlegroups.com
Hi Stefan,

Thank you for the response, the code you shared is in Java right?, my requirement is to write in javascript which is based on the Rhino engine.

Thanks
Ashok.


--
You received this message because you are subscribed to the Google Groups "mozilla-rhino" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mozilla-rhin...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mozilla-rhino/e4893fe2-5d28-4d2e-85fd-54edf83835f4n%40googlegroups.com.

Stefan Schnell

unread,
Jan 18, 2024, 11:01:59 PM1/18/24
to mozilla-rhino
Hello Ashok,

yes, this is correct, it is Java. You can use Java seamlessly with the Rhino engine. I find this to be one of the biggest advantages of the Rhino engine, the problem-free use of Java in the context of and in combination with JavaScript.

Best regards
Stefan

Ashok Ilapavluri

unread,
Jan 19, 2024, 12:02:35 AM1/19/24
to mozill...@googlegroups.com
Thank you so much Stefan, it worked for me!!!!

My Rhino is based on Java8, hence I modified it like below and it worked for me, I need to import them explicitly using the Packages prefix to make it work.

Thank you once again for the help :)
Appreciate if you find any official documentation on this for reference.


var Locale = Packages.java.util.Locale;
  var locale = new Locale("de", "DE");
var SimpleDateFormat = Packages.java.text.SimpleDateFormat;
var simpleDateFormat = new SimpleDateFormat("d. MMMM yyyy", locale);

On Thu, Jan 18, 2024 at 11:34 PM Stefan Schnell <stefan.schn...@gmail.com> wrote:
--
Reply all
Reply to author
Forward
0 new messages