Skip to content
Advertisement

What is the Signature of LocalTime datatype in Java to create objects with it ? int/string cannot be applied to LocalTime Error [closed]

This is in my constructor in class Office.

  public Office(String name, String location, LocalTime openingTime, LocalTime closingTime) {
        this.name = name;
        this.location = location;
        this.openingTime = openingTime;
        this.closingTime = closingTime;
    }

Now, I want to create an object of Class Office with LocalTime being one of the attributes. However I’m unable to pass the values for Localtime while creating the object. I’m trying

   Office office1=new Office("Company Name","Chennai",10:30,6:30);
   //Or this
   Office office1=new Office("Company Name","Chennai",10,6);
   //Or this
   Office office1=new Office("Company Name","Chennai",'10:30','6:30');
   //Or this
   Office office1=new Office("Company Name","Chennai","10:30","6:30");

None of these is working so far. It is not matching with the signature of the data type LocalTime. Would appreciate the solution to this.

Advertisement

Answer

We can more briefly define your class as a record in Java 16+. A record is appropriate if the main purpose of your class is to communicate data transparently and immutably. The compiler implicitly creates the constructor, getters, equals & hashCode, and toString.

public record Office ( String name, String location, LocalTime openingTime, LocalTime closingTime ) {}

Instantiate. Pay attention to 24-hour clock.

Office o = new Office ( "Acme Corp." , "Chennai" , LocalTime.parse( "10:30" ) , LocalTime.of( 18 , 30 ) ) ;

You obviously have yet to master the basics of the Java language. Please study your textbook and other resources before posting here. Oracle provides the Java Tutorials free of cost.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement