admin管理员组文章数量:1355535
I store dates on server side in UTC timezone. When client (browser) wants to pass some date to server it sends date like
"Tue Jan 03 2012 16:50:32 GMT+0400 (Russian Standard Time)"
- Is this format standard across all browsers?
- If 1. is false, how can I redefine the
Date
format function? Is this a good practice? - How can I convert JS
Date
s to a UTCjava.util.Date
withjava.text.SimpleDateFormat
?
UPDATE
I thought that passing date as formatted string (with timezone part) will cause less headache since I shouldn't bother to convert dates to UTC on client side. So I avoid any date conversions in JS code.
I store dates on server side in UTC timezone. When client (browser) wants to pass some date to server it sends date like
"Tue Jan 03 2012 16:50:32 GMT+0400 (Russian Standard Time)"
- Is this format standard across all browsers?
- If 1. is false, how can I redefine the
Date
format function? Is this a good practice? - How can I convert JS
Date
s to a UTCjava.util.Date
withjava.text.SimpleDateFormat
?
UPDATE
I thought that passing date as formatted string (with timezone part) will cause less headache since I shouldn't bother to convert dates to UTC on client side. So I avoid any date conversions in JS code.
Share Improve this question edited Jan 3, 2012 at 13:11 Mike Samuel 121k30 gold badges227 silver badges254 bronze badges asked Jan 3, 2012 at 12:55 Plastic RabbitPlastic Rabbit 3,0194 gold badges28 silver badges27 bronze badges 4- 1 What does the client code look like? It would be simpler if it could pass the "milliseconds since the Unix epoch" value, which would only need simple integer parsing. – Jon Skeet Commented Jan 3, 2012 at 12:58
- see stackoverflow./questions/999172/how-to-parse-date-in-java – Hemant Metalia Commented Jan 3, 2012 at 13:01
- @jon-skeet Please, take a look to my update. Thanks. – Plastic Rabbit Commented Jan 3, 2012 at 13:06
-
@PlasticRabbit: JavaScript
Date
already knows the UTC milliseconds-since-epoch - it's better not to introduce time zones. – Jon Skeet Commented Jan 3, 2012 at 13:22
3 Answers
Reset to default 4You should send the number of milliseconds since epoch (1 Jan 1970 UTC) that is available via the +
prefix operator as in +new Date(2012, 0, 1)
.
Sending anything with a timezone requires both machines to have the same timezone definitions which means you will likely end up with subtle bugs where two dates occurred in one order on one machine but in a different order on another. You can eliminate that whole class of bugs by using the millis since epoch representation.
To answer your questions:
Is this format standard across all browsers?
Date.prototype.toString
and toUTCString
are both implementation dependent but toISOString
is reliable.
http://es5.github./#x15.9.5.43
15.9.5.43
Date.prototype.toISOString ( )
# Ⓣ ⓇThis function returns a String value represent the instance in time represented by this Date object. The format of the String is the Date Time string format defined in 15.9.1.15. All fields are present in the String. The time zone is always UTC, denoted by the suffix Z. If the time value of this object is not a finite Number a RangeError exception is thrown.
15.9.1.15 Date Time String Format # Ⓣ Ⓔ Ⓑ
ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 Extended Format. The format is as follows:
YYYY-MM-DDTHH:mm:ss.sssZ
Whereas http://es5.github./#x15.9.5.2 says
15.9.5.2
Date.prototype.toString ( )
# Ⓣ ⓇThis function returns a String value. The contents of the String are implementation-dependent, but are intended to represent the Date in the current time zone in a convenient, human-readable form.
http://es5.github./#x15.9.1.15
15.9.5.42
Date.prototype.toUTCString ( )
# Ⓣ ⓇThis function returns a String value. The contents of the String are implementation-dependent, but are intended to represent the Date in a convenient, human-readable form in UTC.
NOTE The intent is to produce a String representation of a date that is more readable than the format specified in 15.9.1.15. It is not essential that the chosen format be unambiguous or easily machine parsable. If an implementation does not have a preferred human-readable format it is remended to use the format defined in 15.9.1.15 but with a space rather than a “T” used to separate the date and time elements.
I don't know whether this format is standardized for all browsers.
But you could use the getTime()
function in JavaScript which returns the milliseconds since 1 January 1970 00:00:00 UTC
and initializes the Java Date object
with this value.
You could send the data in UNIX time format, maybe?
本文标签: Passing javascript date to javaStack Overflow
版权声明:本文标题:Passing javascript date to java - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743954567a2567898.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论