import java.util.Date; public class date { public static void main(String[] args){ //实例化(构造)一个新类,使用Date下的方法,必须实例化(构造) System.out.println(new Date());//Wed Aug 07 23:27:00 CST 2019
String s = new Date().toString(); System.out.println(s);//Wed Aug 07 23:32:09 CST 2019
//此代码等同于上述代码,构造一个新的类并且赋值给一个变量,后面可直接使用变量.方法来使用Date类中所有的方法 Date barthday = new Date();//引用新构造的对象变量barthday String b = barthday.toString(); System.out.println(b);//Wed Aug 07 23:32:46 CST 2019
//错误用法 //Date barth; //String d = barth.toString();//未构造方法,无法使用Date类中的方法
//正确用法 //Date barth = new Date(); //或者 Date barth = barthday;//引用已存在的对象 } }
public class localdate { public static void main(String[] args){ //自定义特殊时间 LocalDate time = LocalDate.of(2019 , 8 , 7); System.out.println(time);//2019-08-07
int year = time.getYear();//年 int month = time.getDayOfMonth()+1;//月 int day = time.getDayOfMonth();//日 System.out.println(year); System.out.println(month); System.out.println(day);
//增加天数,赋给days,days根据当前时间+1000计算。 LocalDate days = time.plusDays(1000); year = days.getYear(); month = days.getMonthValue(); day = days.getDayOfMonth(); System.out.println(year);//2022 System.out.println(month);//5 System.out.println(day);//3 } }
public class plusdate { public static void main(String[] args){ LocalDate time = LocalDate.of(2019 , 8 , 7); LocalDate days = time.plusDays(1000);//此方法是将当前时间+1000后赋给新变量,原变量不变
//老版增加天数写法,使用此方法后,变量会改变 GregorianCalendar some = new GregorianCalendar(2019 , 8 , 8); some.add(Calendar.DAY_OF_MONTH , 1000); int year = some.get(Calendar.YEAR);//年 int month = some.get(Calendar.MONTH) + 1;//月 int day = some.get(Calendar.DAY_OF_MONTH);//日 System.out.println(year); System.out.println(month); System.out.println(day); } }
public class datelizi { public static void main(String[] args){ LocalDate date = LocalDate.now();//实例化 int month = date.getMonthValue();//月 int today = date.getDayOfMonth();//日
date = date.minusDays(today - 1);//获取当前天数指定减去的天数 DayOfWeek weekday = date.getDayOfWeek();//获取当前日期是周几 int value = weekday.getValue();//返回当前月份值
System.out.println("Mon Tue Wed Thu Fri sat Sun"); for (int i=1;i<value;i++){//月份从1开始,1小于当前月份 System.out.println(" "); } while (date.getMonthValue() == month){ System.out.printf("%3d" , date.getDayOfMonth()); if (date.getDayOfMonth() == today){ System.out.println("*"); }else { System.out.println(" "); } date = date.plusDays(1); if (date.getDayOfWeek().getValue() == 1){ System.out.println(); } } if (date.getDayOfWeek().getValue() != 1){ System.out.println(); } } }
0x03 用户自定义类
Employee类
增加雇员薪水小程序
import java.time.*; public class EmployeeTest{ public static void main(String args){ Employee[] staff = new Employee[3];//定义三个成员
//封装一个类,接收传输过来的数值,放入类中处理 public Employee(String n , double s, int year, int month, int day){ name = n; salary = s; hireDay = LocalDate.of(year , month , day); }
public Employee(String n , double s, int year, int month, int day) public String getName() public double getSalary() public LocalDate getHireDay() public void raiseSalary(double byPercent)