如何以Java或JavaScript显示当天名称为星期五的月份或年份的日期?
例如,对于2011年12月,代码将显示:
答案 0 :(得分:4)
这是我最佳的Javascript尝试。
它的工作原理是通过算法确定月份的第一个星期五的日期,然后直接向前推进7天,直到月份结束。
// find all dates that fall on a particular day of the week
// for the given year and month
function getDaysOfMonth(year, month, dow) {
--month; // to correct for JS date functions
var d = new Date(year, month, 1); // get the first of the month
var dow_first = d.getDay(); // find out what DoW that was
var date = (7 + dow - dow_first) % 7 + 1; // and the first day matching dow
var dates = [];
d.setDate(date);
do {
dates.push(new Date(d)); // store a copy of that date
date += 7; // go forward a week
d.setDate(date);
} while (d.getMonth() === month); // until the end of the month
return dates;
}
演示,星期五(JS格式的第5天):
document.write(getDaysOfMonth(2011, 12, 5).join("<br>"));
有关完整的工作演示,请参阅http://jsfiddle.net/wLFmM/。
要获取整年的日期,只需将其调用12次,然后合并结果; - )
答案 1 :(得分:1)
在Java中:
for (int i = 1; i <= 31; i++) {
Calendar cal = new GregorianCalendar(2011, Calendar.DECEMBER, i);
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
// process date
}
}
在Javascript中:
for (i = 1; i <= 31; i++) {
var date = new Date(2011, 11, i);
if (date.getDay() == 5) {
// process date
}
}
修改强>
响应评论跳跃的简单逻辑:
int step = 1;
for (int i = 1; i <= 31; i += step) {
// ...
if (.....) {
step = 7;
// .....
}
}
答案 2 :(得分:1)
在Javascript中,
function getFridays(year,month){
var fridays=new Array();
var i=0;
var tdays=new Date(year, month, 0).getDate();
for(date=0;date<=tdays;date++) {
sdate=(date<10)?"0"+date:date;
dd=(month+1)+"/"+sdate+"/"+year;
var day=new Date(year,month,date);
if(day.getDay() == 5 )
{
fridays[i++]=dd;
}
}
return fridays;
}
答案 3 :(得分:0)
如下所示:
new SimpleDateFormat("yyyy-MM-dd EEEE", Locale.US).format(date));
2011-12-19 Monday
答案 4 :(得分:0)
在java中,您可以使用java.util.Calendar类。
Calendar c = Calendar.newInstance();
c.setTime(System.currentTimeInMills());
if (c.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
.. do your stuff here
}
你也可以迭代这几天:
Calendar c = Calendar.newInstance();
c.add(1, Calendar.DAY);
答案 5 :(得分:0)
这是我的JavaScript尝试。我怀疑这是非常低效的,你必须以JavaScript预期的方式提供月份,即1月是0,12月是11。
function showFridays(year, month) {
var dates = [];
var months = [];
if(!month){
for(var i=0; i<12; i++) {
months.push(i);
}
}
else {
months.push(month);
}
for(var i=0; i<months.length; i++){
var month_number = months[i];
for(var j=1; j<32; j++) {
var date = new Date(year, month_number, j)
// Because e.g. new Date(2011,2,31) will evaluate to 3rd March, check that date.getMonth() returns the same number as we passed in, so that we reject duplicate dates.
if(date.getDay() == 5 && date.getMonth() == month_number) {
dates.push(date);
}
}
}
for(var i=0; i<dates.length; i++) {
console.log(dates[i].toLocaleString());
}
}
答案 6 :(得分:0)
定义此功能:
function getFridays(month, year){
var ret = [];
for(var i = 1; i <= 31; i++){
var date = new Date();
date.setDate(i);
date.setMonth(month - 1);
date.setFullYear(year);
if(date.getDay() === 5){
var today = date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear();
ret.push(today);
}
}
return ret;
}
然后打电话给它,比如12月,就像这样:
document.write(getFridays(12, 2011));
您有 JavaScript 解决方案。
编辑:根据对其他解决方案的评论更新逻辑:
function getFridays(month, year){
var ret = [];
for(var i = 1; i <= 7; i++){
var date = new Date();
date.setDate(i);
date.setMonth(month - 1);
date.setFullYear(year);
if(date.getDay() === 5){
var today = date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear();
ret.push(today);
for(var j = 1; j < 5; j++){
var d = date.getDate() + j * 7;
if(d <= 31){
ret.push((d) + '/' + (date.getMonth() + 1) + '/' + date.getFullYear());
}
}
}
}
return ret;
}
基本上,它的作用是,它会查找本月的第一个星期五,然后只需在其他星期五添加7个星期五,直到你出去这个月为止。请注意,我没有添加任何逻辑来检查是否有不到31天的有效期(半个月)。如果您使用它,可能需要改进第二个功能。
答案 7 :(得分:0)
以下是Java中的一个示例,适用于2011年12月。更改日期以适合您的使用案例:
Calendar beginCalendar = Calendar.getInstance();
//initialize the date to the first day the desired year and month
//Below example initializes to 1st of December 2011
beginCalendar.set(2011,Calendar.DECEMBER,1);
Calendar endCalendar = Calendar.getInstance();
//set the end date to the end of the month using the begin date
endCalendar.set(beginCalendar.get(Calendar.YEAR),beginCalendar.get(Calendar.MONTH),beginCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
//loop through till we hit the first friday of the month
while(beginCalendar.get(Calendar.DAY_OF_WEEK) != Calendar.FRIDAY){
beginCalendar.add(Calendar.DATE,1);
}
//loop from the first friday of the month till the end of month and add 1 week in each iteration.
while (beginCalendar.compareTo(endCalendar) <= 0) {
System.out.println(beginCalendar.getTime().toString());//this prints all the fridays in the given month
beginCalendar.add(Calendar.WEEK_OF_YEAR, 1);
}