取得本星期的开始和结束日期

来源:互联网 发布:知乎每周精选 rss 编辑:程序博客网 时间:2024/05/06 10:31
<script language="javascript">
function padZero(num)
 {
  return (num < 10)? '0' + num : num ;
 }
function chg_date(range,num1,num2){
    //alert(num1+'-'+num2);
    if(range=='t' || range=='w' || range=='lw' || range=='r'){
    PageForm.from_date.value ='<%=getDate.getStringDateShort()%>';
    PageForm.to_date.value =PageForm.from_date.value;}

    if(range!='t'){
        if(PageForm.from_date.value!=PageForm.to_date){
            PageForm.from_date.value ='<%=getDate.getStringDateShort()%>';
            PageForm.to_date.value =PageForm.from_date.value;
        }
        var aStartDate = PageForm.from_date.value.split('-');
        var newStartDate = new Date(parseInt(aStartDate[0], 10),parseInt(aStartDate[1], 10) - 1,parseInt(aStartDate[2], 10) + num1);
        PageForm.from_date.value = newStartDate.getFullYear()+ '-' + padZero(newStartDate.getMonth() + 1)+ '-' + padZero(newStartDate.getDate());
        var aEndDate = PageForm.to_date.value.split('-');
        var newEndDate = new Date(parseInt(aEndDate[0], 10),parseInt(aEndDate[1], 10) - 1,parseInt(aEndDate[2], 10) + num2);
        PageForm.to_date.value = newEndDate.getFullYear()+ '-' + padZero(newEndDate.getMonth() + 1)+ '-' + padZero(newEndDate.getDate());
    }
}
</script>
调用:<input type='button' w',-<%=getDate.getWeek(getDate.getStringDateShort())-1 %>,7-<%=getDate.getWeek(getDate.getStringDateShort())-1 %>);" value='本星期'>
当然,你要加入两个表单来接收这两个日期:
<input type="text >
<input type="text >
附对日期的操作类:
package JavaBean.util;

/**
 * Title:       处理日前的类
 * Description:
 * Copyright:    Copyright (c) 2006
 * Company:      gbpc
 * @author:       prettywolf
 * @version 1.01
 */
import java.util.*;
import java.text.*;

public class GetDate {

    public String datetime;
    public int year;
    public int month;
    public int day;
    public int hour;
    public int minute;
    public int second;
   
    public GetDate(){}
   
    public void setDatetime(String newvalue){
        this.datetime = newvalue;
    }
    public String getStrYear(){
        return this.datetime.substring(0,4);
    }
    public String getStrMonth(){
        return this.datetime.substring(5,7);
    }
    public String getStrDay(){
        return this.datetime.substring(8,10);
    }
    public String getStrHour(){
        return this.datetime.substring(11,13);
    }
    public String getStrMinute(){
        return this.datetime.substring(14,16);
    }
    public String getStrSecond(){
        return this.datetime.substring(17,19);
    }
    public static Date getNowDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString=formatter.format(currentTime);
        ParsePosition pos = new ParsePosition(8);
        Date currentTime_2 = formatter.parse(dateString, pos);
        return currentTime_2;
    }
    public static Date getNowDateShort() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString=formatter.format(currentTime);
        ParsePosition pos = new ParsePosition(8);
        Date currentTime_2 = formatter.parse(dateString, pos);
        return currentTime_2;
    }
    public static String getStringDate(){
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }
    public static String getStringDateShort(){
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }
    public static Date strToDate(String strDate){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos=new ParsePosition(0);
        Date strtodate=formatter.parse(strDate,pos);
        return strtodate;
    }
    public static String dateToStr(java.util.Date dateDate){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(dateDate);
        return dateString;   
    }
    public static Date strToBirthday(String strDate){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos=new ParsePosition(0);
        Date strtodate=formatter.parse(strDate,pos);
        return strtodate;
    }
    public static Date getNow(){
        Date currentTime = new Date();
        return currentTime;
    }
    public static long getS(String strDate){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos=new ParsePosition(0);
        Date strtodate=formatter.parse(strDate,pos);
        return strtodate.getTime();
    }

    public static String getLastDate(long day) {
        Date date=new Date();
        long date_3_hm=date.getTime()-3600000*24*day;
        Date date_3_hm_date=new Date(date_3_hm);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date_3_hm_date);
        return dateString;
    }

    public static int getNowDay(String StrDate) {
        Date Time1=strToDate(StrDate);
        Date Time2=new Date();
        long day=Time1.getTime()-Time2.getTime();
        return (int)day/(24*60*60*1000);
    }

    public static int getNowDay(Date StrDate) {
        Date Time1=StrDate;
        Date Time2=new Date();
        long day=Time1.getTime()-Time2.getTime();
        return (int)day/(24*60*60*1000);
    }
   
    public int getWeek(String date){
        Calendar cal = Calendar.getInstance();
        int year = Integer.parseInt(date.substring(0,4));
        int month = Integer.parseInt(date.substring(5,7));
        int day = Integer.parseInt(date.substring(8,10));
        cal.set(year,month-1,day);
        //String[] dayarray={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
        return cal.get(Calendar.DAY_OF_WEEK)-1;
    }
   

}
 
原创粉丝点击