0

Actually I wanted to convert one String to date and then I need to format it in to one another format..

The String I have is

 String val="Wed Jan 08 08:49:13 GMT+05:30 2014";

To convert it to

 2014-01-30 10:14:18  , this format

Is it possible to do this I tried some method like

       SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
        String formattedDate = df.format(c.getTime());

        Calendar cal =   Calendar.getInstance(TimeZone.getTimeZone("GMT+1:00"));
          // Date currentLocalTime = ;
            DateFormat date = new SimpleDateFormat("HH:mm:ss:mmss a"); 

But nothing worked

4
  • why not use joda-time? Commented Feb 6, 2014 at 13:30
  • Telling us that nothing worked is not helpful. Tell us how and why it didn't work. Commented Feb 6, 2014 at 13:30
  • 1
    If you have a date string, you need to parse it into a Date before you can format it differently. Commented Feb 6, 2014 at 13:30
  • Of course it is possible but I don't see how your code is gonna do that. For date parsing I recommend Joda Time, which parses rather well. Anyway, how about writing a JUnit test case or two? Commented Feb 6, 2014 at 13:32

1 Answer 1

3

Your format string for parsing doesn't match the example date you have given.

You could try something like the following:

    String val = "Wed Jan 08 08:49:13 GMT+05:30 2014";
    DateFormat inFmt = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
    Date date = inFmt.parse(val);
    DateFormat outFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    outFmt.setTimeZone(TimeZone.getTimeZone("GMT+1:00"));
    System.out.println(outFmt.format(date));

Note the following correspondences in the first two lines:

  • Wed -> EEE
  • Jan -> MMM
  • 08 -> dd
  • etc.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.