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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:38:38  来源:igfitidea点击:

If I have a specific date of a day, how do I get the date of that day in the previous week?

javadatetimedatecalendar

提问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 LocalDateclass 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 Monthenum. 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.

查看此代码在 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 TemporalAdjustersclass (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 类?

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 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多

回答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_MONTHor WEEK_OF_YEAR

使用WEEK_OF_MONTHWEEK_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.Datefrom DateTime as:

您可以java.util.Date从 DateTime获取:

Date javaDate=jodaDateTime.toDate();