1、 SimpleDateFormat
SimpleDateFormat CeshiFmt0=new SimpleDateFormat("Gyyyy年MM月dd日 HH时mm分ss秒"); //公元2010年07月27日 09时19分29秒
SimpleDateFormat CeshiFmt1=new SimpleDateFormat("yyyy/MM/dd HH:mm");//2010/07/27 09:19
SimpleDateFormat CeshiFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //2010-07-27 09:19:29
SimpleDateFormat CeshiFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E "); //2010年07月27日 09时19分29秒 星期二
SimpleDateFormat CeshiFmt4=new SimpleDateFormat("yyyy/MM/dd E");//2010/07/27 星期二
SimpleDateFormat CeshiFmt5=new SimpleDateFormat("一年中的第 D 天 ,第w个星期 ,一个月中第W个星期 ,k时 z时区");
//一年中的第 208 天 ,第31个期 ,一个月中第5个星期 ,9时 CST时区
SimpleDateFormat日期-时间格式模式参数:
字母 日期或时间元素 表示 示例
G Era 标志符 Text AD
y 年 Year
1996
;
96
M 年中的月份 Month July; Jul;
07
w 年中的周数 Number
27
W 月份中的周数 Number
2
D 年中的天数 Number
189
d 月份中的天数 Number
10
F 月份中的星期 Number
2
H 一天中的小时数(
0
-
23
) Number
0
k 一天中的小时数(
1
-
24
) Number
24
K am/pm 中的小时数(
0
-
11
) Number
0
h am/pm 中的小时数(
1
-
12
) Number
12
m 小时中的分钟数 Number
30
s 分钟中的秒数 Number
55
S 毫秒数 Number
978
z 时区 General time zone Pacific Standard Time; PST; GMT-
08
:
00
Z 时区 RFC
822
time zone -
0800
2、时间类型转换
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
(1).Calendar 转化 String //获取当前时间的具体情况,如年,月,日,week,date,分,秒等
Calendar calendat = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = sdf.format(calendar.getTime());
(2).String 转化Calendar
String str="2010-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date =sdf.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
(3).Date 转化String
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
String dateStr=sdf.format(new Date());
(4).String 转化Date
String str="2010-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date birthday = sdf.parse(str);
(5).Date 转化Calendar
Calendar calendar = Calendar.getInstance();
calendar.setTime(new java.util.Date());
(6).Calendar转化Date
Calendar calendar = Calendar.getInstance();
java.util.Date date =calendar.getTime();
(7).Date 转成 String
System.out.println(sdf.format(new Date()));
(8).String 转成 Timestamp
Timestamp ts = Timestamp.valueOf("2011-1-14 08:11:00");
(9).Timestamp 转成 String
Timestamp和Date多数用法是一样的。
(10).Date 转 TimeStamp
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = df.format(new Date());
Timestamp ts = Timestamp.valueOf(time);
3、获取当前时间时分秒
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
int year=cal.get(Calendar.YEAR);
int month=cal.get(Calendar.MONTH)+1;//Canlendar中的月是以0开始的所以要加1
int date=cal.get(Calendar.DATE);
int hour=cal.get(Calendar.HOUR);
int minute=cal.get(Calendar.MINUTE);
int second=cal.get(Calendar.SECOND);
int milliSecond=cal.get(Calendar.MILLISECOND);
4、时间的加减法
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.set(Calendar.DATE,cal.get(Calendar.DATE)-2);//当前时间前两天
cal.set(Calendar.DATE,cal.get(Calendar.DATE)+2);//当前时间后两天
Date date = new Date();
date.setTime(date.getTime() - 1*24 * 60 * 60 * 1000);//当前时间前1天
date.setTime(date.getTime() + 1*24 * 60 * 60 * 1000);//当前时间后1天
Date date = new Date();
int hour = nowDate.getHours();
int min = nowDate.getMinutes();
int second = nowDate.getSeconds();
5、JS中的天的运算(1)获取当前时间
var now= new Date();
var year=now.getYear();
var month=now.getMonth()+1;
var day=now.getDate();
var hour=now.getHours();
var minute=now.getMinutes();
var second=now.getSeconds();
alert(year+"-"+month+"-"+day+" "+hour+":"+":"+minute+":"+second);
(2)有关工作日的运算
/*
*功能:计算N个工作日以后的时间(工作日除去周六周天)
*参数:
*1、当前时间
*2、N个工作日
*return:N个工作日后的时间
*
*/
function f1(date, days){
date=new Date(date);
days=new Date(days);
var dateVal = date.getTime();
var oneDayVal = 24*60*60*1000;//一天
var newDateVal = dateVal;
newDateVal += oneDayVal
for(var i = 0 ; i < days; i++){
var newDay = new Date(newDateVal).getDay();//获取星期几,周天为0
if(newDay == 0 || newDay == 6){
days++;//如果为周六周天,则总天数+1
}
newDateVal += oneDayVal;
}
newDateVal -= oneDayVal
return new Date(newDateVal).Format("yyyy-MM-dd hh:mm:ss");//格式化日期
}
// 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function(fmt)
{ //author: meizz
var o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt))
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)
if(new RegExp("("+ k +")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
return fmt;
}
6、Calendar Date ,DateFormat, SimpleDateFormat的区别