-2

I need to convert a date in dd/MM/yyyy format and store it to a Date object. i tried but not getting the desired result.My code :

    Date date=new Date();
    System.out.println("Normal Date :"+date);
    DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    String strdate=formatter.format(date);
    System.out.println("String date: "+strdate);
    date=(Date)formatter.parse(strdate);
    System.out.println("Formated Date : "+date);

Current output


Normal Date :Wed Feb 18 10:54:40 IST 2015

String date: 18/02/2015

Formated Date : Wed Feb 18 00:00:00 IST 2015

Desired output


Normal Date :Wed Feb 18 10:54:40 IST 2015

String date: 18/02/2015

Formated Date : 18/02/2015

How can I achieve this?

Krish Munot
  • 1,093
  • 2
  • 18
  • 29
Dev
  • 21
  • 5
  • `System.out.println("Formated Date : "+formatter.format(date));`? – MadProgrammer Feb 18 '15 at 05:55
  • Remember, the "format" is irrelevant. It's the data that's important. `Date` is just a container for the number of milliseconds since the Unix Epoch, it carries no notion of formatting. When you want to display the date, use an appropriate `DateFormat` – MadProgrammer Feb 18 '15 at 05:57
  • @MadProgrammer, i have this constraint on the column. @Column(name="from", nullable=false, length=10). so can't store complete date. it returns an error. and i'm not autorized to change that – Dev Feb 18 '15 at 06:04
  • Then the only choice is to use something like `formatter.format(date)` and store it as text – MadProgrammer Feb 18 '15 at 06:08
  • possible duplicate of [display Java.util.Date in a specific format](http://stackoverflow.com/questions/6262310/display-java-util-date-in-a-specific-format) – Basil Bourque Feb 18 '15 at 07:39

3 Answers3

0

Date Object can be created from a specific format but when you print it, it will always print as per its .toString() implementation.

So you wont be able to achieve what you are trying to.

Sufiyan Ghori
  • 18,164
  • 14
  • 82
  • 110
Kedar Parikh
  • 1,241
  • 11
  • 18
0

Date object stores date in international standards. if you want to print the way you want use this.

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(dateFormat.format(yourdate));

possible duplicate of display Java.util.Date in a specific format

Community
  • 1
  • 1
prasadmadanayake
  • 1,415
  • 15
  • 23
  • Actually its not for just a display purpouse. I need to store it on database. @Temporal(TemporalType.DATE) @Column(name="from", nullable=false, length=10) public Date getFrom() { return this.from; } public void setFrom(Date from) { this.from = from; } The max lenth is 10 – Dev Feb 18 '15 at 05:51
  • @Dev, then store it in database. – Sufiyan Ghori Feb 18 '15 at 05:51
  • @Dev then you can store it in the database as a varchar. But I don't understand why you store it like that because then the comparison of dates in the database would be messy. it is good practice to store date as a SQL Date and whenever displaying the date, format it at will – prasadmadanayake Feb 18 '15 at 05:55
  • @Dev `Date` is just a container for the number of milliseconds since the unix epoch, it does not carry any formatting information. Simply store the `Date` value into the database as is, assuming that the column supports a date value – MadProgrammer Feb 18 '15 at 05:59
  • @prasadmadanayake , if i change type to varchar i can store it. But i'm not authorized to do so. that's why i am asking a question like this – Dev Feb 18 '15 at 06:00
  • @Dev then you should just store it as a SQL Date Object. formatting only necessary only when displaying . manipulating of the date will be handled by SQL engine. just store the date as it is and format it when displying – prasadmadanayake Feb 18 '15 at 06:12
  • then you should just store it as a SQL Date Object. formatting only necessary only when displaying . manipulating of the date will be handled by SQL engine. just store the date as it is and format it when displying – prasadmadanayake Feb 18 '15 at 06:12
0

Store it in database as sqlDate = new java.sql.Date(utilDate.getTime());

Abhi
  • 341
  • 7
  • 15