Java 如果我有一天的特定日期,我如何获得前一周的那一天的日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6545935/
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
If I have a specific date of a day, how do I get the date of that day in the previous week?
提问by Ian
SEE ANSWER FROM @Basil Bourque for most up to date answer
请参阅@Basil Bourque 的最新答案
For example, if I have a "Date" variable "date1" with a value of (dd/mm/yyy) 03/07/2011, which is a Sunday. How do I get the "Date" of the previous Sunday "date2"? which would have the value (dd/mm/yyyy) 26/06/2011.
例如,如果我有一个“日期”变量“date1”,其值为 (dd/mm/yyy) 03/07/2011,即星期日。如何获得上周日“date2”的“日期”?其值为 (dd/mm/yyyy) 26/06/2011。
Is there an easy way of doing it, for example:
有没有一种简单的方法来做到这一点,例如:
pseudo code:
伪代码:
Date date1 = (03/07/2011);
Date date2 = date1.subtractNumberOfDays(7);
采纳答案by Basil Bourque
tl;dr
tl;博士
LocalDate.of( 2011 , Month.JULY , 3 )
.minusWeeks( 1 )
2011-06-26
2011-06-26
java.time
时间
The Question and Answers use old outmoded date-time classes. Instead use the java.time classes.
问答使用旧的过时日期时间类。而是使用 java.time 类。
LocalDate
LocalDate
The LocalDate
class represents a date-only value without time-of-day and without time zone.
该LocalDate
级表示没有时间一天和不同时区的日期,唯一的价值。
LocalDate localDate = LocalDate.of( 2011 , Month.JULY , 3 );
Alternatively, pass an integer in second argument instead of the Month
enum. Pass 1-12 for January-December.
或者,在第二个参数中传递一个整数而不是Month
枚举。1-12 月通过 1-12。
Previous week
上个礼拜
You can subtract a week from the date.
您可以从日期中减去一周。
LocalDate weekPrior = localDate.minusWeeks( 1 );
See this code run live at IdeOne.com.
Previous day-of-week
前一周
If you want a specific day of the week, use a TemporalAdjuster
.
如果您想要一周中的特定日期,请使用TemporalAdjuster
.
Several such handy implementations provided in the TemporalAdjusters
class (note the plural 's').
TemporalAdjusters
类中提供了几个这样方便的实现(注意复数“s”)。
LocalDate priorTuesday = localDate.with( TemporalAdjusters.previous( DayOfWeek.TUESDAY ) ) ;
About java.time
关于 java.time
The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
该java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date
, Calendar
, & SimpleDateFormat
。
The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310。
Where to obtain the java.time classes?
从哪里获得 java.time 类?
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- The ThreeTenABPproject adapts ThreeTen-Backport(mentioned above) for Android specifically.
- See How to use ThreeTenABP….
- Java SE 8、Java SE 9及更高版本
- 内置。
- 具有捆绑实现的标准 Java API 的一部分。
- Java 9 添加了一些小功能和修复。
- Java SE 6和Java SE 7
- 多的java.time功能后移植到Java 6和7在ThreeTen-反向移植。
- 安卓
- 所述ThreeTenABP项目适应ThreeTen-反向移植(上述)为Android特异性。
- 请参阅如何使用ThreeTenABP ...。
The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
该ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如Interval
,YearWeek
,YearQuarter
,和更多。
回答by Petar Ivanov
You should use Calendar:
您应该使用日历:
Calendar date = new GregorianCalendar(2011, Calendar.JULY, 3);
date.add(Calendar.DAY_OF_MONTH, -7);
System.out.println(date.getTime());
You can create a calendar from date too:
您也可以从日期创建日历:
Date date1 = new Date(111, Calendar.JULY, 3);//the year field adds 1900 on to it.
Calendar date = new GregorianCalendar();
date.setTime(date1);
date.add(Calendar.DAY_OF_MONTH, -7);
date2 = date.getTime();
Be aware that:
意识到:
- Java uses 0 to represent January !
- Date(year, month, day) is deprecated since JDK version 1.1 !
- Java 使用 0 表示一月!
- Date(year, month, day) 自 JDK 1.1 版起已弃用!
See the GregorianCalendar JavaDoc:
请参阅 GregorianCalendar JavaDoc:
Constructs a GregorianCalendar with the given date set in the default time zone with the default locale. Parameters: year the value used to set the YEAR calendar field in the calendar. month the value used to set the MONTH calendar field in the calendar. Month value is 0-based. e.g., 0 for January. dayOfMonth the value used to set the DAY_OF_MONTH calendar field in the calendar.
使用默认语言环境在默认时区中设置给定日期,构造一个 GregorianCalendar。参数: year 用于设置日历中 YEAR 日历字段的值。月 用于在日历中设置 MONTH 日历字段的值。月份值是从 0 开始的。例如,0 表示一月。dayOfMonth 用于在日历中设置 DAY_OF_MONTH 日历字段的值。
回答by Thresh
A more clear approach:
更清晰的方法:
Calendar date = new GregorianCalendar(2011, 1, 1);
date.add(Calendar.WEEK_OF_MONTH, -1);
System.out.println(date.getTime());
use either WEEK_OF_MONTH
or WEEK_OF_YEAR
使用WEEK_OF_MONTH
或WEEK_OF_YEAR
回答by Tapas Bose
If you are willing to use Joda timeit will be very easy.
如果您愿意使用Joda 时间,那将非常容易。
An example:
一个例子:
DateTime toDay=new DateTime();
DateTime dateOfPreviousWeek=toDay.minusDays(7);
Another:
其他:
DateTime toDay = new DateTime(2011, 7, 1, 0, 0, 0, 0);
DateTime dateOfPreviousWeek = toDay.minusDays(7);
You can get java.util.Date
from DateTime as:
您可以java.util.Date
从 DateTime获取:
Date javaDate=jodaDateTime.toDate();