获取一周的第一个日期 [英] fetch the first date of the week
本文介绍了获取一周的第一个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在java中使用日历和日期获取本周的第一个日期,输入为::
1.每月的周数
2.月
3.年
I want to fetch the first date of the week in java using Calendar and Date with the inputs as :: 1. Week number of the month 2. Month 3. year
Calendar cal = Calendar.getInstance();
int weekOfMonth = Calendar.WEEK_OF_MONTH;
System.out.println("weekOfMonth : " + weekOfMonth);
此后我不知道继续。
推荐答案
Calendar类中有一个方法getFirstDayOfWeek(),它将返回0表示星期日,1表示星期一等等。以下代码片段基于此,并将给你一周的第一个日期。
There is a method getFirstDayOfWeek() in Calendar class that will return the day in 0 for Sunday, 1 for Monday and so on.The following code snippet is based on this and will give you the first date of the week.
private static Date getFirstDateOfWeek(){
Calendar cal = Calendar.getInstance();
Date d = Calendar.getInstance().getTime();
int currDay = d.getDay(); // getting the current day
int startDay = (currDay - cal.getFirstDayOfWeek()) + 1; // calculate the difference of number days to the current date from the first day of week
d.setDate(d.getDate() - startDay); // setting the date accordingly.
return d;
}
希望这会对你有帮助。
这篇关于获取一周的第一个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文