Tried following code to get timezone conversion and failed.

public static String ConvertTmz(String timeFormat, String timeStr, String tmz){
        String theTimeFormat="MM/dd/yyyy HH:mm:ss";
        if (timeFormat!=null) theTimeFormat=timeFormat;
        SimpleDateFormat sdf = new SimpleDateFormat(theTimeFormat);

        sdf.setTimeZone(TimeZone.getTimeZone(tmz));

        GetTimeStr(timeStr);
        GregorianCalendar time=new GregorianCalendar(yyyy, mm-1, dd, hh, mi, ss);
        return sdf.format(time.getTime());

    }

Please help.

What does line 8 do? Where do all those variables on line 9 come from?

Sorry for the confusion. See following for correct one

    public static String GetTimeString(String timeFormat, java.util.Date date, String tmz){
        String theTimeFormat="MM/dd/yyyy HH:mm:ss";
        if (timeFormat!=null) theTimeFormat=timeFormat;
        SimpleDateFormat formatter = new SimpleDateFormat(theTimeFormat);

        formatter.setTimeZone(TimeZone.getTimeZone(tmz));

        String dateString =null;
        if (date==null) dateString = formatter.format(new java.util.Date());
        else  dateString = formatter.format(date);

        return dateString;
    }

System.out.println(GetTimeString(null, null, "PST")); -> 03/22/2013 08:12:08
System.out.println(GetTimeString(null, null, "UTC")); -> 03/22/2013 15:12:55

That looks to me like it's working perfectly

what exactly is the problem??
I tried by putting tmz="PST","GMT","PDT","AST" and it is giving me the correct time in required format.

Edit: Didn't saw james already posted this

        java.util.Date date=new java.util.Date();
        String tmz="PDT";
        String newTime=GetTimeString(null, date, tmz);
        System.out.println(date+".......... After "+tmz+": "+newTime);

Fri Mar 22 09:23:19 GMT-06:00 2013.......... After EDT: 03/22/2013 15:23:19
Fri Mar 22 09:24:22 GMT-06:00 2013.......... After CDT: 03/22/2013 15:24:22
Fri Mar 22 09:25:01 GMT-06:00 2013.......... After PDT: 03/22/2013 15:25:01

Look at all newTime after conversion, they are not correct.

Check if you are taking daylight saving into consideration as i am not seeing that in your code.

Maybe your problem is with those particular 3 letter codes.Have you tried "UTC","PST","GMT","PDT","AST" ?

The JavaDoc for TimeZone says:

Three-letter time zone IDs
For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. However, their use is deprecated because the same abbreviation is often used for multiple time zones (for example, "CST" could be U.S. "Central Standard Time" and "China Standard Time"), and the Java platform can then only recognize one of them.

    TimeZone theTmz=TimeZone.getTimeZone(tmz);
    System.out.println("DST enabled: "+theTmz.inDaylightTime(date));

    Those two lines of codes are added and return "false". What's wrong with "EDT", "CDT" and "PDT"?

What's wrong with "EDT", "CDT" and "PDT"?

See previous post!

TimeZone theTmz=TimeZone.getTimeZone("PDT");
System.out.println(theTmz.getID());  ->  "GMT" // GMT is used when the TimeZone is invalid

It seems the JavaDoc means exactly what it says. You should be using proper time zone ID's. You can get a list with System.out.println(Arrays.toString(TimeZone.getAvailableIDs()));

http://docs.oracle.com/javase/7/docs/api/java/util/TimeZone.html

That means if you give improper(not supported) timezone ,then it will convert to GMT

 GregorianCalendar calendar2 = new GregorianCalendar(TimeZone.getTimeZone("PDT"),
                Locale.US);
            System.out.println(calendar2);

Output:-

java.util.GregorianCalendar[time=1363967913227,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2013,MONTH=2,WEEK_OF_YEAR=12,WEEK_OF_MONTH=4,DAY_OF_MONTH=22,DAY_OF_YEAR=81,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=3,HOUR_OF_DAY=15,MINUTE=58,SECOND=33,MILLISECOND=227,ZONE_OFFSET=0,DST_OFFSET=0]

What are correct timezone ids for "EDT", "CDT", "MDT", "PDT" and "MST" in US?

Look in the list of valid IDs using the code I posted earlier.

Got it. Are those IDs for standard time only? If yes, how can I handle Daylight Saving Time of each timezone?

A TimeZone definition includes the DST. Did you read the API doc - it explains all that.

I have read the JavaDoc. Still not sure how to differentiate Standard Time from Daylisht Saving Time. Please advice.

You specify the time zone (a geographical region), Java uses the appropriate time for that place, automatically adjusting for DST as specified in the DST info for that time zone. You don't need to do anything about it - it's automatic, based on the relevant dates.

Can I have the flexibility to specify the timezone for Standard and DST?

That question makes no sense. You have misunderstood what a time zone is. DST is not a timezone, it is part of the definition of a time zone. Here's an example from the API doc

you can get a U.S. Pacific Time TimeZone object with:
TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");

You seem to think that Los Angeles somehow changes its timezone every spring and autumn? The America/Los_Angeles timezone includes daylight saving time in the summer. The TimeZone object knows all about that. What flexibility are you asking for? The short answer is "no, you do not have the flexibility" because the governemnt has defined how DST works for America/Los_Angeles, its not up to you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.