問題描述
我在 MST,我希望我的日期在 PST.我設(shè)置了我想要的時(shí)區(qū).現(xiàn)在,如果我這樣做 c.getTime()
我總是得到我的服務(wù)器時(shí)間.相反,我想要太平洋日期時(shí)間.請(qǐng)幫忙如何獲取指定時(shí)區(qū)的日期時(shí)間對(duì)象.
I am in MST and I want my Date in PST. I set the timeZone that I want.
Now if i do c.getTime()
I always get my server time.
Instead I want Pacific Date time. Please help
How to get the date time Object in the specified timezone.
Calendar c= Calendar.getInstance();
TimeZone timezone= TimeZone.getTimeZone("PST");
c.setTimeZone(timezone)
推薦答案
更新: Joda-Time 項(xiàng)目已經(jīng)被 java.time 類.請(qǐng)參閱其他答案.
UPDATE: The Joda-Time project has been succeeded by the java.time classes. See this other Answer.
(a) 使用 Joda-Time(或 Java 8 中內(nèi)置的新 JSR 310).甚至不要考慮使用臭名昭著的糟糕 java.util.Date/Calendar.
(a) Use Joda-Time (or new JSR 310 built into Java 8). Don't even think about using the notoriously bad java.util.Date/Calendar.
(b) 你的問題不清楚.您對(duì)答案的評(píng)論談?wù)摫容^,但您在問題中沒有提及比較.
(b) Your question is not clear. Your comments on answers talk about comparing, but you say nothing about comparing in your question.
(c) 避免使用 3 個(gè)字母的時(shí)區(qū)縮寫.閱讀 Joda-Time 文檔中關(guān)于 TimeZone<的棄用說明/a> 類.
(c) Avoid the use of 3-letter time zone abbreviations. Read note of deprecation in Joda-Time doc for TimeZone class.
(d) 避免使用默認(rèn)時(shí)區(qū).說出你的意思.您計(jì)算機(jī)的時(shí)區(qū)可以有意或無意更改.
(d) Avoid default time zone. Say what you mean. The time zone of your computer can change intentionally or not.
(e) 在 StackOverflow 中搜索joda"以獲取大量代碼片段和示例.
(e) Search StackOverflow for 'joda' for lots of code snippets and examples.
(f) 這里有一些 Joda-Time 示例代碼可以幫助您入門.
(f) Here's some Joda-Time example code to get you started.
// ? 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// Specify your time zone rather than rely on default.
org.joda.time.DateTimeZone californiaTimeZone = org.joda.time.DateTimeZone.forID( "America/Los_Angeles" );
org.joda.time.DateTimeZone denverTimeZone = org.joda.time.DateTimeZone.forID( "America/Denver" );
org.joda.time.DateTime nowDenver = new org.joda.time.DateTime( denverTimeZone );
org.joda.time.DateTime nowCalifornia = nowDenver.toDateTime( californiaTimeZone );
// Same moment in the Universe’s timeline, but presented in the local context.
System.out.println( "nowDenver: " + nowDenver );
System.out.println( "nowCalifornia: " + nowCalifornia );
運(yùn)行時(shí)……
nowDenver: 2013-11-21T18:12:49.372-07:00
nowCalifornia: 2013-11-21T17:12:49.372-08:00
關(guān)于 Joda-Time…
About Joda-Time…
// Joda-Time - The popular alternative to Sun/Oracle's notoriously bad date, time, and calendar classes bundled with Java 7 and earlier.
// http://www.joda.org/joda-time/
// Joda-Time will become outmoded by the JSR 310 Date and Time API introduced in Java 8.
// JSR 310 was inspired by Joda-Time but is not directly based on it.
// http://jcp.org/en/jsr/detail?id=310
// By default, Joda-Time produces strings in the standard ISO 8601 format.
// https://en.wikipedia.org/wiki/ISO_8601
// About Daylight Saving Time (DST): https://en.wikipedia.org/wiki/Daylight_saving_time
// Time Zone list: http://joda-time.sourceforge.net/timezones.html
這篇關(guān)于基于 TimeZone Java/Groovy 的日期時(shí)間轉(zhuǎn)換的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!