The Gregorian Calendar Class You can use new Gregorian Calendar( to construct a default gregorian Calendar with the current time and use new Gregorian Calendar(year, month, date)to construct a Gregorian Calendar with the specified year month, and date. The month parameter is O-based,i.e 0 is for January Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 11 The GregorianCalendar Class You can use new GregorianCalendar() to construct a default GregorianCalendar with the current time and use new GregorianCalendar(year, month, date) to construct a GregorianCalendar with the specified year, month, and date. The month parameter is 0-based, i.e., 0 is for January
The get Method in Calendar Class oThe get(int field) method defined in the Calendar class is useful to extract the value for a given time field. o The time fields are defined as constants such aS YEAR month DATE, HOUR (for the 12-hour clock), HOUR OF DaY(for the 24 hour clock), MINUtE, seCoNd, DAY of Week(the day number within the current week with 1 for Sunday), DAY OF month(same as the date value), DAY OF YEAR(the day number within the current year with 1 for the first day of the year), WEEK OF MONTH ( the week number within the current month), and WEEK OF YEAR ( the week number within the current year) Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 12 The get Method in Calendar Class ⚫The get(int field) method defined in the Calendar class is useful to extract the value for a given time field. ⚫The time fields are defined as constants such as YEAR, MONTH, DATE, HOUR (for the 12-hour clock), HOUR_OF_DAY (for the 24- hour clock), MINUTE, SECOND, DAY_OF_WEEK (the day number within the current week with 1 for Sunday), DAY_OF_MONTH (same as the DATE value), DAY_OF_YEAR (the day number within the current year with 1 for the first day of the year), WEEK_OF_MONTH (the week number within the current month), and WEEK_OF_YEAR (the week number within the current year)
Interfaces An interface is a classlike construct that contains only constants and abstract methods oIn many ways, an interface is similar to an abstract class but an abstract class can contain variables and concrete methods as well as constants and abstract methods To distinguish an interface from a class, Java uses the following syntax to declare an interface public interface InterfaceName constant declarations method signatures i Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 13 Interfaces ⚫An interface is a classlike construct that contains only constants and abstract methods. ⚫In many ways, an interface is similar to an abstract class, but an abstract class can contain variables and concrete methods as well as constants and abstract methods. To distinguish an interface from a class, Java uses the following syntax to declare an interface: public interface InterfaceName { constant declarations; method signatures; }
Interface is a Special Class o An interface is treated like a special class in Java. Each interface is compiled into a separate bytecode file, just like a regular class o Like an abstract class, you cannot create an instance from an interface using the new operator o but in most cases you can use an interface more or less the same way you use an abstract class. For example, you can use an interface as a data type for a variable, as the result of casting, and so on Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 14 Interface is a Special Class ⚫An interface is treated like a special class in Java. Each interface is compiled into a separate bytecode file, just like a regular class. ⚫ Like an abstract class, you cannot create an instance from an interface using the new operator. ⚫ but in most cases you can use an interface more or less the same way you use an abstract class. For example, you can use an interface as a data type for a variable, as the result of casting, and so on
Example of an Interface use an interface to define a generic compareTo method l This interface is defined in ∥ java. lang package package java. lang, public interface Comparable i public int compare To(Object o Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 15 Example of an Interface // This interface is defined in // java.lang package package java.lang; public interface Comparable { public int compareTo(Object o); } use an interface to define a generic compareTo method