English 中文(简体)
How to print out time zone abbreviations when using offset hours in Joda Time?
原标题:

I m using Joda Time, and I m being passed DateTimeZones that are created using DateTimeZone.forOffsetHours(). I d like to print these timezones using standard timezone acronyms such as "PST", "EST", etc.

However, whenever I print DateTimes that use these timezones, I get an "hh:mm" representation of the timezone instead of the name acronym.

Here s an example:

public class tmp {
    public static void main( String args[] ) {
        // "PST"
        System.out.println( DateTimeFormat.forPattern("z").print( new DateTime() ) );

        // "PST"
        System.out.println( DateTimeFormat.forPattern("z").print( new DateTime( DateTimeZone.forTimeZone( TimeZone.getTimeZone("PST")) )) );

        // "-08:00"
        System.out.println( DateTimeFormat.forPattern("z").print( new DateTime( DateTimeZone.forOffsetHours(-8) )) );
    }
}

Is there a way to print out the appropriate timezone acronym in the last example using Joda Time?

最佳回答

No, it is not possible, but it s not a joda-time issue, it s because of how timezones work.

The offset (e.g. UTC-8) doesn t determine the location, so is also doesn t determine the acronym that depends on the location. As you can see here, there is UTC-8 in multiple timezones.

The first example works because your default timezone is PST. The second one works because you ask for a timezone by its name (with all the daylight saving stuff, etc.). In the third one you get a fixed-offset timezone that has no name associated with it.

问题回答

暂无回答




相关问题
Set time zone using Clock Configuration Service Provider

I am attempting to use the Clock CSP to set the system time zone on a Windows Mobile 6 Professional device (Opticon H-19A). I am using the following XML: <characteristic type="clock"> <...

How to manage timezones in a web application?

I wan t to manage the different timezones of my users in my web application, but I have no idea where to start. I have to save the local time of each user in my database?, or maybe make the conversion ...

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Converting asp.net/sql web app to be ime zone savy

Our current app stores all dates via the server s datetime. I m thinking we need to update all datetime values in the database over to UTC time. Now for displaying these dates back to the user, I ...

How to determine if date/time is EDT or EST in Java?

I have a PDF that users must complete. The PDF has date and time fields on it. The PDF instructs the users to input the date and time in Eastern format (Not EST or EDT). When the user completes the ...

Named scope not cooperating with timezone?

A really dodgy problem I ve got. Here s my model: class Entry < ActiveRecord::Base default_scope :order => published_at DESC named_scope :published, :conditions => ["published_at < ?...

热门标签