-4

How to convert string to date in us time zone format in java .I am trying to convert string to date in us time zone format but it is always taking IST format

TimeZone tz = TimeZone.getTimeZone("US/Alaska");
SimpleDateFormat sdf =  new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");

String date="Sun, 9 Mar 2014 02:00:00 EST";

Date d = null;
try {
  d = sdf.parse(date);
} catch (ParseException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}
System.out.println("date is"+d);
boolean inDs = tz.inDaylightTime(d);
System.out.println("inDs"+inDs);

while printing date Mar 09 12:30:00 IST 2014

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
mohan
  • 13,035
  • 29
  • 108
  • 178
  • Your code throws `java.text.ParseException: Unparseable date: "Sun, 9 Mar 2014 02:00:00 EST"`. –  Jun 19 '14 at 13:28
  • See the out put properly and then post..... – Nitul Jun 19 '14 at 13:32
  • @Nitul can you tell what is problem – mohan Jun 19 '14 at 13:35
  • possible duplicate of [Format date in java](http://stackoverflow.com/questions/4772425/format-date-in-java) – JonK Jun 19 '14 at 13:37
  • @LutzHorn try with `Locale.ENGLISH` as 2nd paremeter to sdf, since if you are in a European locale then "Sun" is not valid... – vikingsteve Jun 19 '14 at 13:37
  • 5
    A `Date` object has no concept of a time zone. What you are seeing is the default `toString()` output for a `Date`, which just outputs the date using your local time zone. – GriffeyDog Jun 19 '14 at 13:43
  • possible duplicate of [Java string to date conversion](http://stackoverflow.com/questions/4216745/java-string-to-date-conversion) – Basil Bourque Jun 19 '14 at 16:31

1 Answers1

0

Use Calendar instead of Date. Anyways the below solution of converting a date to Calendar and back to Date is not efficient. If you can get the day, month, year etc. seperately, its better to use cal.set()

    TimeZone tz = TimeZone.getTimeZone("US/Alaska");
    SimpleDateFormat sdf = new SimpleDateFormat(
            "EEE, d MMM yyyy HH:mm:ss z");

    String date = "Sun, 9 Mar 2014 02:00:00 EST";

    Date d = null;
    try {
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(date));
        cal.setTimeZone(tz);
        System.out.println("date is " + cal.getTime());
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Dinal
  • 661
  • 4
  • 9