如何在这种情况下找到具有最新入职日期的员工

huangapple go评论58阅读模式
英文:

How to find Employee with the latest Joining Date in this case

问题

我有一个员工列表我正在尝试找到具有最新入职日期的员工”。 迄今为止我已经实现了以下内容

**员工** 

```java
package com;

public class Employee {
    private String accountNumber;
    private String joiningdate;

    public Employee(String accountNumber, String joiningdate) {
        super();
        this.accountNumber = accountNumber;
        this.joiningdate = joiningdate;
    }

    // 设置器和获取器
}

应用:

package com;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class LatestDueDate {

    public static void main(String[] args) throws Exception {
        try {
            SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");

            List<Employee> empList = new ArrayList<>();

            empList.add(new Employee("1234", "07/11/2020"));
            empList.add(new Employee("1234", "06/12/2020"));

            Employee emp = empList.stream().max(Comparator.comparing(Employee::getJoiningdate)).get();
        } catch (Exception e) {

        }
    }
}

我知道我需要将字符串日期格式转换为日期。


<details>
<summary>英文:</summary>

I have the List of Employees, I am trying to find the `Employee` with the latest Joining date. This is what i have implemented so far.
 
**Employee :** 

```java
package com;

public class Employee {
    private String accountNumber;
    private String joiningdate;

    public Employee(String accountNumber, String joiningdate) {
        super();
        this.accountNumber = accountNumber;
        this.joiningdate = joiningdate;
    }

    // setters and getters
}

App :

package com;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class LatestDueDate {

    public static void main(String[] args) throws Exception {
        try {
            SimpleDateFormat format = new SimpleDateFormat(&quot;MM/dd/yyyy&quot;);

            List &lt; Employee &gt; empList = new ArrayList &lt; &gt; ();

            empList.add(new Employee(&quot;1234&quot;, &quot;07/11/2020&quot;));
            empList.add(new Employee(&quot;1234&quot;, &quot;06/12/2020&quot;));

            Employee emp = empList.stream().max(Comparator.comparing(Employee::getJoiningdate)).get();
        } catch (Exception e) {

        }
    }
}

I know that I need to convert the string date format to Date.

答案1

得分: 3

你可以将字符串日期转换为 LocalDate,并在比较器内部进行比较。但我建议您在雇员类中为 joiningdate 使用 LocalDate,这样您就不需要在比较器内部解析日期字符串以获得最新的加入日期。

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
Employee emp = empList.stream()
    .max(Comparator.comparing(e -> LocalDate.parse(e.getJoiningdate(), formatter)))
    .get();

注意:不要使用旧的 Date 类,而是使用 java.timeLocalDate

英文:

You can convert String date into LocalDate and compare inside comparator. But I recomend you to use LocalDate for joiningdate in Employee class then you don't need to parse date string inside comparator to latest joining date one.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern(&quot;dd/MM/yyyy&quot;);
Employee emp = empList.stream()
    .max(Comparator.comparing(e -&gt; LocalDate.parse(e.getJoiningdate(), formatter)))
    .get();

Note: Don't use old Date class rather use LocalDate of java.time

答案2

得分: -1

你正在按照员工的入职日期进行排序,但问题在于你的Java类使用了_text_字符串来表示这个日期,而不是一个真正的java.util.Date或等效的Java 8类。如果你必须继续使用文本日期,那么你至少应该以ISO格式表示它们,例如:

List<Employee> empList = new ArrayList<>();

empList.add(new Employee("1234", "2020/11/07"));
empList.add(new Employee("1234", "2020/12/06"));

Employee emp = empList.stream().max(Comparator.comparing(Employee::getJoiningDate)).get();
System.out.println("最近加入的员工日期: " + emp.getJoiningDate());

上述代码有效,因为文本日期2020/12/06会在2020/11/07之后排序。一个更好的方法可能是要么使用适当的日期类型来表示入职日期,要么在对员工进行排序之前,将文本日期转换为实际的日期类型。

英文:

You are sorting by the employee's joining date, but the problem is that your Java class represents this date using a text string, not a bona fide java.util.Date or equivalent Java 8 class. If you must stick with text dates, then you should at least represent them in an ISO format, e.g.

List&lt;Employee&gt; empList = new ArrayList&lt;&gt;();

empList.add(new Employee(&quot;1234&quot;, &quot;2020/11/07&quot;));
empList.add(new Employee(&quot;1234&quot;, &quot;2020/12/06&quot;));

Employee emp = empList.stream().max(Comparator.comparing(Employee::getJoiningdate)).get();
System.out.println(&quot;Latest employee joined on: &quot; + emp.getJoiningDate());

The above works, because the text date 2020/12/06 will sort after 2020/11/07. A better approach might be to either represent the joining date using a proper date type, or to at least convert the text dates to actual date types before sorting the employees.

huangapple
  • 本文由 发表于 2020年8月25日 23:20:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63582062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定