Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

datetime conversion

 
Thomas Byrne
Greenhorn
Posts: 16
Mac OS X Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
Mac OS X Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic