0

I'm new to java and having a really hard time understanding what I am doing wrong here. I'm still having a hard time fully understanding abstract classes, and interfaces so I think that's where my problem is coming from.

I'm trying to create a class that will allow me to create party objects, I'm able to instantiate all variables except for the date. Here is what I have so far:

public class Main {

    public static void main(String[] args) {

         ...

         Party joes = new Party("Joes party", "Joe", "Joe's Place",
                        new GregorianCalendar(2012,10,10));
         joes.printParty();
    }
}

public class Party {

    //attributes of party
    private String partyName;
    private String partyHost;
    private String location;
    private GregorianCalendar partyTime;

      public Party(String partyName, String partyHost, String location, GregorianCalendar partyTime){
        this.partyName = partyName;
        this.partyHost = partyHost;
        this.location = location;
        this.partyTime = partyTime;
      }
}

When I print this out I get:

"Joes party is going to be hosted by Joe at Joe's Place on:
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=false,lenient=true..."
matsjoyce
  • 5,744
  • 6
  • 31
  • 38
Kronos
  • 3
  • 2
  • See also http://stackoverflow.com/questions/30434334/gregoriancalendar-outputs-the-date-is-java-util-gregoriancalendartime-11415564 – Tunaki Dec 04 '16 at 21:54
  • Sorry if I'm missing something but I saw this answer and I thought that it was an output issue. I thought I was having an issue with the way the data was being passed. – Kronos Dec 04 '16 at 22:14
  • Why do you think that? – Tunaki Dec 04 '16 at 22:15
  • When I run the debugger I don't see any of the data that I am passing being stored in the partyTime variable. – Kronos Dec 04 '16 at 22:17
  • The fact that you're printing something means that it is correctly passed. You just need to format it properly with a `SimpleDateFormat` or get its time with `getTime()`, as per the linked question. – Tunaki Dec 04 '16 at 22:29
  • Got it now, the problem was that I wasn't adding the .getTime() method when I was printing out the object. Thanks for your help! – Kronos Dec 04 '16 at 22:29
  • @Kronos FYI, the `java.util.Calendar` is one of the old date-time classes that have proven to be troublesome, confusing, and flawed. Now legacy, supplanted by the [java.time](http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. Use [`ZonedDateTime`](https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html) instead. This new class has a much more sensible implementation for its `toString` method amongst its many other improvements. – Basil Bourque Dec 04 '16 at 23:21

1 Answers1

1

What is being displayed is the result of the toString() method defined in the class Calendar. This is almost never what someone wants to display when converting a date to a string, but this is how the Calendar class was defined.

Luckily, there are facilities in the Java Platform library that allow you to format your dates in exactly the form which you'd like to display ... so that's what you'll need to do. Instead of passing a reference to your GregorianCalendar instance to the print method, you'll create your own formatted String instance, using the SimpleDateFormat class for example, and then pass the reference to that string to the print method.

scottb
  • 9,908
  • 3
  • 40
  • 56
  • So the problem is not with the way that I am creating or passing the variables? It's just printing out in an unhelpful way? – Kronos Dec 04 '16 at 22:08
  • Your program is working in exactly the way you have defined it to behave. When you pass an object reference to the `System.out.println(...)` method, print will automatically call the `toString()` method on that object in order to determine what it should print (and what you see is the result of the `toString()` method defined in `Calendar`). Rather than pass the `GregorianCalendar` reference to the print method, you should format your own more meaningful string to display ... and then pass a reference to that string to the print method. – scottb Dec 04 '16 at 22:41