3

I'm having a problem and I absolutely can't wrap my head around it. I am inexperienced in Java (or any language, for that matter), so excuse me if this is a stupid question.

From what I can tell, the format() method from java.time.format.DateTimeFormatter is not working how I thought it did. I am consistently getting the following error message (CLI):

Watch.java:609: error: cannot find symbol
                String dateTimeDisplay = dateTime.format(dateTimeFormat);
                                                 ^
  symbol:   method format(DateTimeFormatter)
  location: variable dateTime of type Object
1 error

And here are the bits of code I think are relevant:

import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class Watch {
    
    protected int hour, hourTens, hourOnes, minute, minuteTens, minuteOnes, amPM;
    
    public void watchMethod(String userInput) { 
        
        Object dateTime; // Create reference to object called dateTime
    
        switch (userInput) {
            case "1":
            ZonedDateTime dateTimeHere = ZonedDateTime.now();
            hour = dateTimeHere.getHour();
            minute = dateTimeHere.getMinute();
            // amPM is used exclusively to display AM/PM or a heart (see lines 580 to 567).
            amPM = dateTimeHere.getHour();
            dateTime = dateTimeHere;
            break;
            
            case "2":
            ZonedDateTime dateTimeThere = ZonedDateTime.now(ZoneId.of("Europe/Paris"));
            hour = dateTimeThere.getHour();
            minute = dateTimeThere.getMinute();
            // amPM is used exclusively to display AM/PM or a heart (see lines 560 to 567).
            amPM = dateTimeThere.getHour();
            dateTime = dateTimeThere;
            break; 
            
            case "3":
            hour = -1;
            minute = -1;
            break;
        }

        // There is some code that prints things to console depending on the hour, minute, and amPM variables.
        // I don't think this is relevant, but that's what's here.

        // The following code prints out the actual date and time from a ZonedDateTime object.
        DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("hh:mm a 'on' EEEE, MMMM dd, yyyy");
        String dateTimeDisplay = dateTime.format(dateTimeFormat); // ERROR
        System.out.print("\nIt is currently " + dateTimeDisplay + "\n");
    }
}

This is all one class.

I have checked that my imports are correct and that there are no misspellings anywhere! This is what other questions related to my problem have led me to believe was the issue, but it doesn't seem to be the case.

I'm also not very familiar with using Object as a reference type, so if that could be what's causing the problem, I'm not surprised. It's all I could figure out to break dateTimeHere/dateTimeThere out of the switch block.

Anyway, I'd greatly appreciate any help I receive.

QUICK EDIT: I also figured that if there were no suitable dateTime object (i.e. case "3"), this would also cause an error. I briefly added an if/else statement to remedy this, with some code in case "3" indicating that dateTime was null, but this did absolutely nothing. Let me know if I should re-add this, though.

EDIT: Thanks to a comment from @MarsAtomic, I see that this post could have used another read-through. My problem is that at the end of my code, I want to print out the ZonedDateTime data the switch statement retrieved, whether that is from the user's computer location or "Paris/Europe". Eventually, that "Paris/Europe" string will (ideally) have some user input from a subclass (as is the case with userInput, but that particular string was just for choosing one of the 3 cases already shown).

iota
  • 34,586
  • 7
  • 32
  • 51
ajent
  • 81
  • 7
  • 1
    The field dateTime is a of type Object. Object does not have a .format() method. If you want to access the format method of ZonedDateTime, you have to declare dateTime as ZonedDateTime (or cast it to ZonedDateTime). The real question is, what made you think you should've declared dateTime an Object in the first place? – MarsAtomic Jul 20 '20 at 01:20
  • Hi, thanks for getting here so quickly! My problem was that initially, the code `String dateTimeDisplay = dateTime.format(dateTimeFormat);` was `String dateTimeDisplay = dateTimeHere.format(dateTimeFormat);` and I kept getting a missing symbol error for dateTimeHere. So I made a new dateTime object, and set dateTimeHere and dateTimeThere equal to it, to fix the scope problem. I guess I just considered `Object` to be like `String` -- stupid mistake. – ajent Jul 20 '20 at 01:24
  • 2
    That's OK, but this is a good time to familiarize yourself with the [X Y Problem](http://xyproblem.info/). You can really compound problems when you don't address the direct cause of an issue in favor of addressing symptoms. Why don't you just describe what you're trying to do in the first place -- I'm loathe to provide a solution at this point that doesn't address the underlying issue. – MarsAtomic Jul 20 '20 at 01:28
  • @MarsAtomic, my bad. I just updated the post, I hope this is now suitable. – ajent Jul 20 '20 at 01:38
  • 2
    Next problem you will be facing is that there are scenarios where `dateTime` will not be initialized with any explicit value. Notice that you are initializing it only in cases `"1"` and `"2"` but what about case `"3"` or if `userInput` will contain any other value like `"dog"`? – Pshemo Jul 20 '20 at 01:45
  • I see that. Okay, I will add some code to consider the case where dateTime remains null (which is what case "3" will do). The code actually won't allow for other inputs (in the subclass, which isn't shown) than 1, 2, or 3, as it's in a while loop that will restart if it is any other value, so no worries there. – ajent Jul 20 '20 at 01:52

1 Answers1

3

You probably meant to declare dateTime as a ZonedDateTime, as Object's definition does not include a format method.

Revised code:

ZonedDateTime dateTime = null; // Create reference to object called dateTime
//...
DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("hh:mm a 'on' EEEE, MMMM dd, yyyy");
if (dateTime != null) {
    String dateTimeDisplay = dateTime.format(dateTimeFormat);
    System.out.print("\nIt is currently " + dateTimeDisplay + "\n");
}
iota
  • 34,586
  • 7
  • 32
  • 51
  • 2
    Gah, what a dumb mistake. Yeah, I was thinking `Object` worked like `String` for some reason, which I then also thought worked like a primitive data type with no methods! Thanks for your help, I should be good to go now. :) – ajent Jul 20 '20 at 01:54
  • @ajent Glad to help. Happy coding! – iota Jul 20 '20 at 01:54