• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Converting Date to String in java

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It is giving me the exception "java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date"
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
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

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.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic