Java 如何在爪哇获得上周日和下周日?
我问了,但答案令人困惑,所以现在我想,如果我能找到过去的星期日和未来的星期日,中间的任何一天都是本周,未来的星期日和之后的星期日之间的任何一天都是下周,对吗 因此,我的新问题是:如何在Java中获得上一个星期日和下一个星期日?Java 如何在爪哇获得上周日和下周日?,java,date,Java,Date,我问了,但答案令人困惑,所以现在我想,如果我能找到过去的星期日和未来的星期日,中间的任何一天都是本周,未来的星期日和之后的星期日之间的任何一天都是下周,对吗 因此,我的新问题是:如何在Java中获得上一个星期日和下一个星期日? 我推荐Calendar.get(Calendar.DAY\u OF_WEEK)您可以尝试使用Calendar.WEEK\u OF_YEAR字段,该字段为您提供当前年份内周的数字表示形式 @Test public void testThisAndNextWeek() thr
我推荐Calendar.get(Calendar.DAY\u OF_WEEK)您可以尝试使用
Calendar.WEEK\u OF_YEAR
字段,该字段为您提供当前年份内周的数字表示形式
@Test
public void testThisAndNextWeek() throws Exception {
GregorianCalendar lastWeekCal = new GregorianCalendar(2010,
Calendar.DECEMBER, 26);
int lastWeek = lastWeekCal.get(Calendar.WEEK_OF_YEAR);
GregorianCalendar nextWeekCal = new GregorianCalendar(2011,
Calendar.JANUARY, 4);
int nextWeek = nextWeekCal.get(Calendar.WEEK_OF_YEAR);
GregorianCalendar todayCal = new GregorianCalendar(2010,
Calendar.DECEMBER, 27);
int currentWeek = todayCal.get(Calendar.WEEK_OF_YEAR);
assertTrue(lastWeekCal.before(todayCal));
assertTrue(nextWeekCal.after(todayCal));
assertEquals(51, lastWeek);
assertEquals(52, currentWeek);
// New Year.. so it's 1
assertEquals(1, nextWeek);
}
如果不使用更好的时间/日期包
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
Calendar now = new GregorianCalendar();
Calendar start = new GregorianCalendar(now.get(Calendar.YEAR),
now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH) );
while (start.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
start.add(Calendar.DAY_OF_WEEK, -1);
}
Calendar end = (Calendar) start.clone();
end.add(Calendar.DAY_OF_MONTH, 7);
System.out.println(df.format(now.getTime()) );
System.out.println(df.format(start.getTime()) );
System.out.println(df.format(end.getTime()) );
如果今天是星期日,则视为时间段的开始。如果你想要本周和下周的时间(从你的问题听起来),你可以用14代替最后的7。添加(…)行。将时间设置为午夜,以便比较介于开始和结束之间的另一个对象。这样如何:
Calendar c=Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
c.set(Calendar.HOUR_OF_DAY,0);
c.set(Calendar.MINUTE,0);
c.set(Calendar.SECOND,0);
DateFormat df=new SimpleDateFormat("EEE yyyy/MM/dd HH:mm:ss");
System.out.println(df.format(c.getTime())); // This past Sunday [ May include today ]
c.add(Calendar.DATE,7);
System.out.println(df.format(c.getTime())); // Next Sunday
c.add(Calendar.DATE,7);
System.out.println(df.format(c.getTime())); // Sunday after next
结果是:
Sun 2010/12/26 00:00:00
Sun 2011/01/02 00:00:00
Sun 2011/01/09 00:00:00
前两天之间的任何一天都是本周,后两天之间的任何一天都是下周。首先,不要使用Java的日期/时间包。有一个更好的实用程序包-下载并使用它 要确定您的时间是在本周、上周还是任何一周,请执行以下操作:
编辑: Joda时间可以是,以下是Joda如何做到这一点的示例:
// Get the date today, and then select midnight of the first day of the week
// Joda uses ISO weeks, so all weeks start on Monday.
// If you want to change the time zone, pass a DateTimeZone to the method toDateTimeAtStartOfDay()
DateTime midnightToday = new LocalDate().toDateTimeAtStartOfDay();
DateTime midnightMonday = midnightToday.withDayOfWeek( DateTimeConstants.MONDAY );
// If your week starts on Sunday, you need to subtract one. Adjust accordingly.
DateTime midnightSunday = midnightMonday.plusDays( -1 );
DateTime midnightNextSunday = midnightSunday.plusDays( 7 );
DateTime midnightSundayAfter = midnightNextSunday.plusDays( 7 );
Interval thisWeek = new Interval( midnightSunday, midnightNextSunday );
Interval nextWeek = new Interval( midnightNextSunday, midnightSundayAfter );
if ( thisWeek.contains( someDate.getTime() )) System.out.println("This week");
if ( nextWeek.contains( someDate.getTime() )) System.out.println("Next week");
我最近开发了一个用于解决此用例的工具:
Date today = new Date(2014, 7, 1); // assume today is 2014-07-01
Date previousSunday = today.previousOrSame(DayOfWeek.SUNDAY); // 2014-06-29
Date nextSunday = today.next(DayOfWeek.SUNDAY); // 2014-07-06
java.time
简而言之:
LocalDate.now().with( next( SUNDAY ) )
看这个
细节
我想我应该为后代添加一个Java8解决方案。使用,并在类中找到实现
这种方法也适用于其他时态类,如
导入
如前所述,它假定以下静态导入:
import java.time.LocalDate;
import static java.time.DayOfWeek.SUNDAY;
import static java.time.temporal.TemporalAdjusters.next;
import static java.time.temporal.TemporalAdjusters.previous;
我的解决方案:
LocalDate date = ...;
LocalDate newWeekDate = date.plusDays(1);
while (newWeekDate.getDayOfWeek() != DayOfWeek.SATURDAY &&
newWeekDate.getDayOfWeek() != DayOfWeek.SUNDAY) {
newWeekDate = date.plusDays(1);
}
给定下面的下周日代码,您可以轻松找到如何查找上周日
private static void nextSunday()引发异常
{
SimpleDataFormat sdf=新SimpleDataFormat(“yyyy-MM-dd”);
日历=Calendar.getInstance();
calendar.setTime(新日期());
int weekday=calendar.get(calendar.DAY/w);
int days=日历。星期日-工作日;
如果(天数<0)
{
天数+=7天;
}
calendar.add(calendar.DAY,年中的天);
System.out.println(sdf.format(calendar.getTime());
}
如果今天是星期天,该怎么办?如果今天是星期天,这是7天工作周的第一天,从今天早上00:00到下星期天的00:00是本周。我建议在使用Java内置的日期之前跳出窗口…;)跳出窗外可能有点激进,除非你在一楼工作,但我当然同意避免日历和设计糟糕的朋友。幸运的是,这个答案写完后,已经出来了。我热烈推荐它。我喜欢joda time,但如果有一个代码示例,也许会更好?;)你说得对,我本应该这么做的。Edited.FYI,该项目现在处于,建议迁移到类。看,这不是最好的答案。java.util.Date和.Calendar类非常糟糕,甚至Sun/Oracle都放弃了它们。新类现在捆绑在Java8中,以取代旧类。灵感来源于java.time,并具有一些卓越的功能。如果您不能使用java 8,此解决方案非常漂亮!在Android上,我们仅限于Java6API!对于那些寻求Java8解决方案的人,我在下面发布了一个答案。在三星手机上,它给出的是周日而不是过去的周日,所以我减去了一周。如果(c.getTimeInMillis()>today.getTimeInMillis()){c.set(Calendar.WEEK\u OF_MONTH,today.get(Calendar.WEEK\u OF_MONTH)-1)}FYI,那么像、java.text.SimpleDateFormat
这样非常麻烦的日期时间类现在被java 8和更高版本中内置的类所取代。参见.Great solution,ThanksFYI,非常麻烦的日期时间类,如和java.text.simpleDataFormat
,现在被内置在Java8和更高版本中的类所取代。请参阅。请参阅中使用java.time的现代解决方案。这在Android上对于支持API版本<26仍然很有用
LocalDate date = ...;
LocalDate newWeekDate = date.plusDays(1);
while (newWeekDate.getDayOfWeek() != DayOfWeek.SATURDAY &&
newWeekDate.getDayOfWeek() != DayOfWeek.SUNDAY) {
newWeekDate = date.plusDays(1);
}
private static void nextSunday() throws ParseException
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
int days = Calendar.SUNDAY - weekday;
if (days < 0)
{
days += 7;
}
calendar.add(Calendar.DAY_OF_YEAR, days);
System.out.println(sdf.format(calendar.getTime()));
}