如何在点击前一天从数据库中获取数据

问题描述 投票:0回答:1

我正在构建我的应用程序。单击“上一个”按钮时,我想从数据库中获取文档。然后应显示前一天的文件(例如 11 个文件)。当我再次点击按钮时,应该会显示两天前的文件(例如 5 个文件)。当我再次点击按钮时,应该会显示三天前的文档(例如 8 个文档)等等

我有一种从当天获取文件的方法,但我不知道如何让每次点击将日期后移一天并获取文件。

uztId 是来自数据库的用户 ID。

dateOfIssue 是数据类型。

我的本地日期是“dd.MM.yyyy”

控制器:

@RequestMapping(value = "/getDocsPrevDay/{uztId}", method = RequestMethod.GET)
@ResponseBody
@ResponseStatus(HttpStatus.OK)
@ApiOperation(value = "Get documents previous day and again")
@ApiResponses({
        @ApiResponse(code = 200, message = "Success", response = ResponseApi.class)
})
public List<Documents> getPrevDay(@PathVariable("uztId") Integer uztId) {
    return documentsDao.getPrevDay(uztId);
}

道:

private static final long ONE_DAY_MILLI_SECONDS = 24 * 60 * 60 * 1000;
private static final String DATE_FORMAT = "dd.MM.yyyy";

public List<Documents> getPrevDay(Integer uztId) {
    java.util.Date currentDate = new java.util.Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);

    long previousDayMilliSeconds = currentDate.getTime() - ONE_DAY_MILLI_SECONDS;
    java.util.Date previousDate = new Date(previousDayMilliSeconds);
    String previousDateStr = dateFormat.format(previousDate);

    System.out.println("current date: " + dateFormat.format(currentDate));
    System.out.println("previous: " + previousDateStr);

    List<Documents> dokumentsPrevious = (List<Documents>) getCurrentSession()
            .createQuery("from Documents where uztId = :uztId and dateOfIssue = current_date()")
            .setParameter("uztId", uztId)
            .getResultList();

    return dokumentsPrevious;
}
java spring-boot hql
1个回答
0
投票

我正在解决我的问题。

祝你有美好的一天。

控制器:

@RequestMapping(value = "/getPrevDay/{uztId}/{daysOffset}", method = RequestMethod.GET)
@ResponseBody
@ResponseStatus(HttpStatus.OK)
@ApiOperation(value = "Każdy klik wyświetla dokumenty z pobranego dnia - 1")
@ApiResponses({
    @ApiResponse(code = 200, message = "Success", response = ResponseApi.class)
})
public List<Documents> getPrevDay(@PathVariable("uztId") Integer uztId,@PathVariable Integer daysOffset) {
    return documentsDao.getPrevDay(uztId, daysOffset);
}

道:

public List<Documents> getPrevDay(Integer uztId, Integer daysOffset) {

    ZoneId defaultZoneId = ZoneId.systemDefault();
    LocalDate myVariable= LocalDate.now();
    myVariable= myVariable.plusDays(daysOffset);
    Date prevDay = Date.from(myVariable.atStartOfDay(defaultZoneId).toInstant());

    List<Documents> dokumentsPrevious = (List<Documents>) getCurrentSession()
        .createQuery("from Documents where uztId = :uztId and dataOfIssue = :dateOfIssue order by dateOfIssue DESC")
        .setParameter("uztId", uztId)
        .setParameter("dateOfIssue ", prevDay)
        .getResultList();

    return dokumentsPrevious;
}
© www.soinside.com 2019 - 2024. All rights reserved.