获取前几个月的第一个和最后一个日期

nukf8bse  于 2021-07-06  发布在  Java
关注(0)|答案(4)|浏览(309)

我需要从数据库中查询上个月的数据。所以我需要上个月的第一个和最后一个日期,以便能够进行“between”sql查询。
当然,第一次约会总是从1开始,但是硬编码看起来很糟糕。另外,硬编码日期格式“dd-mm-yyyy”似乎是一个糟糕的做法。有没有更简单的方法?现在我有一些业余的工作,比如:

YearMonth thisMonth    = YearMonth.now();
        YearMonth lastMonth    = thisMonth.minusMonths(1);

        String dd = Integer.toString(lastMonth.lengthOfMonth());
        String mm = Integer.toString(lastMonth.getMonthValue());
        String yyyy = Integer.toString(lastMonth.getYear());

        String startDate = "01-" + mm + "-" + yyyy;
        String endDate = dd + "-" + mm + "-" + yyyy;

        System.out.println("startDate: " + startDate);
        System.out.println("endDate: " + endDate);
iovurdzv

iovurdzv1#

可以使用temporaladjusters.firstdayofmonth()和temporaladjusters.lastdayofmonth()
参考文件:https://docs.oracle.com/javase/8/docs/api/java/time/temporal/temporaladjusters.html#lastdayofmonth--

LocalDate currentDate = LocalDate.now();
LocalDate currentDateLastMonth = LocalDate.of(
                currentDate.getYear(), 
                currentDate.getMonth().minus(1), 
                currentDate.getDayOfMonth());

LocalDate firstDayOfMonth = currentDateLastMonth.with(TemporalAdjusters.firstDayOfMonth());
LocalDate lastDayOfMonth = currentDateLastMonth.with(TemporalAdjusters.lastDayOfMonth());

System.out.println("first day of the month "+ firstDayOfMonth);
System.out.println("last day of the month "+ lastDayOfMonth);
thigvfpy

thigvfpy2#

将localdate对象传输到sql查询

不要将第一个和最后一个日期作为字符串发送到数据库查询。自jdbc 4.2传输以来 LocalDate 物体。在java中有几种方法可以进行计算。我喜欢的方法是 YearMonth 就像你在这个问题上所做的那样。

YearMonth thisMonth    = YearMonth.now(ZoneId.of("Atlantic/Reykjavik"));
    YearMonth lastMonth    = thisMonth.minusMonths(1);

    LocalDate startDate = lastMonth.atDay(1);
    LocalDate endDate = lastMonth.atEndOfMonth();

    System.out.println("startDate: " + startDate);
    System.out.println("endDate: " + endDate);

今天运行时的输出:

startDate: 2020-11-01
endDate: 2020-11-30

要传递给jdbc:

String query = "select * from your_table where your_date_col between ? and ?;";
    PreparedStatement statement = yourDatabaseConnection.prepareStatement(query);
    statement.setObject(1, startDate);
    statement.setObject(2, endDate);
kuarbcqp

kuarbcqp3#

所以我需要上个月的第一个和最后一个日期,以便能够进行“between”sql查询。
您可以直接在数据库中进行计算。在oracle中,您可以将上个月的第一天和最后一天作为 date 就像这样:

where mydate between add_months(trunc(sysdate, 'month'), -1)
                 and last_day(add_months(trunc(sysdate, 'month'), -1))

不过,一般来说,你并不需要 between 最后一天。您可以使用半开放时间间隔-这也更安全,因为它考虑了属于一个月最后一天的日期:

where mydate >= add_months(trunc(sysdate, 'month'), -1)
  and mydate <  trunc(sysdate, 'month')
pwuypxnk

pwuypxnk4#

以下调整器可供您使用:

temporaladjusters.firstdayofmonth()

临时调整器。lastdayofmonth()

演示:

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

class Main {
    public static void main(String[] args) {
        // Today's date
        LocalDate today = LocalDate.now();

        LocalDate oneMonthAgo = today.minusMonths(1);

        LocalDate firstDateOfLastMonth = oneMonthAgo.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate lastDateOfLastMonth = oneMonthAgo.with(TemporalAdjusters.lastDayOfMonth());

        System.out.println(firstDateOfLastMonth);
        System.out.println(lastDateOfLastMonth);
    }
}

输出:

2020-11-01
2020-11-30

在trail:date-time了解有关现代日期时间api的更多信息。

如何在jdbc 4.2中使用java 8日期和时间api(jsr-310):

String query = "SELECT column_name(s)
                FROM table_name
                WHERE column_name BETWEEN ? AND ?";

PreparedStatement st = conn.prepareStatement(query);
st.setObject(1, firstDateOfLastMonth);
st.setObject(2, lastDateOfLastMonth);
st.executeQuery();

//...Process ResultSet 

st.close();

相关问题