2011-10-11 09:53
浏览 80


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?


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.


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


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?

图片转代码服务由CSDN问答提供 功能建议


  1. 如何根据JAVA中的具体日期找到一周中的哪一天?

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


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


      GregorianCalendar g = new GregorianCalendar(年,月,日,小时,分钟); 
     int dayOfWeek = g.DAY_OF_WEEK; 


      Calendar cal = Calendar.getInstance(); 
    int dayOfWeek2 = cal.get(Calendar.DAY_OF_WEEK); 


  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drn34916 2011-10-11 10:12

    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();
    int weekday = calendar.get(Calendar.DAY_OF_WEEK);

    Find the difference between two times

    打赏 评论
  • douliang1900 2011-10-11 10:03

    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();
    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.

    打赏 评论

相关推荐 更多相似问题