This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting Date to String in java

 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi,

In my application am getting list of dates from database.. Now i want to convert those dates into Strings. I wrote the code like this. Is this correct? It is giving me the exception.



please suggest me where i am doing wrong? Thanks
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there something wrong with the code - does it not return the output you expect?

One thing you could improve: Currently you are creating a new SimpleDateFormat inside the loop (line 7), so a new SimpleDateFormat object will be created for each iteration. That's not necessary. Just create it once, outside the loop, and reuse it inside the loop.

DateFormatUtils is not a standard Java class (it's probably a class somewhere else in your own project), so I can't say anything about that.
 
Campbell Ritchie
Sheriff
Posts: 48448
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using a List and a Set separately looks peculiar. You could used a linked set, which you can read about here, which does both. You are not actually creating a List of dates, but a set ordered by insertion.
You appear to be creating a String and never using it. You can probably delete that part and not notice any difference.
 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It is giving me the exception "java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date"
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On which line?

I'm assuming that code isn't quite what you're running (or there's some missing) - you've got the declaration of oldDate missing, but you still refer to it. And you declare lstOfNewDates but then use lstOfNewDriveDates.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which line do you get that error? By the way, the code as you presented it above does not compile, because oldDate is not defined (you're using it in lines 9 and 10, and declaring it in line 6, but that line is commented out). Please show your exact code that produces the ClassCastException when you run it, otherwise it's really hard to tell why you get that error.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vidya Gupta wrote:
It is giving me the exception "java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date"


The error message is telling you exactly what you're doing wrong. It's hard to get much more explicit than that.

You can't just take a date and magically say "Okay, now it's a String." That's not how casting works. You can't change one type of object into another type of object. If you want a String representation of the Date, you can either call its toString() (if you don't need a particular format), or you can use SimpleDateFormat (if you do need a particular format). Note that in both cases, the Date object is not changed in any way. It's still just a Date. A new String object is created based on the the Date's contents.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic