dounie0889
2011-10-11 09:53
浏览 80

在android中根据日期查找一天

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?

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

我有两个问题:

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

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

    Thanx,

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

    1。

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

    2。

      Calendar cal = Calendar.getInstance(); 
    cal.set(年,月,日,小时,分钟);  
    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();
    
    calendar.setTime(specific_date);
    
    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();
    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.

    打赏 评论

相关推荐 更多相似问题