aspose file tools*
The moose likes Beginning Java and the fly likes conversion from Date ojbect to Date object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "conversion from Date ojbect to Date object" Watch "conversion from Date ojbect to Date object" New topic
Author

conversion from Date ojbect to Date object

vipul bondugula
Ranch Hand

Joined: Oct 14, 2010
Posts: 218
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.


Thanks
Vipul Kumar
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

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?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
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

Joined: Jul 26, 2007
Posts: 10535
    
    9

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

Joined: Oct 14, 2010
Posts: 218
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

Joined: Jul 26, 2007
Posts: 10535
    
    9

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

Joined: Aug 22, 2008
Posts: 193

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.


Do not wait to strike till the iron is hot; but make it hot by striking....
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1031
    
    5

Never ever store a Date in a database as a String.
vipul bondugula
Ranch Hand

Joined: Oct 14, 2010
Posts: 218
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

Joined: Aug 16, 2005
Posts: 14435
    
  23

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1031
    
    5

http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1031
    
    5

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

Joined: Dec 08, 2010
Posts: 1512
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
vipul bondugula
Ranch Hand

Joined: Oct 14, 2010
Posts: 218
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

Joined: Oct 14, 2010
Posts: 218
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

Joined: Dec 08, 2010
Posts: 1512
    
    5

You are welcome.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: conversion from Date ojbect to Date object