\$\begingroup\$
\$\endgroup\$
1
I have a method to get the time parameter, is this code neat, can the return parameter use other data structures?
private Map<String, Object> getTimeCondition(long smallDaysToSubtract, long bigDaysToSubtract) {
DateTimeFormatter format = DateTimeFormatter.ofPattern(DateUtil.yyyy_MM_dd);
ZoneId zoneId = ZoneId.systemDefault();
LocalDate startLocalDate = LocalDate.now().minusDays(smallDaysToSubtract);
LocalDate endLocalDate = LocalDate.now().minusDays(bigDaysToSubtract);
LocalDateTime startLocalDateTime = LocalDateTime.of(startLocalDate, LocalTime.MIN);
LocalDateTime endLocalDateTime = LocalDateTime.of(endLocalDate, LocalTime.MAX);
Instant startInstant = startLocalDateTime.atZone(zoneId).toInstant();
Instant endInstant = endLocalDateTime.atZone(zoneId).toInstant();
Date startTime = Date.from(startInstant);
Date endTime = Date.from(endInstant);
String startDateStr = startLocalDate.format(format);
String endDateStr = endLocalDate.format(format);
Map<String, Object> map = new HashMap<>();
map.put("startTime", startTime);
map.put("endTime", endTime);
map.put("startDateStr", startDateStr);
map.put("endDateStr", endDateStr);
return map;
}
-
\$\begingroup\$ Welcome to Code Review! I hope you get some great answers. \$\endgroup\$Phrancis– Phrancis2018年06月21日 04:26:53 +00:00Commented Jun 21, 2018 at 4:26
1 Answer 1
\$\begingroup\$
\$\endgroup\$
2
Hmm... I cannot really make much of this code - what is the pupose of that parameter map?
However, there are a few things to note:
- You make use of the modern datetime-api, which is great! And you use it in a correct way - I cannot see any flaws there. BUT in the end you throw all that precision, zoning and niceness overboard and convert everything into
old.ugly.Date
- why? As long as you use this internally, you should aim for the more precise and powerful api and only convert if necessary for legacy code. - Mashup of different tasks: in most cases, the formatting of data (or dates in this case) belongs to the presentation of an application, whereas the precise values belong to the business logic. Thus, I'd leave the formatting out of the method and move this into the presentation layer, which would also help with the next point...:
Map<String, Object>
as a result value. WithObject
values you throw away all generic goodness a map has, and have to cast the values when you use them. This could be circumvented by eiher heeding my advice in the point above (returningMap<String, LocalDateTime>
) or by creating a result class which just consists of the four fields with the correct data types.- Magic strings: "startTime", "endTime", etc. should probably be moved to publicly accessible constants, so the the consumer of the map can use these constants to look up the values he needs without "knowing" and duplicating the concrete key-strings.
-
\$\begingroup\$ Wow, thank you for your suggestion.Can you elaborate on your fourth suggestion? \$\endgroup\$dai– dai2018年06月21日 05:44:51 +00:00Commented Jun 21, 2018 at 5:44
-
\$\begingroup\$ Simply declare something like
public static final String START_TIME = "startTime";
in your class, so that whoever uses this can writemap.get(YourClass.START_TIME)
instead ofmap.get("startTime")
. That way, you can change the concrete string at any time and no knowledge of internal values is needed to use the class. \$\endgroup\$mtj– mtj2018年06月21日 06:27:44 +00:00Commented Jun 21, 2018 at 6:27
lang-java