Java在特定日期前一天获取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26599946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Get One Day Before Specific Date
提问by
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:
我有一个字符串过期日期。但是我需要在过期日期下降的前一天执行一些 SQL 语句。我得到我的过期日期和:
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();
}
Then, I got an if statement to perform SQL statement:
然后,我得到了一个 if 语句来执行 SQL 语句:
if(expiredDate.equals(currentDate)){
promptExtensionDialog();
}
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?
我想要实现的是 if 语句,而不是 expiredDate 本身,我需要在过期日期前一天获取并与当前日期进行比较。我想知道如何实现这一目标?
Thanks in advance.
提前致谢。
EDIT
编辑
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?
这会返回下一个日期而不是上一个日期。你有什么想法?
采纳答案by Sergio
Using Java's (pre-8) built-in Date and Time API will eat you alive. Use JodaTimefor complex DateTime manipulations.
使用 Java (pre-8) 内置的日期和时间 API 会让你生不如死。使用JodaTime进行复杂的 DateTime 操作。
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);
回答by Eric B.
Have you tried JodaTime? It is a fantastic library to do date manipulation easily. In fact, a lot of Java 8 date handling are derived from JodaTime.
你试过JodaTime吗?这是一个很棒的库,可以轻松地进行日期操作。事实上,Java 8 的很多日期处理都是从 JodaTime 派生而来的。
For your needs, you could do something like:
根据您的需要,您可以执行以下操作:
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy");
DateTime dt = formatter.parseDateTime(expiredDate);
DateTime dayBefore = dt.minusDays(1);
回答by Basil Bourque
The other two answers are basically correct. But they omit the crucial issue of time zones and start of day. If you want all of yesterday, do something like the following.
另外两个答案基本正确。但是他们忽略了时区和一天开始的关键问题。如果您想要昨天的所有内容,请执行以下操作。
DateTimeZone zone = DateTimeZone.forID( "America/Montreal" );
DateTime now = DateTime.now( zone );
DateTime yesterdayStart = now.minusDays( 1 ).withTimeAtStartOfDay();
Convert to a java.sql.Timestamp.
java.sql.Timestamp ts = new java.sql.Timestamp( yesterdayStart.getMillis() );