0

The code is as mentioned below:

public static void main(String[] args){
    Date date = new Date();
    DateFormat dateFormat= new SimpleDateFormat("dd-MMM-yyy");

    try{
        Date formattedDate = dateFormat.parse(date.toString());
        System.out.println(formattedDate.toString());
    }catch(ParseException parseEx){
        parseEx.printStackTrace();
    }
}

In code above, dateFormat.parse(date.toString()) is throwing unparseable date exception: Unparseable date: "Mon Jan 28 18:53:24 IST 2013

I am not able to figure out the reason for it.

1
  • Are you trying to get the string representation of your date in the given format? Commented Jan 28, 2013 at 13:27

3 Answers 3

5

Format the java.util.Date instance into String using SimpleDateFormat.format(java.util.Date)

Date date = new Date();
DateFormat dateFormat= new SimpleDateFormat("dd-MMM-yyy");

try {
    Date formattedDate = dateFormat.parse(dateFormat.format(date));
    System.out.println(formattedDate.toString());

} catch (ParseException parseEx) {
   parseEx.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

Comments

4

Why would you want to convert a date to a string and parse it back to a date?

The reason your code fails is because you are trying to convert a full date with a formatter which only accepts dates in the dd-MMM-yyy-format.

3 Comments

I wanted to check if formmattedDate.toString() will return a full date or date in dd-MMM-yyyy format.
@kurt: I am facing same issue as you mentioned. I have to convert a full date: "2/18/2016 10:55:16 AM" into a format "EEE MMMMM dd, yyyy". It is giving me unaparseable Unparseable date Exception. What is the workaround?
If you have 'T' in date format (e.g. "2018-01-31T16:01:49") use this date format DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")
2
public static void main(String[] args) throws ParseException {

    Date date = new Date();
    DateFormat dateFormat = new SimpleDateFormat(
            "EEE MMM d HH:mm:ss Z yyyy");

    Date formattedDate = dateFormat.parse(date.toString());
    System.out.println(formattedDate);

}

This is what you exactly want to do ...yes?

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.