HI all, I want to change a calendar object to a String. I am getting a Calendar from a bean and I want to change it to "ddMMM" date format adn then convert it to String. I am using the following code but it gives me error cannot cast from Calendar to Date.. How can i achieve it. Please help me.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); Date strDate1=calendarDepartureDT; String strDate=formatter.format(calendarDepartureDT);
Date date = formatter.parse(strDate1); String strCorrectedDate = new SimpleDateFormat("ddMMM").format(date);
Please help..
Regards,<br />Roshani
Jaime M. Tovar
Ranch Hand
Joined: Mar 28, 2005
Posts: 133
posted
0
//Some extra code. //Calendar calendarDepartureDT=Calendar.getInstance(); //calendarDepartureDT=segment.getDepartureDateTime();
//This is the cast error //Date strDate1=calendarDepartureDT; //Do something like this. Date strDate1=calendarDepartureDT.getTime();
//Does not make sense Date date = formatter.parse(strDate1); //strDate1 is already a Date
Also i think there is an error in your logic. formatter has the format "yyyy-MM-dd'T'HH:mm:ss" that is not compatible with "ddMMM" if you want to parse an already formated text you have to use the same format for both.
She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
RoshaniG Gopal
Ranch Hand
Joined: May 15, 2006
Posts: 180
posted
0
Hi Jaime, Thanks for your reply. But when i run the following program, i get the correct response.
try{ // String strDate="2007-01-09T05:30:00"; String strDate=""; Date now = Calendar.getInstance().getTime(); prt("Now is :"+now); strDate=formatter.format(now); prt("After formatting now is:"+now);
Date date = formatter.parse(strDate); String strCorrectedDate = new SimpleDateFormat("ddMMM").format(date); prt(" Corrected Date: " +strCorrectedDate); } catch(Exception e) { prt("There was an excption" +e); }
// prt(" It is now : " + formatter.format(now.getTime())); prt(); }
public static void main(String[] args) { prt(); doSimpleDateFormat(); }
} ------------- Secondly, Date strDate1=calendarDepartureDT.getTime(); will fetch the current time for the calendar but i want the time that has been set in the bean.
Thanks in advance.
Stephen Svoboda
Greenhorn
Joined: Aug 13, 2006
Posts: 2
posted
0
I don't now anything about beans, but if you want the current date, I would use
rather than getting the time of a Calendar.
Jaime M. Tovar
Ranch Hand
Joined: Mar 28, 2005
Posts: 133
posted
0
That�s because I used your code as an example. Here you assign the Calendar.getInstance() to your bean, but I don�t have your bean (segment) code then I don�t know if it holds a calendar or a date.
If you want to format the Calendar date you have to pass it as a Date object to the SimpleDateFormat object. That is by calling the getTime() method of your Calendar object. Either if it comes from the bean or it is declared locally.
Hi Jamie, Thanks for all your answers.. It was a silly mistake i was doing. I just a toString() on the Cal and it was done.. Segment returned a Calendar. I used the follwoing and got it working. Thanks for your time. ------------------------- Calendar calendarDepartureDT=segment.getDepartureDateTime(); String strDate=calendarDepartureDT.toString(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); Date date = formatter.parse(strDate); String strCorrectDepartureDate = new SimpleDateFormat("ddMMM").format(date); ---------------