问题描述
我在MST中,而我希望在PST中约会.我设置了我想要的timeZone.现在,如果我执行 c.getTime()
,我将始终获得服务器时间.相反,我想要太平洋日期时间.请帮忙如何获取指定时区中的日期时间对象.
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)
推荐答案
更新: java.time Joda-Time 项目./em>类.参见其他答案.
UPDATE: The Joda-Time project has been succeeded by the java.time classes. See this other Answer.
(a)使用 Joda-Time (或Java 8中内置的新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)您的问题不清楚.您对答案的评论谈到了比较,但是您对问题中的比较一无所知.
(b) Your question is not clear. Your comments on answers talk about comparing, but you say nothing about comparing in your question.
(c)避免使用3个字母的时区缩写.阅读有关 TimeZone 类.
(c) Avoid the use of 3-letter time zone abbreviations. Read note of deprecation in Joda-Time doc for TimeZone class.
(d)避免使用默认时区.说你的意思.您计算机的时区可以有意更改,也可以无意更改.
(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 );
运行时...
nowDenver: 2013-11-21T18:12:49.372-07:00
nowCalifornia: 2013-11-21T17:12:49.372-08:00
关于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
这篇关于基于TimeZone Java/Groovy的日期时间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!