我正在使用简单的jsp在Java中构建Web应用程序,我想知道是否有一种方法可以在datetime
和datetime_stop每{{1}之间获取datetime_start
值的数组}}。
我正在寻找类似的东西:
hours/day/minutes/etc
例如:
Array getvalues(datetime_start, datetime_stop, "day/hours/min")
它返回:
getvalues("2018-07-02 20:25:08.208812","2018-07-03 20:25:08.208812","hours");
有人能帮助我吗?
答案 0 :(得分:1)
使用java.time
,您可以实现自己的方式。一个使您入门的简单示例可能是:
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
final LocalDateTime dateFirst = LocalDateTime.parse("2018-07-02 20:25:08.208812", formatter);
final LocalDateTime dateLast = LocalDateTime.parse("2018-07-03 20:25:08.208812", formatter);
final ChronoUnit unit = ChronoUnit.valueOf("HOURS");
final List<String> dates = new ArrayList<>();
for (LocalDateTime dateBetween = dateFirst; !dateBetween.isAfter(dateLast); dateBetween = dateBetween.plus(Duration.of(1, unit))) {
dates.add(dateBetween.format(formatter));
}
System.out.println(dates);
如您所见,您可以添加一些自定义:
LocalDateTime
期望使用T
而不是日期和时间之间的空格。如果您希望保留格式,则可以实现自己的格式化程序并将其提供给LocalDateTime#parse
dates.toArray(new String[dates.size()])
),或者如果您想对数组进行其他操作,可以使用unit.between(dateFirst, dateLast)
预先知道数组的大小。在构建数组时。使用此方法,您可以创建自己的方法来遵循API和逻辑。
答案 1 :(得分:1)
这是获得所需结果的一种简单方法。您可能需要添加更多检查,例如输入有效性检查和处理。
包含主要逻辑的实用程序类:
public class PeriodicDateTimeProducer {
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
public List<LocalDateTime> getPeriodicDateTime(String start, String end, PeriodMeasure measure){
LocalDateTime startDateTime = LocalDateTime.parse(start, DATE_TIME_FORMATTER);
LocalDateTime endDateTime = LocalDateTime.parse(end, DATE_TIME_FORMATTER);
List<LocalDateTime> results = new ArrayList<>();
// Use isAfter instead of isBefore becoz you want to include the endDateTime, if it's a valid result
while(!startDateTime.isAfter(endDateTime)){
results.add(startDateTime);
startDateTime = startDateTime.plus(1, measure.getChronoUnit());
}
return results;
}
}
枚举,可帮助您限制可以使用的单位。它还有助于减少由错别字引起的错误:
public enum PeriodMeasure{
DAY(ChronoUnit.DAYS),
HOUR(ChronoUnit.HOURS),
MINUTE(ChronoUnit.MINUTES);
PeriodMeasure(ChronoUnit unit){
this.unit = unit;
}
private ChronoUnit unit;
public ChronoUnit getChronoUnit(){
return this.unit;
}
}
测试人员类别:
public class TestPeriodicDateTime {
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
public static void main (String... args){
PeriodicDateTimeProducer dateTimeUtil = new PeriodicDateTimeProducer();
List<LocalDateTime> rst = dateTimeUtil.getPeriodicDateTime("2018-07-02 20:25:08.208812", "2018-07-03 20:25:08.208812", PeriodMeasure.HOUR);
rst.forEach(r -> System.out.println(DATE_TIME_FORMATTER.format(r)));
}
}
结果:
2018-07-02 20:25:08.208812
2018-07-02 21:25:08.208812
2018-07-02 22:25:08.208812
2018-07-02 23:25:08.208812
2018-07-03 00:25:08.208812
2018-07-03 01:25:08.208812
2018-07-03 02:25:08.208812
2018-07-03 03:25:08.208812
2018-07-03 04:25:08.208812
2018-07-03 05:25:08.208812
2018-07-03 06:25:08.208812
2018-07-03 07:25:08.208812
2018-07-03 08:25:08.208812
2018-07-03 09:25:08.208812
2018-07-03 10:25:08.208812
2018-07-03 11:25:08.208812
2018-07-03 12:25:08.208812
2018-07-03 13:25:08.208812
2018-07-03 14:25:08.208812
2018-07-03 15:25:08.208812
2018-07-03 16:25:08.208812
2018-07-03 17:25:08.208812
2018-07-03 18:25:08.208812
2018-07-03 19:25:08.208812
2018-07-03 20:25:08.208812
Process finished with exit code 0
此解决方案的主要思想: