首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java:处理日期

Java:处理日期
EN

Stack Overflow用户
提问于 2018-07-31 02:24:17
回答 1查看 214关注 0票数 1

我对Java非常陌生。所以请耐心听我说。我有4个变量:

mode =  prior|later|value;(can have either of these 3 values)
occurrence = 2
day = Wednesday
eta = 14:00

我正在尝试实现一个方法,它将基于以下条件返回一个列表date

案例1:(mode=prior,occurence=2,day=wednesday,eta=14:00)

应返回从当前月份起下个月的前2个星期三的日期。

output = Wed Aug 01 14:00:00 2018,Wed Aug 08 14:00:00 2018

案例2:(mode=later,occurence=2,day=wednesday,eta=14:00)

应从当前月份返回下个月的最后2个星期三的日期。

output = Wed Aug 22 14:00:00 2018,Wed Aug 29 14:00:00 2018

案例3:(mode=value,occurence=3,day=wednesday,eta=14:00)

应从当前月份返回下个月的第三个星期三的日期。

output = [Wed Aug 15 14:00:00 2018, Wed Aug 29 14:00:00 2018]

这就是我到目前为止所做的,

public static List<Date> getDates(Calendar c, String mode, int occurrence, int dayNo, String eta) {
            List<Date> dates = new ArrayList<Date>();
            switch(mode) {
            case "value": {
                String[] times = eta.split(":");
                c.set(Calendar.DAY_OF_WEEK, dayNo);  
                c.set(Calendar.DAY_OF_WEEK_IN_MONTH, occurrence);
                c.set(Calendar.MONTH, Calendar.JULY + 1); 
                c.set(Calendar.YEAR, 2018);
                c.set(Calendar.HOUR_OF_DAY, Integer.parseInt(times[0]));
                c.set(Calendar.MINUTE, Integer.parseInt(times[1]));
                dates.add(c.getTime());
            }
            }
            return dates;
        }

有没有更好的方法来做我做过的事。另外,有人能帮我实现案例1和案例2吗?

EN

回答 1

Stack Overflow用户

发布于 2018-07-31 03:30:12

因此,正如我理解你的问题,我写了一些代码,例如。我不能理解"value“选项。对我来说,期望的输出和方法的解释并不明显。如果你解释一下,我可以修改答案。

public class Main {

    public static void main(String[] args) {
        for (String s : findOccurence("prior", DayOfWeek.WEDNESDAY, 2, "14:00")){
            System.out.println(s);
        }
    }

    public static String[] findOccurence(String mode, DayOfWeek day, int occurrence, String eta) {
        LocalDate now = LocalDate.now();
        LocalDate start = LocalDate.of(now.getYear(), now.getMonth().plus(1), 1);
        LocalDate finish = start.plusDays(start.lengthOfMonth());

        List<LocalDate> dates = Stream.iterate(start, date -> date.plusDays(1))
                .limit(ChronoUnit.DAYS.between(start, finish))
                .filter(d -> d.getDayOfWeek() == day)
                .collect(Collectors.toList());

        String[] formattedEta = eta.split(":");

        if (occurrence > dates.size()) {
            throw new IndexOutOfBoundsException();
        }

        if (mode.equalsIgnoreCase("value")) {

        }
        else if (mode.equalsIgnoreCase("later")) {
            dates = Lists.reverse(dates);
        }

        //later and prior shares common logic

        return dates.stream()
                .limit(occurrence)
                .map(d -> day.toString()
                            + " "
                            + start.getMonth().name()
                            + " "
                            + d.getDayOfMonth()
                            + " "
                            + LocalTime.of(Integer.valueOf(formattedEta[0]), Integer.valueOf(formattedEta[1]), 0)
                            + " "
                            + start.getYear())
                .collect(Collectors.toList())
                .toArray(new String[occurrence]);
    }
}

输出为:

WEDNESDAY AUGUST 1 14:00 2018
WEDNESDAY AUGUST 8 14:00 2018
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51600118

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档