This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Calendar Object to String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calendar Object to String" Watch "Calendar Object to String" New topic
Author

Calendar Object to String

RoshaniG Gopal
Ranch Hand

Joined: May 15, 2006
Posts: 180
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.

Calendar calendarDepartureDT=Calendar.getInstance();
calendarDepartureDT=segment.getDepartureDateTime();


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
//Some extra code.
//Calendar calendarDepartureDT=Calendar.getInstance();
//calendarDepartureDT=segment.getDepartureDateTime();

//Try this.
Calendar 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
Hi Jaime,
Thanks for your reply. But when i run the following program, i get the correct response.

import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.*;

public class CalendarExample1 {

private static void prt(String s) {
System.out.println(s);
}

private static void prt() {
System.out.println();
}

private static void doSimpleDateFormat() {
prt("SIMPLE DATE FORMAT");
prt("=================================================================");

// Get today's date
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

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
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
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.

Calendar calendarDepartureDT=Calendar.getInstance();
calendarDepartureDT=segment.getDepartureDateTime();

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.

String strDate=formatter.format(calendarDepartureDT.getTime());
RoshaniG Gopal
Ranch Hand

Joined: May 15, 2006
Posts: 180
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);
---------------
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calendar Object to String
 
Similar Threads
Problem in Changing Timezone (URGENT)
Converting from GMT to Local Timezone using two seperate fields (date & time).
Getting problem in SimpleDateFormate
Java Patterns
Convert Calendar object to Date object