Java常用类库Data类_Calender类_Math类等日期类的相关处理

Date

  1. Date表示特定的瞬间,精确到毫秒。在Java1.1版本后Date API废弃了更多的方法,官方提倡使用Calendar以及使用DateFormat日期格式化类来代替Date中被废弃的诸多方法。

 

Date对象的创建

  1. public Data() //表示的JVM所运行的系统时间
  2. public Data(long date) //1970年至今的一个毫秒计时

 

SimpleDateFormat格式化日期类型

 

 

Date常用方法

  1. public boolean after(Date when) //比较两个时间的先后,如果调用者时间更晚,返回true
  2. public boolean before(Date when) //比较两个时间的先后,如果调用者时间更早,返回true
  3. public int compareTo(Date anotherDate) //如果调用者时间大于比较对象,返回正数,相同返回0,小于返回负数
  4. public long getTime() //得到1970年以来实际经过日期的毫秒数
  5. public void setTime(long time) //以一个毫秒数来设定当前日期
  6. public String toString() //把时间对象以dow mon dd hh:mm:ss zzz yyyy的格式输出(星期,月份,天,时,分,秒,时区,年)
  7. SimpleDateFormat(pattern).parse(str); //将字符串格式化为Date类型,其中pattern是字符串类型,是代表将要格式的字符串str的格式,如yyyy-MM-dd,str是将要格式化的字符串
  8. SimpleDateFormat("yyyy-MM-dd").format(date)//将Date类型格式化为字符串类型,其中“yyyy-MM-dd”是要输出的字符串格式,date是要格式化的日期类型

 

LocalDate方法

  1. https://www.cnblogs.com/jtools/p/6502999.html
  2. https://www.liaoxuefeng.com/article/991339711823296

 

Calender对象的创建

  1. public static Calender getInstance() //获取到当前JVM运行的系统的时间
  2. public staitc Calender getInstance(Locale aLocale) //获取到一个时区常量,如中国

 

Calender主要常量字段

  1. public static final int AM //指示从午夜到中午之前这段时间的AM_PM字段值
  2. public static final int PM //指示从中午到午夜之前这段时间的AM_PM字段值
  3. public static final int YEAR //日历中的年份
  4. public static final int MONTH //日历中的月份,从0开始
  5. public static final int DATE //日历当中当月的天,从1开始
  6. DAY_OF_MONTH //当月的第几天,从1开始

(1) CalendarDAY_OF_MONTH, DAY_OF_YEAR, DATE的区别,例子:

(2) cal1.add(Calendar.DAY_OF_MONTH,1);  

(3) cal1.add(Calendar.DAY_OF_YEAR,1);  

(4) cal1.add(Calendar.DATE,1);

(5) 就单纯的add操作结果都一样,因为都是将日期+1

(6) 就没有区别说是在月的日期中加1还是年的日期中加1

(7) 但是Calendar设置DAY_OF_MONTHDAY_OF_YEAR的目的不是用来+1

(8) 将日期加1,这通过cal1.add(Calendar.DATE,1)就可以实现

(9) DAY_OF_MONTH的主要作用是cal.get(DAY_OF_MONTH),用来获得这一天在是这个月的第多少天

(10) Calendar.DAY_OF_YEAR的主要作用是cal.get(DAY_OF_YEAR),用来获得这一天在是这个年的第多少天。

(11) 同样,还有DAY_OF_WEEK,用来获得当前日期是一周的第几天

  1. public static final int HOUR //日历12小时表示0-11
  2. public static final int HOUR_OF_DAY //日历24小时制表示
  3. public static final int MINUTE //指示一小时中的分钟,0-59
  4. public static final int SECOND //指示一分钟的秒
  5. public static final int MILLISECOND //指示一分钟的毫秒
  6. public static final int HOUR_OF_DAY //日历24小时制表示
  7. public static final int MINUTE //指示一小时中的分钟
  8. public abstract void add(int field,int value) //更改日历中某个字段field的值
  9. public boolean after(Object when) //判断日历当前时间是否在给定的时间之前,返回值1,-1,0
  10. public boolean before(Object when) //判断日历当前时间是否在给定的时间之后,返回值1,-1,0
  11. public int compareTo(Calender anotherCalender) //比较两个Calender的时间值从历元至现在的毫秒差

 

Calender主要方法2

  1. public final Date getTime() //获取日历当前时间对象
  2. public void set(int field,int value) //将给定的日历字段field设置为给定value
  3. public final void set(int year,int month,int date) //设置日历的年份、月份、日期值
  4. public final void set(int year,int month,int date,int hourOfDay,int minute,int second) //设置日历的年、月、日、时、分、秒的值

 

Math

  1. Math类定义了数学运算的基本功能,Math类中所有属性及功能方法都定义为static的,Math类不需要创建实例,Math类是final修饰的终极类,不能被继承

 

Math类常量字段

  1. public static final double E //自然对数的底数(2.718281828459045)通常用于科学领域的计算使用
  2. public static final double PI //圆的周长和直径的比,即圆周率表示值(3.141592653589793

 

Math类常用方法

  1. public static double abs(double|float|long|int num) //返回绝对值
  2. public static double cbrt(double num) //计算给定值的立方根
  3. public static double ceil(double num) //返回最小的(最接近负无穷大)double值,该值大于等于参数,并等于某个整数
  4. public static double floor(double a) //返回最大的(最接近正无穷大)double值,该值小于等于参数,并等于某个整数
  5. public static double max(double a,double b) //返回两个数中最大值,相等则随机返回其中一个
  6. public static double min(double a,double b) //返回两个数中最小值,相等则随机返回其中一个
  7. public static double pow(double a,double b) //返回第一个参数的第二个参数次幂的值
  8. public static double double random() //返回带正号的double值,该值大于等于0.0且小于1.0的随机数

 

import java.util.Date;
import java.util.Scanner;

/**
 * 根据以下需求完成既定任务
 * 某软件公司要对公司的员工进行一次人员整理, 业务要求如下:
 * 其中员工有如下信息, 姓名, 所属部门, 生日, 入职日期, 在公司工作的时长(以月份为单位),假如有如下职员信息
 * 序号    姓名(String)    所属部门(String)    生日(Date)    入职日期(Date)    工作时长/月(int)
 * 1    张无忌    人事部    1989-10-20    2010-10-20    计算得知
 * 2    任盈盈    市场部    1992-11-20    2017-10-20    计算得知
 * 3    向问天    人事部    1971-5-20    1999-10-20    计算得知
 * 4    练霓裳    研发部    1980-10-26    2014-10-20    计算得知
 * 5    张三丰    研发部    1976-4-22    2000-10-20    计算得知
 *
 * 编写一个Employee员工类并提供定义私有的以上所述员工属性, 标准的设置getXX和setXX方法,
 * 编写员工管理类EmployeeManagerSupport,此类应提供如下2个基本功能方法
 * public void input(Employee emp){                } 此方法实现对员工的录入功能, 将录入的员工存储在当前类的成员属性Employee 数组中
 * public Employee [ ] findEmp(int month ){        } 此方法实现对入职时间超过month个月员工查找功能,并返回符合条件的员工数组,这些员工将被提升2及工资
 * public Employee [ ] findEmp(Date date){        } 此方法实现对出生早于date日期的员工查找功能并返回符合条件的员工数组 ,这些员工将被授予突出贡献奖
 * 编写包含主方法的类,从控制台调用录入方法实现将上表中信息实现录入, 接着再 调用相对的input方法,实现输出哪些员工符合2级涨薪条件,哪些员工能够被授予突出贡献奖. 在控制台输出这些员工的姓名及部门等信息.
 */

public class EmployManagerSupport {
    //定义一个employee数组
    private static Employee[] employees;    //1.先声明数组的类型为Emloyee类型
    //定义了一个index变量作为数组下标,初值是0
    private static int index;

    //静态块初始化数组为5个元素
    static {
        employees = new Employee[5];        //2.再给数组分配内存空间
    }

    /**
     * public void input(Employee emp){        } 此方法实现对员工的录入功能,
     * 将录入的员工存储在当前类的成员属性Employee 数组中
     * @param employee
     */
    //调用这个静态方法时,传入的employee对象将会加入到employees数组当中
    public static void input(Employee employee){
        if (index<employees.length){        //直到employees数组装满5个对象为止
            employees[index++] = employee;
        }
    }

    /**
     * public Employee [ ] findEmp(int month ){        } 此方法实现对
     * 入职时间超过month个月员工查找功能,并返回符合条件的员工数组,这些员工将被提升2及工资
     * @param month
     * @return
     */
    //该方法返回值是Employeep[]类型的数组,传入的参数是用户输入的月份
    public static Employee[] findEmp(int month){
        //新建一个类型为Emplpyee[]数组,空间为5的对象emp1
        Employee[] emp1 = new Employee[5];
        //定义一个索引j=0,一会儿给emp1用
        int j=0;
        for (Employee emp :employees){  //遍历employees数组,每一个对象存入Employee类型的emp对象中
            if (emp!=null){
                if (emp.getEmployeeHiredTime()>month){  //如果员工工作时长符合要求
                    emp1[j++] = emp;    //将这个员工存入新数组emp1中
                }
            }
        }
        return emp1;    //返回emp1数组
    }

    /**
     * public Employee [ ] findEmp(Date date){        } 此方法实现对
     * 出生早于date日期的员工查找功能并返回符合条件的员工数组 ,这些员工将被授予突出贡献奖
     * @param date
     * @return
     */
    //该方法返回值是Employeep[]类型的数组,传入的参数是用户输入的日期
    public static Employee[] findEmp(Date date){
        //新建一个类型为Emplpyee[]数组,空间为5的对象emp2
        Employee[] emp2 = new Employee[5];
        //定义一个索引j=0,一会儿给emp2用
        int j=0;
        for (Employee emp:employees){   //遍历employees数组,每一个对象存入Employee类型的emp对象中
            if (emp!=null){
                if (DayMethod.compare(emp.getEmployeeBirth(),date)){    //如果员工出生日期比用书输入的Date更早,返回true,这个循环可以继续执行
                    emp2[j++] = emp;    //将这个员工存入新数组emp2中
                }
            }
        }
        return emp2;    //返回emp2数组
    }

    /**
     * 将键盘输入的信息封装成一个Employee对象
     */
    public static Employee getEmployee(){
        Scanner input = new Scanner(System.in);

        System.out.println("请输入员工的姓名");
        String employeeName = input.next();

        System.out.println("请输入员工的部门");
        String employeeDepartment = input.next();

        System.out.println("请输入员工的生日(yyyy-MM-dd)");
        String birthday  = input.next();

        System.out.println("请输入员工的入职日期(yyyy-MM-dd)");
        String hireDate = input.next();

        //使用格式化方法,把字符串格式化为Date类型
        Date employeeBirth = DayMethod.parse(birthday,"yyyy-MM-dd");
        Date employeeDay = DayMethod.parse(hireDate,"yyyy-MM-dd");

        //通过构造方法,封装成一个对象并返回
        return new Employee(employeeName,employeeDepartment,employeeBirth,employeeDay,DayMethod.calc(employeeDay));
    }

    public static void main(String[] args) {
        //先来一个Scanner
        Scanner in = new Scanner(System.in);

        //循环录入员工数据
        while(true){
            if (index==5)   //如果已经录入了5个员工数据,break这个循环
                break;

                //定义一个Employee类型的对象employee来接收录入员工信息方法的返回值
                Employee employee = getEmployee();

                //调用input方法将对象放入数组中
                input(employee);

                System.out.println("是否继续(y/n)");
                String tag = in.next();

                //equalsIgnoreCase() 方法用于将字符串与指定的对象比较,不考虑大小写。
                //如果给定对象与字符串相等,则返回 true;否则返回 false。
                if (tag.equalsIgnoreCase("n"))
                    break; //如果用户输入n,跳出循环

        }

            System.out.println("现有员工信息");
            for (Employee emp:employees)
                if (emp != null)
                    System.out.println(emp);

            //查询工作月份,实现输出哪些员工符合2级涨薪条件
            System.out.println("请输入需要查询入职了多少个月的员工");
            int month = in.nextInt();   //接收用户输入的月份,作为参数传给findEmp方法
            Employee[] emps = findEmp(month);   //定义一个数组接收放法的返回值

            for (Employee emp3 :emps)
                if (emp3 != null)
                System.out.println(emp3);   //遍历输出数组,即输出符合条件的员工

            //查询出生日期,输出哪些员工能够被授予突出贡献奖
            System.out.println("请输入需要比较的出生日期(yyyy-MM-dd)");
            String str = in.next(); //将用户的输入用一个字符串接收
            Date d1 = DayMethod.parse(str,"yyyy-MM-dd");    //将字符串转换成Date类型
            Employee[] emp_b = findEmp(d1); //将Date类型参数传入方法执行,使用新创建的数组接收返回值
            for (Employee emp4 :emp_b)
                if (emp4 != null)
                    System.out.println(emp4);   //遍历输出符合条件的员工
    }


}

 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.util.Calendar;
import java.util.Date;

/**
 * 定义一个日期方法类用来存储格式化日期以及比较日期有关的方法
 */
public class DayMethod {

    //定义一个format格式化日期的方法,接收一个Date对象,将它格式化为yyyy-MM-dd的String类型返回
    public static String format(Date date){
        return new SimpleDateFormat("yyyy-MM-dd").format(date);
    }

    //定义一个compare方法,2.比较两个时间的先后,如果调用者时间date1更早,返回true,如果date2时间更早,返回false
    public static boolean compare(Date date1,Date date2){
        return date1.before(date2);
    }

    //定义一个将字符串格式化成Date的方法,str是将要被格式化的字符串。pattern是字符串的书写格式,如"yyyy-MM-dd"
    public static Date parse(String str, String pattern)  {
        try {
            return new SimpleDateFormat(pattern).parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    //定义一个计算工作时长的方法
    public static int calc(Date employeeDay){
        //获取一个本地的时间对象
        LocalDate date1 = LocalDate.now();

        //创建一个Calender日历对象c,默认值是获取到当前JVM运行的系统的时间
        Calendar c = Calendar.getInstance();

        //将日历对象c的时间设置为员工的入职时间employeeDay
        c.setTime(employeeDay);

        //使用LocalDate.of(年,月,日)可以通过年月日的方法,格式化一个Date日期
        //获取日历c里的年月日,输入到date2对象中,因为获取到的月份是从0开始的所以MONTH需要+1
        LocalDate date2 = LocalDate.of(c.get(Calendar.YEAR),c.get(Calendar.MONTH)+1,c.get(Calendar.DAY_OF_MONTH));

        //创建一个Period类型的between对象,通过Period.between方法计算date2和date1的时间差
        Period between = Period.between(date2,date1);

        //因为between方法在计算相差天数、相差月数的时候,发现只能计算同月的天数、同年的月数,不能计算隔月的天数以及隔年的月数
        //所以需要用相隔的年数*12,在加上相隔的月数,就可以得到隔年的月数了
        //因此返回值是between.getYears()*12
        return between.getYears()*12+between.getMonths();
    }

    //定义一个主方法测试自己的计算方法是否生效
    public static void main(String[] args) {
        System.out.println(calc(DayMethod.parse("2021-6-12","yyyy-MM-dd")));
    }
}
import java.util.Date;

/**
 * 封装一个员工类,定义员工的私有属性
 */
public class Employee {
    private String employeeName;            //员工姓名
    private String employeeDepartment;      //员工部门
    private Date employeeBirth;          //员工生日
    private Date employeeDay;            //员工入职日期
    private int employeeHiredTime;          //员工工作时长


    //定义一个空的构造方法
    public Employee(){

    }

    //重写一个构造方法
    public Employee(String employeeName, String employeeDepartment, Date employeeBirth,Date employeeDay,int employeeHiredTime){
        this.employeeName = employeeName;
        this.employeeDepartment = employeeDepartment;
        this.employeeBirth = employeeBirth;
        this.employeeDay = employeeDay;
        this.employeeHiredTime = employeeHiredTime;
    }

    public String getEmployeeName() {
        return employeeName;
    }

    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }

    public String getEmployeeDepartment() {
        return employeeDepartment;
    }

    public void setEmployeeDepartment(String employeeDepartment) {
        this.employeeDepartment = employeeDepartment;
    }

    public Date getEmployeeBirth() {
        return employeeBirth;
    }

    public void setEmployeeBirth(Date employeeBirth) {
        this.employeeBirth = employeeBirth;
    }

    public Date getEmployeeDay() {
        return employeeDay;
    }

    public void setEmployeeDay(Date employeeDay) {
        this.employeeDay = employeeDay;
    }

    public int getEmployeeHiredTime() {
        return employeeHiredTime;
    }

    public void setEmployeeHiredTime(int employeeHiredTime) {
        this.employeeHiredTime = employeeHiredTime;
    }

    //重写Object类里的toString方法,把变量都格式化成字符串输出,打印对象,默认会调用该toString方法
    @Override
    public String toString() {
        return "Employee{" +
                "员工名字='" + employeeName + '\'' +
                ", 员工部门='" + employeeDepartment + '\'' +
                ", 员工生日=" + DayMethod.format(employeeBirth) +
                ", 员工入职日期=" + DayMethod.format(employeeDay) +
                ", 员工工作时长=" + employeeHiredTime +
                '}';
    }
}

 

posted @ 2021-06-26 17:16  伊万  阅读(237)  评论(0编辑  收藏  举报