File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Converting Date to String in java Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Converting Date to String in java" Watch "Converting Date to String in java" New topic
Author

Converting Date to String in java

Vidya Gupta
Ranch Hand

Joined: Mar 18, 2012
Posts: 96


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
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
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

Joined: Mar 18, 2012
Posts: 96

It is giving me the exception "java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date"
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 3786
    
    1

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
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

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

Joined: Jan 03, 2004
Posts: 5783
    
    5

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.

 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Converting Date to String in java
 
Similar Threads
date format conversion to MM/dd/yyyy hh:mm a
Date format
Unparseable date exception
how to change the date,Time format after accessing it from the database
JavaScript Date Format Conversion