0

I have java date something like this Wed Feb 01 2017 03:58:56 UTC and I want to get this is in javascript format like Wed Feb 01 2017 03:58:56 GMT+0100 (UTC).

how can I get this?

Thanks.

tyro
  • 577
  • 8
  • 17
  • you can use `SimpleDateFormatter` or `DateTimeFormatter` https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html – Jos Feb 01 '17 at 16:33
  • Have you tried searching in Google for this. You will get tons of answer there itself. Please give a try. – Nirdesh Sharma Feb 01 '17 at 16:34
  • Please may you learn that Java is not JavaScript? To which language are you referring? – evolutionxbox Feb 01 '17 at 16:49
  • guys obviously I have given try and still searching, and trying the answer.. – tyro Feb 01 '17 at 19:18
  • `Wed Feb 01 2017 03:58:56 GMT+0100 (UTC)` is javascript date format hence I had tagged the javascript as well for this post. Simple date format could give me `Wed Feb 01 2017 03:58:56 UTC` and not the way I want. Nw I'm tryng Nashorn library allows to execute js in java code. `engine.eval("new Date()");` gives `[Date 2017-02-01T19:15:57.572Z]`; and `new Date()` function in js gives date as I wanted `Wed Feb 15 2017 19:11:33 GMT+0000 (GMT)`. Can anybody tell me why nashorn is not giving the o/p as that of `new Date()` in js ? – tyro Feb 01 '17 at 19:33
  • So this gives the expected o/p `engine.eval("print(new Date())");` but it just prints. And below code where I want to get the value in variable, it gives o/p in `[Date 2017-02-01T19:15:57.572Z]` format String ss= engine.eval("eval(new Date())").toString(); System.out.println(ss); – tyro Feb 01 '17 at 20:04
  • Your example string `Wed Feb 01 2017 03:58:56 GMT+0100 (UTC)` is incorrect, as `+0100` is not UTC. – Basil Bourque Feb 01 '17 at 21:51
  • That was just an example.. apologies that I didnt put the correct date.. – tyro Feb 02 '17 at 07:25
  • I found what I wanted; below is the snippet `Date d = new Date(); SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd YYYY '00:00:00' 'GMT'Z '('z')'"); format.setTimeZone(TimeZone.getTimeZone("UTC")); String dateStr = format.format(d); System.out.println(dateStr);` *Output: Thu Feb 02 2017 00:00:00 GMT+0000 (UTC)* – tyro Feb 02 '17 at 07:28

0 Answers0