-5

I am facing the problem while converting the date:

Current format is:Thu Sep 05 12:07:46 IST 2013(dow mon dd hh:mm:ss zzz yyyy)

I need to convert in to:09/04/2013 11:38 PM PDT(mm/dd/yyyy hh:mm a zzz)

But i am not able to convert.

Vignesh Vino
  • 1,242
  • 4
  • 25
  • 50
Subbarao Gaddam
  • 131
  • 1
  • 3
  • 13
  • 5
    What have you already tried? – agad Sep 05 '13 at 07:52
  • Did you even try using or going through SimpleDateFormatter?! – pranky64 Sep 05 '13 at 07:55
  • This has been asked a lot just search StackOverflow , to start with take a look at [SimpleDateFormat](http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html) – AurA Sep 05 '13 at 07:57
  • refer to this SO question: http://stackoverflow.com/questions/3469507/how-can-i-change-the-date-format-in-java – r3ap3r Sep 05 '13 at 08:00

4 Answers4

0

Try using SimpleDateFormatter. You have to tell it the input/output format, you can do that based on this description (you can also find a few common examples there).

The code will be something like this:

try {
    String input = "Thu Sep 05 12:07:46 IST 2013";
    DateFormat formatter = new SimpleDateFormat("I leave this to you :-)))");
    System.out.println(formatter.parse(input));
} catch (ParseException e) {
    e.printStackTrace();
}

Hope that helps.

rlegendi
  • 10,466
  • 3
  • 38
  • 50
0
   try {
          DateFormat dffrom = new SimpleDateFormat("E MMM dd hh:mm:ss zzz yyyy");
          DateFormat dfto = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a zzz");  
          Date date = dffrom.parse("Thu Sep 05 12:07:46 IST 2013");     
          String s = dfto.format(date);
          System.out.println(s);
   } catch (ParseException e) {

   }

OutPut

09/05/2013 00:07:46 AM IST

update

   try {
        DateFormat dffrom = new SimpleDateFormat("E MMM dd hh:mm:ss zzz yyyy");
        DateFormat dfto = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a zzz");
        TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
        dfto.setTimeZone(zone);
        Date date = dffrom.parse("Thu Sep 05 12:07:46 IST 2013");       
        String s = dfto.format(date);
        System.out.println(s);
   } catch (ParseException e) {

   }

output

09/04/2013 11:37:46 AM PDT

Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
0

You can do this

TimeZone tz = TimeZone.getTimeZone("PST8PDT"); // example
 // required format. Remember M is for month, m for miniute
DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a zzz");
df.setTimeZone(tz);
String text = df.format(new Date());// current time
System.out.println(text);

Also please check this TimeZones in Java

Community
  • 1
  • 1
vels4j
  • 11,208
  • 5
  • 38
  • 63
0

You try to convert dateformat and timeZone as well, so you need to convert the timezone in your code.

SimpleDateFormat sf = new SimpleDateFormat("dow mon dd hh:mm:ss zzz yyyy");
isoFormat.setTimeZone(TimeZone.getTimeZone("PDT"));
Date date = isoFormat.parse("mm/dd/yyyy hh:mm a zzz");

this may help you.

Pradip
  • 3,189
  • 3
  • 22
  • 27