admin管理员组文章数量:1404942
Is there a simple way to convert a JavaScript Date to a Java 8 date-time with a time-zone?
I have a web application which consists of two parts, front end is written in JavaScript and the back end in Java. A user should be able to select a date-time in UI and the value should be consumed by the back end. What is the easiest way of converting this value to Java date-time?
Is there a simple way to convert a JavaScript Date to a Java 8 date-time with a time-zone?
I have a web application which consists of two parts, front end is written in JavaScript and the back end in Java. A user should be able to select a date-time in UI and the value should be consumed by the back end. What is the easiest way of converting this value to Java date-time?
Share edited Jan 10, 2018 at 18:47 Boris asked Mar 31, 2016 at 8:04 BorisBoris 24.5k16 gold badges54 silver badges74 bronze badges 3- 3 It would be helpful if you'd provide more description in your question. Presumably you asked this knowing you were about to answer it, which is fine - but if anyone else were trying to answer, they'd need more detail... so you should provide that detail as if you didn't know the answer to start with. That way it will be more helpful for future readers. – Jon Skeet Commented Mar 31, 2016 at 8:12
- 1 up voted because "it is not merely OK to ask and answer your own question, it is explicitly encouraged." See: It's OK to Ask and Answer Your Own Questions and Can I answer my own question? – Yogi Commented Mar 31, 2016 at 8:28
-
What exactly do you mean by “JavaScript Date”? The spec for ECMAScript 2017 says the string format for a date-time is a simplification of the ISO 8601 standard:
YYYY-MM-DDTHH:mm:ss.sssZ
. – Basil Bourque Commented Dec 23, 2017 at 0:18
4 Answers
Reset to default 0use ZoneId.of(String zoneID)
ZoneId.of("UTC");
ZoneId.of("-08:00");
I recently had to do this in a web application with a Java server backend and Javascript running the browser. The problem was that the Javascript code that built the Request to be sent to the server was closed to my code, so I was unable to call any specific methods on the instance of the Javascript Date object to control the formatting. Thus the toString()
method was being implicitly called and the resulting string looked like:
Mon Sep 28 1998 14:36:22 GMT-0700 (PDT)
when using Chrome, Firefox or Safari
Digging into the specification reveals that this method calls toDateString(tv)
where tv
is the time value of the Date instance.
Unfortunately it seems to be up to the implementing flavour to decide how exactly this ends up being displayed:
Return an implementation-dependent String value that represents tv as a date and time in the current time zone using a convenient, human-readable form.
So we can't be sure that all browsers will format the date the same way when .toString()
is called but anecdotally I observed the same format when I tested with a few browsers and changed the region settings, language and calendar to a variety of interesting ones (such as Ethiopian) and I still ended up with the same date format.
Working on the assumption that it's consistent, we can now parse the date in Java using the following:
ZonedDateTime zdt = DateTimeFormatter.ofPattern("EEE MMM d yyyy HH:mm:ss 'GMT'Z (z)")
which preserves the Time Zone information sent by the client.
Anyway, I hope this is useful to someone else, and I'd be grateful if anyone can point to any documentation as to the (in)consistency of Javascript Date toString()
method.
If you can use momentjs, here's something simple that can be consumed easily by Java8:
var zone = moment.tz.guess();
var localDateTime = moment("2018-11-17T11:43:13").format('YYYY-MM-DDTHH:mm:ssZ');
console.log(localDateTime + '[' + zone + ']');
produces (in my timezone):
2018-11-17T11:43:13-05:00[America/Toronto]
And on the Java8 side:
final ZonedDateTime parsed = ZonedDateTime.parse("2018-11-17T11:43:13-05:00[America/Toronto]");
You can convert time using The Universal Coordinated Time (UTC) and ZonedDateTime, for example:
Thu, 31 Mar 2016 06:49:02 GMT
Thu, 31 Mar 2016 06:49:02 -0830
JavaScript
var d = new Date();
var n = d.toUTCString();
toUTCString() method converts a Date object to a string, according to universal time. For example, console.log(n);
will print out a result:
Tue, 17 Mar 2020 08:59:19 GMT
Java 8
ZonedDateTime zdt = ZonedDateTime.parse(text, DateTimeFormatter.RFC_1123_DATE_TIME);
text is a UTC date and time string
ZonedDateTime is a date-time with a time-zone in the ISO-8601 calendar system
RFC_1123_DATE_TIME is a predefined RFC-1123 date-time formatter.
Printing zdt
for the above example value will produce:
2020-03-17T08:59:19Z
本文标签: How to convert JavaScript Date to Java datetime with a timezoneStack Overflow
版权声明:本文标题:How to convert JavaScript Date to Java date-time with a time-zone? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744861589a2629101.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论