Java在特定日期之前提前一天

编程入门 行业动态 更新时间:2024-10-12 20:23:48
本文介绍了Java在特定日期之前提前一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个字符串过期日期。但是我需要在过期日期的前一天执行一些SQL语句。我得到我的过期日期并通过以下方式获取:

I have a String expired date. But I need to perform some SQL statement the day before expired date falls. I get my expired date and by:

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); String expiredDate = null; String currentDate = dateFormat.format(new Date()); Calendar cal = Calendar.getInstance(); try { cal.setTime(dateFormat.parse(loanDate)); cal.add(Calendar.WEEK_OF_YEAR, 2); expiredDate = dateFormat.format(cal.getTimeInMillis()); cal.add(Calendar.WEEK_OF_YEAR, -2); } catch (ParseException e) { e.printStackTrace(); }

然后,我得到了一条if语句来执行SQL语句:

Then, I got an if statement to perform SQL statement:

if(expiredDate.equals(currentDate)){ promptExtensionDialog(); }

我要实现的是if语句,而不是expiredDate本身,我需要在过期日期前一天到来,并与当前日期进行比较。我想知道如何实现这一目标?

What I am trying to achieve is for the if statement, instead of the expiredDate itself, I need to get one day before the expired date and compare with the current date. I wonder how to achieve this?

预先感谢。

编辑

try { cal.setTime(dateFormat.parse(expiredDate)); cal.add(Calendar.DATE, -1); expiredDate = dateFormat.format(cal.getTimeInMillis()); } catch (ParseException e) { e.printStackTrace(); } Toast.makeText(LoanBook.this, expiredDate, Toast.LENGTH_LONG) .show();

这将返回下一个日期而不是上一个日期。您有任何想法吗?

This returns me the next date instead of previous date. Do you have any ideas?

推荐答案

使用Java的内置(8之前的版本)Date and Time API可以使您活得死活。使用 JodaTime 进行复杂的DateTime操作。

Using Java's (pre-8) built-in Date and Time API will eat you alive. Use JodaTime for complex DateTime manipulations.

获取前一天就这么简单。

Getting the previous day is as simple as this.

DateTime dateTime = new DateTime(); System.out.println(dateTime); System.out.println(dateTime.minusDays(1));

如果您不需要任何外部库:

If you don't want any external libraries:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String strDate = "2014-10-28"; Date date = sdf.parse(strDate); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DATE, -1); Date yesterday = calendar.getTime(); System.out.println(yesterday); System.out.println(date);

更多推荐

Java在特定日期之前提前一天

本文发布于:2023-11-24 02:29:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623639.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   在特定   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!