Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes help getting user supplied dates to display Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help getting user supplied dates to display" Watch "help getting user supplied dates to display" New topic
Author

help getting user supplied dates to display

Giles Harney
Greenhorn

Joined: Jun 22, 2004
Posts: 19
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

Joined: Mar 03, 2004
Posts: 101
Giles Harney
Greenhorn

Joined: Jun 22, 2004
Posts: 19
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

Joined: Jun 08, 2004
Posts: 118
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help getting user supplied dates to display
 
Similar Threads
problem with toString() in Sun ONE Studio 4, update 1
Help with servlets
New Objects keeping old values
PROBLEM WITH DISPLAYING CONTENTS OF HQL QUERY
calculating number of days