0

I have a date of the format EEE MMM dd HH:mm:ss zzz yyyy

I have to convert this to dd/MM/yyyy

I did the following:

SimpleDateFormat fmt = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
SimpleDateFormat fmtddMMyyyy = new SimpleDateFormat("dd/MM/yyyy");
java.util.Date d = fmt.parse("Mon May 28 00:00:00 IST 2012");<br>
String formattedDate = fmtddMMyyyy.format(d); 

When I tried to print d it displays 0027833001988071567.

Where am I going wrong?

9
  • 3
    Show us the string you're trying to parse. Commented May 28, 2012 at 13:33
  • What are you using the fmtddMMyyyy for? I don't see any conversion happening anywhere? Commented May 28, 2012 at 13:35
  • 1
    Wow, a mutating question. To clean up this mess: print the string you want to convert and show us the result. Then convert it. Show us the code of the conversion (i.e. everything of it). Print out every object and intermediate value. Show us the results. Make sure your question fits the code. BTW: I'm almost sure you will have solved the problem by then. Commented May 28, 2012 at 14:03
  • Hope am clear now,Thanks Commented May 28, 2012 at 14:03
  • Whenever i try to bind a dd/MM/yyyy value to java.util.date in spring it gets set in the format "EEE MMM dd HH:mm:ss zzz yyyy".Now if i try to convert this back to dd/MM/yyyy i get this problem. Commented May 28, 2012 at 14:15

2 Answers 2

2

I have to guess that your real code is only marginally connected to what you show. Due to some experiments and the documentation.

Possibly the original parsing throws an exception and what every you print has nothing to do with you date manipulation.

In order to fix the problem

  • print the string you want to convert
  • print the parsed date
  • print the converted String
  • Use the constructor variants with Locale argument so everybody on stackoverflow can reproduce it, and you application works the same wherever it is running.

All this in a simple class containing nothing but a main method which contains the code.

If the problem persists, come back with the code and its output.

With my advice partially applied you might end with this:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class Experiment {
public static void main(String args[]) throws ParseException {
    SimpleDateFormat fmt = new SimpleDateFormat(
            "EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
    SimpleDateFormat fmtddMMyyyy = new SimpleDateFormat("dd/MM/yyyy",
            Locale.US);
    java.util.Date d = fmt.parse("Mon May 28 00:00:00 IST 2012");
    String formattedDate = fmtddMMyyyy.format(d);
    System.out.println(formattedDate);
}
}

Which prints out

28/05/2012
Sign up to request clarification or add additional context in comments.

Comments

1

try:

String formattedDate = fmtddMMyyyy.format(d);

UPD: Well I got the following solution:

SimpleDateFormat fmt = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);

works fine (without locale defining wasn't working for me too). Because you define the month name and day-of-week name according to English language, not your local, as I presume.

4 Comments

It still prints "0.0027833001988071567".Thanks
go to debug mode and show us please the value of pageContext.getAttribute("ternaryDateFrom").toString()
ternaryDateDrom==Mon May 28 00:00:00 IST 2012
Thanks,I got it.Locale was the issue.Thanks

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.