java获取某个时间的上周一和周日

java获取某个时间的上周一和周日,第1张

mport javatextSimpleDateFormat;

import javautilCalendar;

public class Test{

public static void main(String args[]){

Calendar cal = CalendargetInstance();

//n为推迟的周数,1本周,-1向前推迟一周,2下周,依次类推

int n = 2;

String monday;

caladd(CalendarDATE, n7);

//想周几,这里就传几CalendarMONDAY(TUESDAY)

calset(CalendarDAY_OF_WEEK,CalendarMONDAY);

monday = new SimpleDateFormat("yyyy-MM-dd")format(calgetTime());

Systemoutprintln(monday);

}

}

'======================================================

' 函数 Date2Chinese

' 功能:获得中文日期的字符串(如一九九八年五月十二日)

' 参数: iDate 要转化的日期

' 返回: 中文日期的字符串

'======================================================

Function Date2Chinese(iDate)

Dim num(10)

Dim iYear

Dim iMonth

Dim iDay

num(0) = "〇"

num(1) = "一"

num(2) = "二"

num(3) = "三"

num(4) = "四"

num(5) = "五"

num(6) = "六"

num(7) = "七"

num(8) = "八"

num(9) = "九"

iYear = Year(iDate)

iMonth = Month(iDate)

iDay = Day(iDate)

Date2Chinese = num(iYear \ 1000) + _

num((iYear \ 100) Mod 10) + num((iYear _

\ 10) Mod 10) + num(iYear Mod _

10) + "年"

If iMonth >= 10 Then

If iMonth = 10 Then

Date2Chinese = Date2Chinese + _

"十" + "月"

Else

Date2Chinese = Date2Chinese + _

"十" + num(iMonth Mod 10) + "月"

End If

Else

Date2Chinese = Date2Chinese + _

num(iMonth Mod 10) + "月"

End If

If iDay >= 10 Then

If iDay = 10 Then

Date2Chinese = Date2Chinese + _

"十" + "日"

ElseIf iDay = 20 Or iDay = 30 Then

Date2Chinese = Date2Chinese + _

num(iDay \ 10) + "十" + "日"

ElseIf iDay > 20 Then

Date2Chinese = Date2Chinese + _

num(iDay \ 10) + "十" + _

num(iDay Mod 10) + "日"

Else

Date2Chinese = Date2Chinese + _

"十" + num(iDay Mod 10) + "日"

End If

Else

Date2Chinese = Date2Chinese + _

num(iDay Mod 10) + "日"

End If

End Function

%>

import javatextDateFormat;

import javatextSimpleDateFormat;

import javautilCalendar;

import javautilDate;

public class TestDate {

    public static void main(String[] args) {

        // 设置当前日期

        Calendar aCalendar = CalendargetInstance();

        aCalendarsetTime(new Date());

        // 取当前日期是星期几(week:星期几)

        int week = aCalendarget(CalendarDAY_OF_WEEK);

        if (week == 1) {

            week = 7;

        } else if (week == 0) {

            week = 6;

        } else {

            week -= 1;

        }

        // 取距离当前日期最近的周日与当前日期相差的天数(count:相差的天数。正数:之后的周日,负数:之前的周日)

        int count = 0;

        if (week <= 3) {

            count = -week;

        } else if (week >= 4) {

            count = 7 - week;

        }

        // 获取距离当前日期最近的周日日期

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd EE");

        Calendar c = CalendargetInstance();

        cadd(CalendarDAY_OF_WEEK, count);

        // 格式化并打印出距离当前日期最近的周日日期

        Systemoutprintln("当前日期: " + dfformat(aCalendargetTime()) + '\n' + "最近周日: " + dfformat(cgetTime()));

    }

}

代码

// 获取当前星期的星期一的日期,返回的是一个Date对象。

function getMonDate()

{

var d=new Date(),

day=dgetDay(),

date=dgetDate();

if(day==1)

return d;

if(day==0)

dsetDate(date-6);

else

dsetDate(date-day+1);

return d;

}

// 0-6转换成中文名称

function getDayName(day)

{

var day=parseInt(day);

if(isNaN(day) || day<0 || day>6)

return false;

var weekday=["星期天","星期一","星期二","星期三","星期四","星期五","星期六"];

return weekday[day];

}

// d是当前星期一的日期对象

var d=getMonDate();

var arr=[];

for(var i=0; i<7; i++)

{

arrpush(dgetFullYear()+'年'+(dgetMonth()+1)+'月'+dgetDate()+'日 ('+getDayName(dgetDay())+')');

dsetDate(dgetDate()+1);

}

显示

2013年3月18日 (星期一)

2013年3月19日 (星期二)

2013年3月20日 (星期三)

2013年3月21日 (星期四)

2013年3月22日 (星期五)

2013年3月23日 (星期六)

2013年3月24日 (星期天)

