Java:从日历中获取一周中的任何一天

发布于 2024-09-26 20:01:53 字数 373 浏览 2 评论 0原文

使用Calendar我可以获得星期、年份和当天的所有详细信息。我如何导航到该周的特定一天?

比如说,calendar.get(Calendar.DAY_OF_WEEK); 返回 3,这意味着星期二。现在,我想去说那周的星期五或那周的任何其他一天。我怎样才能做到这一点?

感谢您的回复。我想我需要让场景更加清晰。 基本上,我试图在指定时间段内禁用系统中的电子邮件警报。 我得到的值如下: 禁用开始 =“周五 19:00” disableEnd = "SUNDAY-19:00"

现在,我需要验证是否应在特定时间发送电子邮件。 例如,如果今天 = 星期四,请随时发送电子邮件 但是,如果今天 = 星期六,则任何时间都无法按照上述值发送。

Using Calendar I can get the week, year and all details for the current day. How can I navigate to a particualr day in that week?

Say, calendar.get(Calendar.DAY_OF_WEEK); returns 3, which means a Tuesday. Now, I want to go to say Friday for that week or any other day in that week. How can I do that?

Thanks for your replies. I think I need to make the scenario more clear.
Basically, I am trying to disable email alerts in my system during specified period.
I get values like:
disableStart = "FRIDAY-19:00"
disableEnd = "SUNDAY-19:00"

Now, i need to verify if email should be sent at a particular time.
e.g. if today = Thursday any time, send email
but, if today = Saturday any time can't send as per values above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

温暖的光 2024-10-03 20:01:53

如果我理解正确,您可以使用 Calendar.set(Field, value) 方法。

SimpleDateFormat f = new SimpleDateFormat("dd-MM-yyyy");
Calendar c = Calendar.getInstance();
System.out.println(c.get(Calendar.DAY_OF_WEEK));
System.out.println(f.format(c.getTime()));
c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
System.out.println(c.get(Calendar.DAY_OF_WEEK));
System.out.println(f.format(c.getTime()));

产生输出

6
08-10-2010
3
05-10-2010

If I understand correctly you can use the Calendar.set(Field, value) method.

SimpleDateFormat f = new SimpleDateFormat("dd-MM-yyyy");
Calendar c = Calendar.getInstance();
System.out.println(c.get(Calendar.DAY_OF_WEEK));
System.out.println(f.format(c.getTime()));
c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
System.out.println(c.get(Calendar.DAY_OF_WEEK));
System.out.println(f.format(c.getTime()));

Produces the output

6
08-10-2010
3
05-10-2010
无敌元气妹 2024-10-03 20:01:53
Calendar c = Calendar.getInstance();
Date date = new Date();
c.setTime(date);
System.out.println("Today:  " + c.getTime());
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
System.out.println("MONDAY: " + c.getTime());
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
System.out.println("TUESDAY: " + c.getTime());
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
System.out.println("WEDNESDAY: " + c.getTime());
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
System.out.println("THURSDAY: " + c.getTime());
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
System.out.println("FRIDAY: " + c.getTime());
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
System.out.println("SATURDAY: " + c.getTime());
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
System.out.println("SUNDAY: " + c.getTime());

给出:

Today:  Fri Oct 08 15:45:14 CEST 2010
MONDAY: Mon Oct 04 15:45:14 CEST 2010
TUESDAY: Tue Oct 05 15:45:14 CEST 2010
WEDNESDAY: Wed Oct 06 15:45:14 CEST 2010
THURSDAY: Thu Oct 07 15:45:14 CEST 2010
FRIDAY: Fri Oct 08 15:45:14 CEST 2010
SATURDAY: Sat Oct 09 15:45:14 CEST 2010
SUNDAY: Sun Oct 10 15:45:14 CEST 2010

这似乎意味着,至少在我的系统上,每周从星期一开始。

Calendar c = Calendar.getInstance();
Date date = new Date();
c.setTime(date);
System.out.println("Today:  " + c.getTime());
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
System.out.println("MONDAY: " + c.getTime());
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
System.out.println("TUESDAY: " + c.getTime());
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
System.out.println("WEDNESDAY: " + c.getTime());
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
System.out.println("THURSDAY: " + c.getTime());
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
System.out.println("FRIDAY: " + c.getTime());
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
System.out.println("SATURDAY: " + c.getTime());
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
System.out.println("SUNDAY: " + c.getTime());

