Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help getting user supplied dates to display

 
Giles Harney
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I go about getting the arrival and departure date in the toString method to pass back to the driver class for display? (format example would be "Wednesday, May 15, 2002")


-------------

import java.util.GregorianCalendar;
import java.text.NumberFormat;

public class Reservation // class declaration "Reservation".
{ // begin class
GregorianCalendar arrivalDate;
GregorianCalendar departureDate;
private long totalDays;
double totalPrice;
final double NIGHTLY_RATE = 115.00;
NumberFormat currency = NumberFormat.getCurrencyInstance();

/**
* Default constructor
*/
public Reservation()
{
totalDays = 0;
totalPrice = 0;
}

/**
* Reservation constructor. Arrive date and departure date supplied.
*/
public Reservation(GregorianCalendar arriveDate, GregorianCalendar departDate)
{
arrivalDate = arriveDate;
departureDate = departDate;

setTotalDays(arriveDate, departDate);
setTotalPrice();
}


/**
* Method
*
*/
public void setTotalDays(GregorianCalendar arriveDate, GregorianCalendar departDate)
{
long arriveDateMillis = arriveDate.getTimeInMillis();
long departDateMillis = departDate.getTimeInMillis();
long daysInMillis = departDateMillis - arriveDateMillis;
long oneDayInMillis = 1000 * 60 * 60 * 24;
totalDays = daysInMillis / oneDayInMillis;
}



/**
* Method
*/
public void setTotalPrice()
{
totalPrice = totalDays * NIGHTLY_RATE;
}



/** Method
*/
public double getTotalPrice()
{
return totalPrice;
}

/** Method
*/
public long getTotalDays()
{
return totalDays;
}


/**
* Override toString
* @return a <code>String</code> specifying the.
*/
public String toString()
{

String reservationData = "Arrival Date: " ??? + "\n"
+ "Price: " + currency.format(NIGHTLY_RATE) + " per night" + "\n"
+ "Total price: " + currency.format(totalPrice) + " for " + totalDays + " nights" + "\n \n";

return reservationData;
}

} // end class
 
Jean-Francois Briere
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Giles Harney
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks!

the only thing is that the output is displaying one month fwd of the entered month... must be something do to with jave using 0 as January...

I am thinking I need to use (Calendar.MONTH) somewhere, but dont know where.

???
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure why they decided that months were going to start with 0, but yeah that's all it is. Just decrement your month value by one I guess.

edit: That's how the GregorianCalendar keeps them, so if you put in month 12, it would spill into the next year anyway. You need to STORE them as 0-11.
[ July 09, 2004: Message edited by: Darin Niard ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic