java
java

Reputation: 481

How to convert Date using SimpleDate Format?

In my Java application I am using Mysql and it returns date in format as yyyy-MM-dd hh:mm:ss. Though I need that in format dd-MM-yyyy hh:mm a. The conversion does happen in an Hibernate VO class.

For this purpose I am doing like that:

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "CREATED_DATE")
private Date createdDate = new Date();

public Date getCreatedDate() {
    Date date = null;
    try {
        date = new SimpleDateFormat("dd/MM/yyyy hh-mm a")
                .parse(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(createdDate));
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return date;
}

public void setCreatedDate(Date createdDate) {
    this.createdDate = createdDate;
}

But it does give a Parse exception.

java.text.ParseException: Unparseable date: "2013-05-06 01:39:00"
    at java.text.DateFormat.parse(DateFormat.java:354)
    at com.sits.ec.valueObjects.MessageBoxVO.getCreatedDate(MessageBoxVO.java:64)

Can anyone give me an advice?

Upvotes: 2

Views: 791

Answers (7)

A.R.H
A.R.H

Reputation: 391

Try This One:

String time=new SimpleDateFormat("yyyy.MM.dd").format(Calendar.getInstance().getTime());

Upvotes: 1

Marco Forberg
Marco Forberg

Reputation: 2644

return createdDate; is all you need.

There is no point in formatting a Date object to a String and then parse that String back to a Date object.

Btw it would work if you used the same format pattern for the parse and format operation.

Update: If you want the Date as formatted String you can use this:

public String getCreatedDateAsString() {
    return new SimpleDateFormat("dd-MM-yyyy hh:mm a").format(createdDate);
}

java.util.Date merely represents a single millisecond in time since 1.1.1980 GMT and knows absolutely nothing about time formats and things like that.

Upvotes: 0

Marko Topolnik
Marko Topolnik

Reputation: 200168

This is what is fundamentally wrong with your code: it accepts a Date and returts a Date. Date has nothing to do with strings, it represents an absolute instant in time. If you at least got the outside API right, we may be able to propose an implementation.

Upvotes: 1

Obl Tobl
Obl Tobl

Reputation: 5684

Try only
String dateString = new SimpleDateFormat("dd-MM-yyyy hh:mm a").format(createdDate);
There is nothing more needed.

Upvotes: 0

darijan
darijan

Reputation: 9785

Because you are trying to parse a String representation of yyyy-MM-dd hh:mm:ss date format into this dd/MM/yyyy hh-mm a date format. How do you expect that to work?

Representation must match.

Upvotes: 0

maqjav
maqjav

Reputation: 2434

Try with:

import java.util.*;
import java.text.*;

public class Example {
    public static void main(String[] args) {
        Date now = new Date();
        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy hh:mm a")
        String s = df.format(now);
        System.out.println("Today is: " + s);
    }
}

Upvotes: 1

Rahul
Rahul

Reputation: 45070

Firstly, Why would you format a date and parse it again to get back the date object? You could directly use the actual date object as such.

Secondly, you're formatting a date with format1 and then trying to parse that with format2, which is not the same as format1. Hence, the ParseException.

This is what you need.

public Date getCreatedDate() {
    return createdDate;
}

Upvotes: 0

Related Questions