微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

与JAVA中的Oracle next_day函数等效

如何解决与JAVA中的Oracle next_day函数等效

我想制作一个类似于oracle的next_day()函数方法,以下是我的代码,该代码在MONDAY,TUESDAY等接受n个工作日的日期,并返回从输入日期。

/**
 * @param date
 * @param weekDay
 * @return Gives the date for next weekday specified in the parameter.
 */    
public Date getNextDay(Date value,String weekday) {
    Calendar date1 = Calendar.getInstance();
    date1.setTime(value);
        
    if (weekday.equalsIgnoreCase("MONDAY")) {
        while (date1.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            date1.add(Calendar.DATE,1);
        }
    } else if (weekday.equalsIgnoreCase("TUESDAY")) {
        while (date1.get(Calendar.DAY_OF_WEEK) != Calendar.TUESDAY) {
            date1.add(Calendar.DATE,1);
        }
    } else if (weekday.equalsIgnoreCase("WednESDAY")) {
        while (date1.get(Calendar.DAY_OF_WEEK) != Calendar.WednESDAY) {
            date1.add(Calendar.DATE,1);
        }
    } else if (weekday.equalsIgnoreCase("THURSDAY")) {
        while (date1.get(Calendar.DAY_OF_WEEK) != Calendar.THURSDAY) {
            date1.add(Calendar.DATE,1);
        }
    } else if (weekday.equalsIgnoreCase("FRIDAY")) {
        while (date1.get(Calendar.DAY_OF_WEEK) != Calendar.FRIDAY) {
            date1.add(Calendar.DATE,1);
        }
    } else if (weekday.equalsIgnoreCase("SATURDAY")) {
        while (date1.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) {
            date1.add(Calendar.DATE,1);
        }
    } else {
        while (date1.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
            date1.add(Calendar.DATE,1);
        }
    }
                
    return date1.getTime();
}

请提出一种更好的方法

解决方法

我建议您使用modern java.time日期时间API进行操作。从 Trail: Date Time 了解有关现代日期时间API的更多信息。 java.util日期时间API和SimpleDateFormat已过时且容易出错。如果您不使用Java-8,则仍可以通过ThreeTenABP库使用Java-8日期时间API。

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

public class Main {
    public static void main(String[] args) {
        // Tests

        // Next occurrence
        System.out.println(getNextDay(LocalDate.now(),"Monday"));
        System.out.println(getNextDay(LocalDate.now(),"Wednesday"));

        // Same (if date falls on the given day) or next occurrence
        System.out.println(getSameOrNextDay(LocalDate.now(),"Monday"));
        System.out.println(getSameOrNextDay(LocalDate.now(),"Wednesday"));
    }

    static LocalDate getNextDay(LocalDate value,String weekday) {
        return value.with(TemporalAdjusters.next(DayOfWeek.valueOf(weekday.toUpperCase())));
    }

    static LocalDate getSameOrNextDay(LocalDate value,String weekday) {
        return value.with(TemporalAdjusters.nextOrSame(DayOfWeek.valueOf(weekday.toUpperCase())));
    }
}

输出:

2020-09-28
2020-09-30
2020-09-28
2020-09-23
,

如果您对第二天感兴趣,我建议您使用java.time.LocalDate而不是java.util.Date

以下代码接受LocalDate这样的String(必须是一周中的一整天,用大写字母表示),并返回代表最近日期的LocalDate星期几:

public static LocalDate nextDay(LocalDate sourceDay,String weekday) {
    // parse the day of week to an enum value
    DayOfWeek dayOfWeek = DayOfWeek.valueOf(weekday);
    // check if the day is the same as the one of the given LocalDate
    if (sourceDay.getDayOfWeek().equals(dayOfWeek)) {
        // and return the LocalDate that's a week later
        return sourceDay.plusWeeks(1);
    } else {
        // otherwise add a day to the given date
        LocalDate nextDayOfWeek = sourceDay.plusDays(1);
        // and do that until the day of week of the given date is reached
        while (nextDayOfWeek.getDayOfWeek() != dayOfWeek) {
            nextDayOfWeek = nextDayOfWeek.plusDays(1);
        }
        // then return the future date
        return nextDayOfWeek;
    }
}

您可以像这样在main中使用它:

    public static void main(String[] args) {
        System.out.println(nextDay(LocalDate.now(),"FRIDAY")
                            .format(DateTimeFormatter.ofPattern("uuuu-MM-dd '('EEEE')'",Locale.ENGLISH)));
    }

输出(今天 2020-09-23):

2020-09-25 (Friday)
,

使用“新”的java.time.DayOfWeekjava.time.LocalDate类:

public LocalDate getNextDay(
    final LocalDate value,final DayOfWeek day
)
{
    int currentDay = value.getDayOfWeek().getValue();
    int expectedDay = day.getValue();
    if ( currentDay >= expectedDay )
    {
        expectedDay += 7;
    }
    return value.plusDays( expectedDay - currentDay );
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。