File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes datetime conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "datetime conversion" Watch "datetime conversion" New topic
Author

datetime conversion

Thomas Byrne
Greenhorn

Joined: Aug 21, 2012
Posts: 16

Hi fellows,

If I'm using code like one below to retrieve data from mysql, is there any way to convert, in the same time (inside the loop), the datetime from mysql to shorter format?
Long: Sep 18, 2012 5:24:12 PM --> to short: 18-09-2012 5:24 PM

Cheers,
Thomas
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18882
    
    8

No.

But that's because in the code you posted, the timestamp values don't have any format at all. A Date, or a Timestamp, is essentially just a number which represents the number of milliseconds since a certain instant of time. So a question about changing the format at that point is misguided. If you want that timestamp to be displayed in a particular format at some time, then you would use a suitable SimpleDateFormat to do that, at the time you're displaying it.
Thomas Byrne
Greenhorn

Joined: Aug 21, 2012
Posts: 16

Paul Clapham wrote:But that's because in the code you posted, the timestamp values don't have any format at all. A Date, or a Timestamp, is essentially just a number which represents the number of milliseconds


But in this case, in the mysql table, column type is datetime not timestamp. So here, before is written to object I can see how its formatted. And even then I cannot format it?
Cheers,
Thomas
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18882
    
    8

I don't know what you're talking about. It may be true that MySQL displays dates in various formats when it's asked to do that. It's also true (as I already said) that in Java, dates don't have a format.

Remember that your question was about Java, not about MySQL. So what MySQL might do is of no consequence. There is no need to format a Java date until you are prepared to display the result of that formatting.

This is most likely how MySQL behaves too -- when you ask to see a date value, it formats it in some way and displays the result. That doesn't mean that the date "is" formatted in that way, only that MySQL formats it that way when you ask to see it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: datetime conversion