• 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

Calendar Object to String

 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//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.
 
RoshaniG Gopal
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
---------------
 
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic