zoukankan      html  css  js  c++  java
  • 纸上得来终觉浅,绝知此事要躬行。

    LocalDate类的使用

     1 /**
     2      * 让我们手写一个日历吧
     3      * @param args
     4      */
     5     public static void main(String[] args) {
     6         LocalDate date = LocalDate.now();
     7         int month = date.getMonthValue();
     8         int today = date.getDayOfMonth();
     9         
    10         date = date.minusDays(today - 1);    // Set to start of month
    11         DayOfWeek weekday = date.getDayOfWeek();
    12         int value = weekday.getValue();        // 1 = Monday ...7 = Sunday
    13         
    14         System.out.println("Mon Tue Mon Thu Fri Sat Sun");
    15         for(int i = 1; i < value; i ++)
    16             System.out.print("    ");
    17         while (date.getMonthValue() == month) 
    18         {
    19             System.out.printf("%3d", date.getDayOfMonth());
    20             if (date.getDayOfMonth() == today) 
    21                 System.out.print("*");
    22             else
    23                 System.out.print(" ");
    24             date = date.plusDays(1);
    25             if (date.getDayOfWeek().getValue() == 1) System.out.println();
    26         }
    27         if (date.getDayOfWeek().getValue() != 1) {
    28             System.out.println();
    29         }
    30     }

    显示效果:

    EmployeeTest

     1 public class EmployeeTest {
     2 
     3     public static void main(String[] args) {
     4         // fill the staff array with three Employee objects
     5         Employee[] staff = new Employee[3];
     6         
     7         staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
     8         staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
     9         staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);
    10         
    11         // raise everyone's salary by 5%
    12         for(Employee e : staff) {
    13             e.raiseSalary(5);
    14         }
    15         
    16         // print out information about all Employee objects
    17         for(Employee e : staff) {
    18             System.out.println("name=" + e.getName()+",salary=" + e.getSalary() + ",hireDay="+ e.getHireDay());
    19         }
    20     }
    21     
    22     
    23 }
    24 
    25 class Employee
    26 {
    27     private String name;
    28     private double salary;
    29     private LocalDate hireDay;
    30     
    31     public Employee(String name, double salary, int year, int month, int day) {
    32         super();
    33         this.name = name;
    34         this.salary = salary;
    35         this.hireDay = LocalDate.of(year, month, day);
    36     }
    37 
    38     public String getName() {
    39         return name;
    40     }
    41 
    42     public double getSalary() {
    43         return salary;
    44     }
    45 
    46     public LocalDate getHireDay() {
    47         return hireDay;
    48     }
    49     
    50     public void raiseSalary(double byPercent) 
    51     {
    52         double raise = salary * byPercent / 100;
    53         salary += salary;
    54     }
    55     
    56 }

     StaticTest

     1 public class StaticTest {
     2     public static void main(String[] args) {
     3         // fill the staff array with three Employee objects
     4         Employee[] staff = new Employee[3];
     5         
     6         staff[0] = new Employee("Tom", 40000);
     7         staff[1] = new Employee("Dick", 60000);
     8         staff[2] = new Employee("Harry", 65000);
     9         
    10         // print out information about all Employee objects
    11         for (Employee e : staff) {
    12             e.setId();
    13             System.out.println("name = " + e.getName() + ", id = " + e.getId() +", salary = " + e.getSalary());;
    14         }
    15         
    16         int n = Employee.getNextId();    // call static method
    17         System.out.println("Next avaliable id=" + n);
    18     }
    19     
    20 }
    21 
    22 class Employee
    23 {
    24     private static int nextId = 1;
    25     
    26     private String name;
    27     private double salary;
    28     private int id;
    29     
    30     public Employee(String name, double salary) {
    31         super();
    32         this.name = name;
    33         this.salary = salary;
    34         this.id = 0;
    35     }
    36 
    37     public String getName() {
    38         return name;
    39     }
    40 
    41     public double getSalary() {
    42         return salary;
    43     }
    44 
    45     public int getId() {
    46         return id;
    47     }
    48     
    49     public void setId()
    50     {
    51         id = nextId;    // set id to next avaliable id
    52         nextId++;
    53     }
    54     
    55     public static int getNextId() {
    56         return nextId;    // return static field
    57     }
    58     
    59     public static void main(String[] args) {
    60         Employee employee = new Employee("Harry", 50000);
    61         System.out.println(employee.getName() + " " + employee.getSalary());
    62     }
    63     
    64     
    65 }
    public class StaticTest {
        public static void main(String[] args) {
            // fill the staff array with three Employee objects
            Employee[] staff = new Employee[3];
            
            staff[0] = new Employee("Tom", 40000);
            staff[1] = new Employee("Dick", 60000);
            staff[2] = new Employee("Harry", 65000);
            
            // print out information about all Employee objects
            for (Employee e : staff) {
                e.setId();
                System.out.println("name = " + e.getName() + ", id = " + e.getId() +", salary = " + e.getSalary());;
            }
            
            int n = Employee.getNextId();    // call static method
            System.out.println("Next avaliable id=" + n);
        }
        
    }
    
    class Employee
    {
        private static int nextId = 1;
        
        private String name;
        private double salary;
        private int id;
        
        public Employee(String name, double salary) {
            super();
            this.name = name;
            this.salary = salary;
            this.id = 0;
        }
    
        public String getName() {
            return name;
        }
    
        public double getSalary() {
            return salary;
        }
    
        public int getId() {
            return id;
        }
        
        public void setId()
        {
            id = nextId;    // set id to next avaliable id
            nextId++;
        }
        
        public static int getNextId() {
            return nextId;    // return static field
        }
        
        public void raiseSalary(double byPercent) 
        {
            double raise = salary * byPercent / 100;
            salary += raise;
        }
        
        public static void tripleSalary(Employee x) {
            x.raiseSalary(200);
        }
        
        public static void main(String[] args) {
            Employee harry;
            harry = new Employee("Harry", 50000);
            tripleSalary(harry);
            System.out.println(harry.getName() + " " + harry.getSalary());
        }
        
        
    }

     

     1 public class StaticTest {
     2     public static void main(String[] args) {
     3         /**
     4          * Test 1: Methods can't modify numeric parameters
     5          */
     6         System.out.println("Testing tripleValue:");
     7         double percent = 10;
     8         System.out.println("Before: percent=" + percent);
     9         tripleValue(percent);
    10         System.out.println("After: percent=" + percent);
    11         
    12         /**
    13          * Test 2: Methods can change the state of object parameters
    14          */
    15         System.out.println("
    Testing tripleSalary");
    16         Employee harry = new Employee("Harry", 50000);
    17         System.out.println("Before: salary=" + harry.getSalary());
    18         tripleSalary(harry);
    19         System.out.println("After: salary=" + harry.getSalary());
    20         
    21         /**
    22          * Test 3: Methods can't attach new objects to object parameters
    23          */
    24         System.out.println("
    Testing swap:");
    25         Employee a = new Employee("Alice", 70000);
    26         Employee b = new Employee("Bob", 70000);
    27         System.out.println("Before: a=" + a.getName());
    28         System.out.println("Before: b=" + b.getName());
    29         swap(a, b);
    30         System.out.println("After: a=" + a.getName());
    31         System.out.println("After: b=" + b.getName());
    32         
    33     }
    34     
    35     public static void tripleValue(double x) {
    36         x = 3 * x;
    37         System.out.println("End of method: x=" + x);
    38     }
    39     
    40     public static void tripleSalary(Employee x) {
    41         x.raiseSalary(200);
    42         System.out.println("End of method: salary=" + x.getSalary());
    43     }
    44     
    45     public static void swap(Employee x, Employee y) {
    46         Employee temp = x;
    47         x = y;
    48         y = temp;
    49         System.out.println("End of method: x=" + x.getName());
    50         System.out.println("End of method: y=" + y.getName());
    51     }
    52     
    53 }
    54 
    55 class Employee        // simplfied Employee class
    56 {    
    57     private String name;
    58     private double salary;
    59     
    60     public Employee(String name, double salary) {
    61         super();
    62         this.name = name;
    63         this.salary = salary;
    64     }
    65 
    66     public String getName() {
    67         return name;
    68     }
    69 
    70     public double getSalary() {
    71         return salary;
    72     }
    73 
    74     
    75     public void raiseSalary(double byPercent) 
    76     {
    77         double raise = salary * byPercent / 100;
    78         salary += raise;
    79     }
    80 }               
  • 相关阅读:
    tomcat 乱码问题
    mongo获取lbs数据
    sping mvc+uploadify 上传文件大小控制3部曲
    jstack 查看线程状态
    nginx 限流配置
    查找占用CPU高线程
    redis sentinel无法启动问题
    GC垃圾回收机制
    ASP.NET 生命周期
    Javascript验证手机号码正则表达式
  • 原文地址:https://www.cnblogs.com/Night-Watch/p/12099133.html
Copyright © 2011-2022 走看看