英文:
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("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) {
}
}
}
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.time
的 LocalDate
。
英文:
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("dd/MM/yyyy");
Employee emp = empList.stream()
.max(Comparator.comparing(e -> 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<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("Latest employee joined on: " + 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论