Hi I am looking to set the timezone for San Antonio, Texas.Can some please tell me how do i set the same in my Java code.
I want it in the format somewhat similar to America/New York
Currently I am using this code
TimeZone.getTimeZ开发者_如何学运维one("America/Denver");
But "America/Denver" doesn't seem to be the right timezone for San Antonio, Texas
For San Antonio, you should use TimeZone.getTimeZone("US/Central")
If you really need to use "America/<City>"
format, the closest to San Antonio us city is "America/Chicago"
.
You can use this to get a list of all available specific IDs for US/Central:
String[] values = TimeZone.getAvailableIDs(TimeZone.getTimeZone("US/Central").getRawOffset());
And then, double check here:
http://www.timeanddate.com/time/zones/
San Antonio → America/Chicago
The IANA time zone identifier for San Antonio is America/Chicago
, as shown on Time.is.
Using java.time
The modern way to handle date-time is with the java.time classes.
The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds.
Instant instant = Instant.now(); // Current moment in UTC.
Apply a ZoneId
to get a ZonedDateTime
.
ZoneId z = ZoneId.of( "America/Chicago" );
ZonedDateTime zdt = instant.atZone( z );
Avoid 3-4 letter zone abbreviations
Specify a proper time zone name in the format of continent/region
such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
.
Never use the 3-4 letter abbreviation such as EST
or IST
or PST
as they are not true time zones, not standardized, and not even unique(!).
The time zone name is often a city. This naming is not meant literally to be just that city. Such naming is simply a way to label a region whose inhabitants have a shared history of the same time zone rules. That region may be quite large if the entire swath of land has always been populated by people sharing the same set of rules.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
related topic: How to handle calendar TimeZones using Java?
timezone for San Antonio:
Standard time zone: UTC/GMT -6 hours
Daylight saving time: +1 hour
Current time zone offset: UTC/GMT -5 hours
Time zone abbreviation: CDT - Central Daylight Time
edit: this link is better in regards to America/New York format: TimeZones in Java
I totally googled your question and got this: http://www.few.vu.nl/~eliens/documents/java/jdk1.2-docs/docs/api/java/util/TimeZone.html
You can also get a TimeZone using getTimeZone along with a time zone ID. For instance, the time zone ID for the Pacific Standard Time zone is "PST". So, you can get a PST TimeZone object with: Use this:
TimeZone tz = TimeZone.getTimeZone("PST");
精彩评论