aspose 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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13869
    
  10

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: 36478
    
  16
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: 4240
    
    7

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

Joined: Aug 16, 2005
Posts: 13869
    
  10

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: 6109
    
    6

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://aspose.com/file-tools
 
subject: Converting Date to String in java
 
Similar Threads
how to change the date,Time format after accessing it from the database
Unparseable date exception
JavaScript Date Format Conversion
Date format
date format conversion to MM/dd/yyyy hh:mm a