javajava
java中Calendar.getInstance()和new Date()的差異以下:
Calendar.getInstance()是獲取一個Calendar對象並能夠進行時間的計算,時區的指定
new Date()是建立了一個date對象,默認是utc格式的。
兩者能夠相互轉化:
Calendar calendar = Calendar.getInstance();
// 從一個 Calendar 對象中獲取 Date 對象
Date date = calendar.getTime();
// 將 Date 對象反應到一個 Calendar 對象中,
// Calendar/GregorianCalendar 沒有構造函數能夠接受 Date 對象
// 因此咱們必需先得到一個實例,而後設置 Date 對象
Date date = new Date();
calendar.setTime(date);node
JavaScriptapp
(function(w) {
w.Calendar = function(opt) {
// 建立日曆控件基本結構
var cldbox = document.createElement("div");
cldbox.className = 'calendar-container';
var tpl = "";
tpl += '<div class="calendar-title">';
tpl += '<div class="calendar-prevyear"><<</div>';
tpl += '<div class="calendar-prevmonth"><</div>';
tpl += '<div class="calendar-year"></div>';
tpl += '<div class="calendar-month"></div>';
tpl += '<div class="calendar-nextmonth">></div>';
tpl += '<div class="calendar-nextyear">>></div>';
tpl += '</div>';
tpl += '<div class="calendar-week"><div>日</div><div>一</div><div>二</div><div>三</div><div>四</div><div>五</div><div>六</div></div>';
tpl += '<div class="calendar-content clearfix"></div>';
cldbox.innerHTML = tpl;
document.querySelector(opt.el).appendChild(cldbox);dom
// dom 對象
var omonth = cldbox.querySelector(".calendar-month");
var oyear = cldbox.querySelector(".calendar-year");
var prevyear = cldbox.querySelector(".calendar-prevyear");
var prevmonth = cldbox.querySelector(".calendar-prevmonth");
var nextyear = cldbox.querySelector(".calendar-nextyear");
var nextmonth = cldbox.querySelector(".calendar-nextmonth");
var content = cldbox.querySelector(".calendar-content");函數
// 時間對象(默認當前)
var dateObj;
if(opt.value) {
dateObj = opt.value;
} else {
dateObj = new Date();
}
// 年月獲取
var year = getYear(dateObj);
var month = getMonth(dateObj);
// 月年的顯示
omonth.innerHTML = month + "月";
oyear.innerHTML = year + "年";
// 獲取本月1號的周值
var fistWeek = getCurmonWeeknum(dateObj);
// 本月總日數
var monDaynum = getCurmonDaynum(dateObj);
// 當前日期
var nowDay = getDay(dateObj);
//初始化顯示本月信息
setContent(content, fistWeek, monDaynum, nowDay);ui
var isSupportMUI = (typeof mui === 'function');
var evt = {
type: isSupportMUI ? 'tap' : 'click'
}
// 顯示當前時間
content.addEventListener(evt.type, function(event) {
if(event.target.tagName == "DIV" && event.target.nodeType == "1" && hasclass(event.target.className, "dayspan")) {
var day = event.target.innerHTML;
var dateObj2 = new Date(dateObj.getFullYear(), dateObj.getMonth(), day);
var week = getWeek(dateObj2);
opt.callback({
'year': getYear(dateObj),
'month': getMonth(dateObj),
'day': day,
'week': week
});
};
})spa
// 上一月
prevmonth.addEventListener(evt.type, function() {
var ddm = null;
var ddy = null;
if((dateObj.getMonth() - 1) == -1) {
ddm = 11;
ddy = dateObj.getFullYear() - 1;
} else {
ddm = dateObj.getMonth() - 1;
ddy = dateObj.getFullYear();
};
dateObj.setFullYear(ddy);
dateObj.setMonth(ddm);
omonth.innerHTML = getMonth(dateObj) + "月";
oyear.innerHTML = dateObj.getFullYear() + "年";
clearContent(content);
fistWeek = getCurmonWeeknum(dateObj);
monDaynum = getCurmonDaynum(dateObj);
nowDay = getDay(dateObj);
setContent(content, fistWeek, monDaynum, nowDay);
})對象
// 下一月
nextmonth.addEventListener(evt.type, function() {
var ddm = null;
var ddy = null;
if((dateObj.getMonth() + 1) == 12) {
ddm = 0;
ddy = dateObj.getFullYear() + 1;
} else {
ddm = dateObj.getMonth() + 1;
ddy = dateObj.getFullYear();
};
dateObj.setFullYear(ddy);
dateObj.setMonth(ddm);
omonth.innerHTML = getMonth(dateObj) + "月";
oyear.innerHTML = dateObj.getFullYear() + "年";
clearContent(content);
fistWeek = getCurmonWeeknum(dateObj);
monDaynum = getCurmonDaynum(dateObj);
// nowDay = getDay(dateObj);
console.log(nowDay);
setContent(content, fistWeek, monDaynum, 1);
})ip
// 上一年
prevyear.addEventListener(evt.type, function() {
var ddy = dateObj.getFullYear() - 1;
dateObj.setFullYear(ddy);
oyear.innerHTML = dateObj.getFullYear() + "年";
clearContent(content);
fistWeek = getCurmonWeeknum(dateObj);
monDaynum = getCurmonDaynum(dateObj);
nowDay = getDay(dateObj);
setContent(content, fistWeek, monDaynum, nowDay);
})get
// 下一年
nextyear.addEventListener(evt.type, function() {
var ddy = dateObj.getFullYear() + 1;
dateObj.setFullYear(ddy);
oyear.innerHTML = dateObj.getFullYear() + "年";
clearContent(content);
fistWeek = getCurmonWeeknum(dateObj);
monDaynum = getCurmonDaynum(dateObj);
nowDay = getDay(dateObj);
setContent(content, fistWeek, monDaynum, nowDay);
})
}
//有無指定類名的判斷
function hasclass(str, cla) {
var i = str.search(cla);
if(i == -1) {
return false;
} else {
return true;
};
}
// 初始化日期顯示方法
function setContent(el, fistWeek, monDaynum, nowDay) {
// 留空
for(var i = 1; i <= fistWeek; i++) {
var subContent = document.createElement("div");
subContent.innerHTML = "";
el.appendChild(subContent);
}
// 正常區域
for(var i = 1; i <= monDaynum; i++) {
var subContent = document.createElement("div");
subContent.className = "canChoose";
var dayspan = document.createElement("div");
dayspan.className = "dayspan";
var point = document.createElement("div");
point.className = "pointspan";
if(i == nowDay) {
subContent.classList.add("today");
}
// if(i==8){
// point.classList.add("active");
// point.classList.add("green");
// }
// if(i==11){
// point.classList.add("active");
// point.classList.add("blue");
// }
// if(i==21){
// point.classList.add("active");
// point.classList.add("red");
// }
// if(i==30){
// point.classList.add("active");
// point.classList.add("orange");
// }
dayspan.innerHTML = i;
subContent.appendChild(dayspan);
subContent.appendChild(point);
el.appendChild(subContent);
}
}
// 清除內容
function clearContent(el) {
el.innerHTML = "";
}
// 判斷閏年
function isLeapYear(year) {
if((year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)) {
return true;
} else {
return false;
}
}
// 獲得當前年份
function getYear(dateObj) {
return dateObj.getFullYear()
}
// 獲得當前月份
function getMonth(dateObj) {
var month = dateObj.getMonth()
switch(month) {
case 0:
return "1";
break;
case 1:
return "2";
break;
case 2:
return "3";
break;
case 3:
return "4";
break;
case 4:
return "5";
break;
case 5:
return "6";
break;
case 6:
return "7";
break;
case 7:
return "8";
break;
case 8:
return "9";
break;
case 9:
return "10";
break;
case 10:
return "11";
break;
case 11:
return "12";
break;
default:
}
}
// 獲得當前號數
function getDay(dateObj) {
return dateObj.getDate();
}
// 獲得週期數
function getWeek(dateObj) {
var week;
switch(dateObj.getDay()) {
case 1:
week = "星期一";
break;
case 2:
week = "星期二";
break;
case 3:
week = "星期三";
break;
case 4:
week = "星期四";
break;
case 5:
week = "星期五";
break;
case 6:
week = "星期六";
break;
default:
week = "星期天";
}
return week;
}
// 獲取本月總日數方法
function getCurmonDaynum(dateObj) {
var year = dateObj.getFullYear();
var month = dateObj.getMonth();
if(isLeapYear(year)) { //閏年
switch(month) {
case 0:
return "31";
break;
case 1:
return "29";
break; //2月
case 2:
return "31";
break;
case 3:
return "30";
break;
case 4:
return "31";
break;
case 5:
return "30";
break;
case 6:
return "31";
break;
case 7:
return "31";
break;
case 8:
return "30";
break;
case 9:
return "31";
break;
case 10:
return "30";
break;
case 11:
return "31";
break;
default:
}
} else { //平年
switch(month) {
case 0:
return "31";
break;
case 1:
return "28";
break; //2月
case 2:
return "31";
break;
case 3:
return "30";
break;
case 4:
return "31";
break;
case 5:
return "30";
break;
case 6:
return "31";
break;
case 7:
return "31";
break;
case 8:
return "30";
break;
case 9:
return "31";
break;
case 10:
return "30";
break;
case 11:
return "31";
break;
default:
}
}
}
// 獲取本月1號的周值 function getCurmonWeeknum(dateObj) { var oneyear = new Date(); var year = dateObj.getFullYear(); var month = dateObj.getMonth(); //0是12月 oneyear.setFullYear(year); oneyear.setMonth(month); //0是12月 oneyear.setDate(1); return oneyear.getDay(); }})(window);