function GetWeekDate(time){

var now = new Date(time); //当前日期

thisnowDayOfWeek = nowgetDay(); //今天本周的第几天

thisnowYear = nowgetYear(); //当前年

thisnowMonth = nowgetMonth(); //月

thisnowDay = nowgetDate(); //日

thisbeginHour="09:00:00";

thisendHour="23:59:59";

thisnowYear += (thisnowYear < 2000) 1900 : 0; //

thisnowDayOfWeek = thisnowDayOfWeek==07:thisnowDayOfWeek; // 如果是周日,就变成周七

}

GetWeekDateprototypedate2str=function(x,y){//date2str(new Date(curTime),"yyyy-MM-dd")

var z ={y:xgetFullYear(),M:xgetMonth()+1,d:xgetDate(),h:xgetHours(),m:xgetMinutes(),s:xgetSeconds()};

return yreplace(/(y+|M+|d+|h+|m+|s+)/g,function(v) {return ((vlength>1"0":"")+eval('z'+vslice(-1)))slice(-(vlength>2vlength:2))});

}

GetWeekDateprototypeformatDate=function(date){//格局化日期:yyyy-MM-dd

var myyear = dategetFullYear();

var mymonth = dategetMonth()+1;

var myweekday = dategetDate();

//alert("formatDate"+myyear+":"+mymonth+":"+myweekday)

if(mymonth < 10){

mymonth = "0" + mymonth;

}

if(myweekday < 10){

myweekday = "0" + myweekday;

}

return (myyear+"-"+mymonth + "-" + myweekday);

}

GetWeekDateprototypegetWeekStartDate=function(){ //获得本周的开端日期

var weekStartDate = new Date(thisnowYear, thisnowMonth, thisnowDay - thisnowDayOfWeek+1);

return thisformatDate(weekStartDate);

}

GetWeekDateprototypegetWeekEndDate=function(){//获得本周的停止日期

var weekEndDate = new Date(thisnowYear, thisnowMonth, thisnowDay + (6 - thisnowDayOfWeek+1));

return thisformatDate(weekEndDate);

}

GetWeekDateprototypegetAWeedkYMD=function(){//获得本周周一~周日的年月日

var ymdArr=[];

for (var i = 0; i < 7; i++) {

ymdArr[i]=[];

//ymdArr[i][0]=thisformatDate(new Date(thisnowYear, thisnowMonth, thisnowDay - thisnowDayOfWeek+i+1));

ymdArr[i][0]=thisdate2str(new Date(thisnowYear, thisnowMonth, thisnowDay - thisnowDayOfWeek+i+1),'yyyy-MM-dd');

ymdArr[i][1]=thisdate2str(new Date(thisnowYear, thisnowMonth, thisnowDay - thisnowDayOfWeek+i+1), 'MM月dd日');

};

return ymdArr;

}

GetWeekDateprototypegetQishu=function(time){//获得本周是指定日期(712日)的第几周

var oNDate=new Date(time); //系统当前时间

var oEDate=new Date('2015/7/12 23:59:59');

var diff= oNDategetTime()-oEDategetTime();

//consolelog(diff)

//consolelog("相差天:"+diff/(2460601000))

//consolelog("相差期数:"+parseInt(diff/(2460601000))/7)

//consolelog("取整期数:"+Mathfloor(parseInt(diff/(2460601000))/7))

return (diff/(2460601000))/7;

}

GetWeekDateprototypegetWeeksDates=function(time){//获取历史周排行的周一到周日时间段

var qishu = (thisqishu || thisqishu==0)thisqishu:thisgetQishu(time);

//var qishu=thisgetQishu(time);

var WeeksTimes=[];//存放时间的二维数组

minYear=2015,

minMonth=7,

minDay=12;

for (var i = 0; i<qishu; i++) {

var sday;

var eday;

WeeksTimes[i]=[];

if(i==0){//如果离2015-07-12只相差1期

sday=minDay+1; //开始时间+1天

}else{//如果离2015-07-12相差期数>1

sday=minDay+1+7i;

}

eday=minDay+7(i+1);//结束时间+7天

WeeksTimes[i][0]=thisformatDate(new Date(minYear,minMonth-1,sday))+" "+thisbeginHour;

WeeksTimes[i][1]=thisformatDate(new Date(minYear,minMonth-1,eday))+" "+thisendHour;

};

//如果是一周的周日就不要减去一期

return WeeksTimes;

}

GetWeekDateprototypetodayData=function(json){//处理tender_list_weekjsp页面

var oQishu=$('qishu');

iQishu=thisqishu;//期数+1是因为,相差0期就是第1期

oThisWeekListBtn=$('total_list_btn'),//查看本周排行榜按钮

sTime=thisgetWeekStartDate()+" "+thisbeginHour,

eTime=thisgetWeekEndDate()+" "+thisendHour;

//1、修改期数

//consolelog("相差周:"+iQishu);

if(/^[1-9]\d$/test(iQishu) || iQishu==0){//整数 +3

oQishuhtml(iQishu1+3);

}else{

oQishuhtml(Mathfloor(iQishu)1+4);

}

oThisWeekListBtnattr("onclick","showWeekList('"+sTime+"','"+eTime+"')");//2、修改查看本周排行榜的起止时间

}

