LocalDate.plusDate
String.toUpperCase
GregorianCalendar.add
import java.time.*;
public class CalendarTest {
public static void main(String[] args) {
//构造一个日历对象
LocalDate date = LocalDate.now();
//用当前的日期和时间进行初始化
int month = date.getMonthValue();
int today = date.getDayOfMonth();
//将date设置为这个月的第一天,并得到这一天为星期几
date = date.minusDays(today - 1);
DayOfWeek weekday = date.getDayOfWeek();
// 1 == Mondy,...,7 = Sunday
int value = weekday.getValue();
//打印表头
System.out.println("Mon Tue Wed Thu Fri Sat Sun");
//打印第一行缩进
for (int i = 1; i < value; i++)
System.out.print(" ");
while (date.getMonthValue() == month) {
System.out.printf("%3d", date.getDayOfMonth());
if (date.getDayOfMonth() == today)
System.out.print("*");
else
System.out.print(" ");
//更改器方法
date = date.plusDays(1);
if (date.getDayOfWeek().getValue() == 1) System.out.println();
}
}
}
Mon Tue Wed Thu Fri Sat Sun
1 2 3 4 5
6 7 8 9 10 11 12
13 14* 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
//构造器与类同名
//每个类可以有一个以上的构造器
//构造器可以有0个、1个或多个参数
//构造器没有返回值
//构造器总是伴随着new操作一起调用
import java.time.*;
//只能有一个共有类,可以有任意数目的非共有类
public class EmployeeTest {
public static void main(String[] args) {
//fill the staff array with three Employ objects
//构造一个Employee数组,并填入三个雇员对象
Employee[] staff = new Employee[3];
staff[0] = new Employee("Carl Cracker", 75000, 1988, 12, 15);
staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
staff[2] = new Employee("Tony Tester", 40000, 1997, 3, 3);
//raise everyone's salary by 5%
for (Employee e : staff)
e.raiseSalary(5);
//print out information about all Employee objects
for (Employee e : staff)
System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" + e.getHireDay());
if (staff[0].getSalary() != staff[1].getSalary())
System.out.println("类的方法可以访问类的任何一个对象的私有域!");
}
}
class Employee {
private String name;
private double salary;
private LocalDate hireDay;
//构造器与类同名
//每个类可以有一个以上的构造器
//构造器可以有0个、1个或多个参数
//构造器没有返回值
//构造器总是伴随着new操作一起调用
public Employee(String n, double s, int year, int month, int day) {
name = n;
salary = s;
hireDay = LocalDate.of(year, month, day);
}
//访问器方法
//只返回实例域值:域访问器
//只读
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public LocalDate getHireDay() {
return hireDay;
}
//salary不是只读域,但是它只能用raiseSalry方法修改,一旦这个域值出现了错误,只要调试这个方法就可以了。
//如果salary域是public的,破坏这个域值的捣乱者有可能出现在任何地方。
public void raiseSalary(double byPercent) {
double raise = salary * byPercent / 100;
salary += raise;
}
}
name=Carl Cracker,salary=78750.0,hireDay=1988-12-15
name=Harry Hacker,salary=52500.0,hireDay=1989-10-01
name=Tony Tester,salary=42000.0,hireDay=1997-03-03
类的方法可以访问类的任何一个对象的私有域!