• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

conversion from Date ojbect to Date object

 
Ranch Hand
Posts: 253
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 John
Ranch Hand
Posts: 253
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never ever store a Date in a database as a String.
 
vipul John
Ranch Hand
Posts: 253
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 John
Ranch Hand
Posts: 253
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 John
Ranch Hand
Posts: 253
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic