The GregorianCalendar constructor asks for the following:
GregorianCalendar(int year, int month, int dayOfMonth);
How can I extract the year, month, and day from an object I have created. Right now I'm using object.YEAR, object.MONTH, and object.DAY_OF_MONTH, but that does not seem to be giving me the right numbers.
Thanks.
Here I get a date based on which calendar date a user clicks. The user can then enter some information into that date which is stored in a HashMap keyed on GregorianCalendar.
cal.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
selectedDate = new GregorianCalendar(year, month, dayOfMonth);
Here I am trying to write the date from the GregorianCalendar year, month, and day parameters to a file to be used later.
private void writeToFile() {
try
{
PrintWriter writer = new PrintWriter(file, "UTF-8");
for (GregorianCalendar gregObject: recipes.keySet()) {
String[] value = recipes.get(gregObject);
int y = gregObject.YEAR;
int m = gregObject.MONTH;
int d = gregObject.DAY_OF_MONTH;
writer.printf("%d %d %d ", y, m, d);
Here is how I read from the file. When I read from the file I get the numbers 1,2,5 for year, month, and date which is wrong. The rest of the information read is correct.
try
{
Scanner getLine = new Scanner(file);
Scanner tokenizer;
while (getLine.hasNextLine()) {
String line = getLine.nextLine();
tokenizer = new Scanner(line);
System.out.println(line);
while (tokenizer.hasNextInt()) {
int y1 = tokenizer.nextInt();
int m1 = tokenizer.nextInt();
int d1 = tokenizer.nextInt();
Obviously I think I am writing the year, month, and day wrongly to the file, so I'm trying to figure out the correct way to extract the year, month, and day from a GregorianCalendar object.