Quick Background:
I have created a GUI for a reservation application in Eclipse. On it, I have multiple combo boxes for arrival and departure dates (month, day, year - for both). I have retrieved the information and tied it to a String variable so it appears as something like this: 12/04/2014.
The Questions:
- What is a good way to convert that string (arrival/departure) to Date datetype for comparison purposes? I will need to verify the arrival is not before today's date and get the length of stay.
- What is a good way to retrieve today's date like so - 2/19/2014.
I have been searching the internet for quite a while but can't seem to find anything clear.
Please do not recommend any add-ons or interface tools as I must code this all by hand with only what is readily available (if that makes sense).
Thank you!
--- EDITS ---
I have tried many of the things mentioned below ...
'Date today = new Date()' gives me a red line under new Date() asking for an argument of int or long.
Below is the code I have when I assemble my date from the combo boxes. dateFormat.parse(holder) gets a red underline prompting to add throws declaration or surround with try/catch. The datatypes for the variables below are ... index (int). holder (String). arrival/departure (java.util.Date) -- for whatever reason it won't accept Date even with that imported, although it does in my Hotel class. Strange, yes, but not my main focus.
// Assignments index = view.getArriveMonthIndex() + 1; // Arrival date holder = index + "/" + view.getArriveDay() + "/" + view.getArriveYear(); arrival = dateFormat.parse(holder); index = view.getDepartMonthIndex() + 1; // Departure date holder = index + "/" + view.getDepartDay() + "/" + view.getDepartYear(); departure = dateFormat.parse(holder);
Something else to perhaps mention/show .. at the very beginning of my class, this is a bit of what I have been playing around with/tried.
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
Calendar todaysDate = Calendar.getInstance();
String today = dateFormat.format(todaysDate.getTime());
^^ Just tested this last part, it works.
I went ahead with the underlined suggestions for my arrive/departure variables. My method now throws a ParseException. Haven't been able to locate a good description for this - what exactly is its point? (very much new if you couldn't tell) -- BACKUP. Changing that back to how it is above. This way requires a throw/catch around my call to the reserveHotel() method.