根据android中的日期查找一天

发布于 2024-12-09 11:26:51 字数 538 浏览 0 评论 0原文

我有 2 个问题:

  1. 如何根据 JAVA 中的特定日期查找星期几?

  2. 我想在Java或PHP中找到2次(每次包括日期和小时)之间的差异,有人可以帮助我吗?

谢谢,

编辑: 我仍然有一个问题,我没有成功找到特定日期的日期...我正在尝试两种方法,两种方法都不起作用。

1.

GregorianCalendar g = new GregorianCalendar(year, month, day, hour, min);
 int dayOfWeek=g.DAY_OF_WEEK;

2.

Calendar cal = Calendar.getInstance();
cal.set(year, month, day, hour, min);           
int dayOfWeek2 = cal.get(Calendar.DAY_OF_WEEK);

有人能给我另一个解决方案吗?

I have 2 questions:

  1. How can I find what is the day of the week according to a specific date in JAVA ?

  2. I want to find a difference between 2 times (each time include date and hour) in Java or PHP, someone can help me with that?

Thanx,

EDIT:
I still have a problem with that, I dont success to find the date of a specific date... i'm trying 2 ways, boths are not working.

1.

GregorianCalendar g = new GregorianCalendar(year, month, day, hour, min);
 int dayOfWeek=g.DAY_OF_WEEK;

2.

Calendar cal = Calendar.getInstance();
cal.set(year, month, day, hour, min);           
int dayOfWeek2 = cal.get(Calendar.DAY_OF_WEEK);

Someone cane give me another solution?

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

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

发布评论

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

评论(2

寄风 2024-12-16 11:26:51

虽然不是特别好,但标准 Java Calendar 类应该足以解决您的第一个问题。您可以使用 Date 对象设置 Calendar 实例的时间,然后访问 DAY_OF_WEEK 字段。是这样的:

Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

至于第二个问题,这取决于你想要如何衡量差异。如果您只需要以毫秒为单位的差异,则只需对每个 Date 实例调用 getTime() 方法,然后将其中一个减去另一个即可。如果您想要以秒、分钟、小时、天等形式表示,您可以简单地使用该值进行一些简单的算术运算。

While not particularly great, the standard Java Calendar class should be sufficient for solving your first problem. You can set the time of a Calendar instance using a Date object, then access the DAY_OF_WEEK field. Something like this:

Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

As for the second problem, it depends how you want the difference to be measured. If you just want the difference in milliseconds, you can simply call the getTime() method on each of your Date instances, and subtract one from the other. If you want it in terms of seconds, minutes, hours, days, etc you can simply do some simple arithmetic using that value.

少跟Wǒ拽 2024-12-16 11:26:51

请参阅这些链接,

查找星期几

根据查找星期几对于特定日期,

请尝试如下所示,

Calendar calendar=Calendar.getInstance();

calendar.setTime(specific_date);

int weekday = calendar.get(Calendar.DAY_OF_WEEK);

找出两个之间的差异次

Refer these links,

Find day of the week

to find the day of the week according to a specific date

try like below,

Calendar calendar=Calendar.getInstance();

calendar.setTime(specific_date);

int weekday = calendar.get(Calendar.DAY_OF_WEEK);

Find the difference between two times

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