Gives:

Today:  Fri Oct 08 15:45:14 CEST 2010
MONDAY: Mon Oct 04 15:45:14 CEST 2010
TUESDAY: Tue Oct 05 15:45:14 CEST 2010
WEDNESDAY: Wed Oct 06 15:45:14 CEST 2010
THURSDAY: Thu Oct 07 15:45:14 CEST 2010
FRIDAY: Fri Oct 08 15:45:14 CEST 2010
SATURDAY: Sat Oct 09 15:45:14 CEST 2010
SUNDAY: Sun Oct 10 15:45:14 CEST 2010

Which seams to mean that, at least on my system, the weeks starts on monday.

若无相欠,怎会相见 2024-10-03 20:01:53

cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);

cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);

ゞ花落谁相伴 2024-10-03 20:01:53

感谢凯文和莫里斯的回答。他们确实给了我一个起点。

我以这个测试代码结束,以防它对任何人有帮助。

private static Date getTimeForAnyDayInWeek(int nDay, int nHour, int nMin)
{
    Calendar c = Calendar.getInstance();
    c.setFirstDayOfWeek(Calendar.MONDAY);
    Date date = Calendar.getInstance().getTime();
    c.setTime(date);
    c.set(Calendar.DAY_OF_WEEK, nDay);
    c.set(Calendar.HOUR_OF_DAY, nHour);
    c.set(Calendar.MINUTE, nMin);
    return c.getTime();
}

public static void main(String[] args)
{
    Date start = getTimeForAnyDayInWeek(6, 19, 00);
    Date end = getTimeForAnyDayInWeek(8, 19, 00);
    Date c = new Date();

    if (start.before(c) && c.before(end))
        System.out.println("BLOCK");
    else
        System.out.println("SEND");
}

谢谢,
阿努巴夫

Thanks to Kevin and Maurice for the answers. They really gave me the start point.

I ended with this test code, in case it helps anyone.

private static Date getTimeForAnyDayInWeek(int nDay, int nHour, int nMin)
{
    Calendar c = Calendar.getInstance();
    c.setFirstDayOfWeek(Calendar.MONDAY);
    Date date = Calendar.getInstance().getTime();
    c.setTime(date);
    c.set(Calendar.DAY_OF_WEEK, nDay);
    c.set(Calendar.HOUR_OF_DAY, nHour);
    c.set(Calendar.MINUTE, nMin);
    return c.getTime();
}

public static void main(String[] args)
{
    Date start = getTimeForAnyDayInWeek(6, 19, 00);
    Date end = getTimeForAnyDayInWeek(8, 19, 00);
    Date c = new Date();

    if (start.before(c) && c.before(end))
        System.out.println("BLOCK");
    else
        System.out.println("SEND");
}

Thanks,
Anubhav

横笛休吹塞上声 2024-10-03 20:01:53

这是为什么 jodatime 如此优秀的一个完美例子,这是我

  DateTime dt = new DateTime(); //current datetime, jodatime format
  DateTime fridayLastWeek = dt.minusWeeks(1).dayOfWeek().setCopy("Friday");
  Date convertedtorubbishdateformat = fridayLastWeek.toDate();

以前浪费的 类似代码很多时间都在使用标准的 java 日期/日历。然后我得到了jodatime,你不会后悔的,它显然会在未来被用作标准java的一部分。我很长时间都没有费心下载这个罐子,我希望我已经这么做了,你不会后悔的。

This is a perfect example of why jodatime is so good, here is my similar code

  DateTime dt = new DateTime(); //current datetime, jodatime format
  DateTime fridayLastWeek = dt.minusWeeks(1).dayOfWeek().setCopy("Friday");
  Date convertedtorubbishdateformat = fridayLastWeek.toDate();

I used to waste so much time witht he standard java date/calendar. Then i got jodatime, you wont regret, it apparently will be used as part of standard java in the future. I didn;t bother downlaoding the jar for for ages, I wish I had done, you won't regret it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文