GetWeekDateprototypehistoryData=function(time){//处理tender_list_weekjsp页面

var oQishu=$('qishu'),

oDateList=$('#dateList'), //顶部导航时间

oHistoryList=$('#history_cont'),//历史周

aThisWeekYMD=thisgetAWeedkYMD(),//本周周一~周日的年月日

aThisWeekDates=thisgetWeeksDates(time);//获取历史周排行的周一到周日时间段

iQishu=thisqishu;

//consolelog("相差周:"+iQishu);

//1、修改期数,+4是加上前3期,在+1是如果间隔3期,当前就是第4期

//如果时间23:59:59是正整数(-1),如果是0整数(3),如果是非整数(向下取整),或负数(向上取整)但是负数不考虑

//oQishuhtml(iQishu1+3);

if(/^[1-9]\d$/test(iQishu) || iQishu==0){//整数 +3

oQishuhtml(iQishu1+3);

}else{

oQishuhtml(Mathfloor(iQishu)1+4);

}

//2、给顶部时间追加时间

for (var i = 0; i < aThisWeekYMDlength; i++) {

var str='<li data-time="'+aThisWeekYMD[i][0]+'">'+aThisWeekYMD[i][1]+'</li>';

$(str)appendTo(oDateList);

};

//3、给历史周排行榜添加周期数

if(thisqishu>1){//如果相差的期数大于1期

//consoledir(aThisWeekDates)

for (var j = 0; j < aThisWeekDateslength-1; j++) {

var iQiNum=j+4;

var str='<li onclick="showWeekList(\''+aThisWeekDates[j][0]+'\',\''+aThisWeekDates[j][1]+'\')">第'+iQiNum+'期</li>';

$(str)prependTo(oHistoryList);

};

}

}

GetWeekDateprototypeinit=function(time){

var iQishu=thisgetQishu(time),//期数+1是因为,相差0期就是第1期

json={};

jsonqishu=iQishu;

thisqishu=iQishu;

if($('#pageType')val()=="today"){//如果是tender_list_weekjsp页面

if(new Date(time)getTime() < new Date('2015/07/12 23:59:59')getTime()){//特殊处理时间小于20150712

$('qishu')html('3');

$('total_list_btn')attr("onclick","showWeekList('2015-07-03 09:00:00','2015-07-12 23:59:59')");

return false;

}

thistodayData(json);

}else if($('#pageType')val()=="history"){

if(new Date(time)getTime() < new Date('2015/07/12 23:59:59')getTime()){//特殊处理时间小于20150712

$('qishu')html('3');

$('#dateList')addClass('dateList2')html('<li data-time="2015-07-03">07月03日</li><li data-time="2015-07-04">07月04日</li><li data-time="2015-07-05">07月05日</li><li data-time="2015-07-06">07月06日</li><li data-time="2015-07-07">07月07日</li><li data-time="2015-07-08">07月08日</li><li data-time="2015-07-09">07月09日</li><li data-time="2015-07-10">07月10日</li><li data-time="2015-07-11">07月11日</li><li data-time="2015-07-12">07月12日</li>');

$('#history_cont')html('<li onclick="showWeekList(\'2015-06-26 09:00:00\',\'2015-07-02 23:59:59\')">第二期</li><li onclick="showWeekList(\'2015-06-19 12:00:00\',\'2015-06-25 23:59:59\')">第一期</li>');

return false;

}

thishistoryData(time);

}

//consoledir(thisgetWeeksDates(time));

};

我也是来找答案的,结果发现两位仁兄都差了点。

那就让我补上这最后这点吧

function getFirstDayOfWeek($year,$week)

{

$first_day = strtotime($year"-01-01");

$is_monday = date("w", $first_day) == 1;

$week_one_start = !$is_monday strtotime("last monday", $first_day) : $first_day;

return $year'第'$week'周开始天:'date('Y-m-d',$week_one_start+(3600247($week-1)))

';结束天为:'date('Y-m-d',$week_one_start+((360024)(7($week-1)+6)));

}

Private Sub Form_Click()

a = Date

i = Weekday(Date)

b = DateAdd("d", 2 - i, a)

c = DateAdd("d", 8 - i, a)

Print "本周一" & b

Print "本周日" & c

End Sub

季度这个更好办了,只有四个季度,分别是1月1至3月31

4月1至6月30

7月1至9月30

10月1至12月31

你获取月分后,select case就可以了

以上就是关于java获取某个时间的上周一和周日全部的内容,包括:java获取某个时间的上周一和周日、怎么样用asp获取上周周日的日期、java 获取前一周星期天的日期,例如今天2013-8-1,上一周星期日就是2013-7-28等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/10147833.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存