• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

conversion from Date ojbect to Date object

 
vipul bondugula
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

i have a requirement. convert Date object to formatted Date object.
i mean


output will be Thu Apr 05 11:28:32 GMT+05:30 2012..

i want output to be like 05/APR/2012.

and the output object must be Date and not String.

If at all you are not clear , i'll post more clearly

Thank You,
Vipul Kumar.
 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the API docs for SimpleDateFormat

vipul bondugula wrote:and the output object must be Date and not String.

Which output are you talking about here?
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain better what you want. If you're talking about output (for display on the screen or wherever), then you ARE talking about String output. So, why should the output object NOT be a String?

For Date to String conversion, you simply use a DateFormat, like this:


String to Date conversion is also possible, by using the parse() method of the DateFormat.
 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D. Ogranos
As much as your help is appreciated and valuable, please do not post complete solution/code because
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
 
vipul bondugula
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks For the reply,
If you're talking about output (for display on the screen or wherever), then you ARE talking about String output. So, why should the output object NOT be a String?

The Date output i had to insert into database.

i have to set the formateed Date ouptut to a Pojo object which has date of birth field of type Date and insert into database.

i can set it to String. But the pojo has dependencies problem..

Thanks.
 
Maneesh Godbole
Saloon Keeper
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vipul bondugula wrote:
i have to set the formateed Date ouptut to a Pojo object which has date of birth field of type Date and insert into database.

If you mean you are going to save the date as string in your DB, ....then dont.
Always save it as a Date in your DB (model) and use the formatter to convert it to locale specific string (view)
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are going to store the Date object into the database then does it really bother you to think about the formatting or this object.
Formatting matter only for the display purpose so that user can easily evaluate the value of something.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never ever store a Date in a database as a String.
 
vipul bondugula
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies.

I am not storing Date as String in the database. I am storing the Date as Date only..

Actually the Date is inserted in the below format..
Thu Apr 05 11:28:32 GMT+05:30 2012

I want date to be in dd/MMM/yyyy format..


thank you.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15452
42
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vipul bondugula wrote:i have a requirement. convert Date object to formatted Date object.

and the output object must be Date and not String.

What you want is impossible. Date objects do not have a format by themselves. A "formatted Date object" does not exist.

When you print a Date object, you have to convert it to a string, and you do that by formatting it. But the format is a property of the SimpleDateFormat object that you use, not of the Date object itself. A Date is just a Date. It doesn't have a format by itself.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and the output object must be Date and not String.


Can you define what you mean by the word output here? Output to what?
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vipul bondugula wrote:Actually the Date is inserted in the below format..
Thu Apr 05 11:28:32 GMT+05:30 2012

I want date to be in dd/MMM/yyyy format..

Again, please check last reply by Maneesh Godbole. To me, that approach seems the most practical one.

Store the date in DB as date itself (i.e. in whatever format db stores it) and while displaying it, just forma it. That way, you can still do date related operations at db level.

If you store date as string, what format will you choose? And what if that format changes in future? And how will you do date related operation (say to query data which went after jan 2012 or something like that?)

I hope this helps.
 
vipul bondugula
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
vipul bondugula wrote:i have a requirement. convert Date object to formatted Date object.

and the output object must be Date and not String.

What you want is impossible. Date objects do not have a format by themselves. A "formatted Date object" does not exist.

When you print a Date object, you have to convert it to a string, and you do that by formatting it. But the format is a property of the SimpleDateFormat object that you use, not of the Date object itself. A Date is just a Date. It doesn't have a format by itself.


Thanks for your explanation and for your time...
 
vipul bondugula
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:
vipul bondugula wrote:Actually the Date is inserted in the below format..
Thu Apr 05 11:28:32 GMT+05:30 2012

I want date to be in dd/MMM/yyyy format..

Again, please check last reply by Maneesh Godbole. To me, that approach seems the most practical one.

Store the date in DB as date itself (i.e. in whatever format db stores it) and while displaying it, just forma it. That way, you can still do date related operations at db level.

If you store date as string, what format will you choose? And what if that format changes in future? And how will you do date related operation (say to query data which went after jan 2012 or something like that?)

I hope this helps.



Thanks you for your suggestion.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vipul bondugula wrote:Hi Friends,

i have a requirement. convert Date object to formatted Date object.
i mean


output will be Thu Apr 05 11:28:32 GMT+05:30 2012..

i want output to be like 05/APR/2012.

and the output object must be Date and not String.


Not possible. Dates don't have formats. Only Strings do.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic