ram
ram

Reputation: 3507

String to Date format conversion problem

In my app I am converting the date string which is in CST format (Thu jun 03 10:00 CST 2011) when I am converting into the date object it is converting it to the current time zone like Pacific Asia etc, but I want the same string which is given I don't want any time zone changes.

Thanks, balu.

Upvotes: 0

Views: 162

Answers (2)

Mandel
Mandel

Reputation: 2988

Use the code given below. The use of locale is optional as it controls the language in which the date string appears. The important piece is the correct use of TimeZone. Try the code with different settings for Locale and TimeZone to see how the output changes. For example if you change Locale to: Locale('fr','FR') the program will output: 13 juillet 2011 08:05:31 PDT instead of July 13, 2011 8:05:52 AM PDT. Notice that the timezone is still PDT as we have not changed the TimeZone.

    Locale locale = new Locale("en","US");
    TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");

    Calendar date = Calendar.getInstance(tz,locale);

    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG,locale);
    df.setTimeZone(tz);

    System.out.println(df.format(date.getTime()));

Upvotes: 0

OceanBlue
OceanBlue

Reputation: 9248

Are you using the correct Locale object for CST?

"Note that there are many convenience methods that automatically use the default locale..."

Upvotes: 1

Related Questions