Java中的时间格式转换:
说在前面:
1.UTC:协调世界时,又称世界标准时间或世界协调时间,简称UTC(从英文“Coordinated Universal Time”/法文“TempsUniversel Coordonné”而来),是最主要的世界时间标准
2.CST:北京时间,China Standard Time,中国标准时间。在时区划分上,属东八区,比协调世界时早8小时,记为UTC+8。不过这个CST这个缩写比较纠结的是它可以同时代表四个不同的时间:
Central Standard Time (USA) UT-6:00
Central Standard Time (Australia) UT+9:30
China Standard Time UT+8:00
Cuba Standard Time UT-4:00
Java Date使用UTC时间,如 Tue Jan 05 14:28:41 CST 2016 表示China Standard Time UT+8:00
日期字符串中的参数含义:
字母 | 日期或时间元素 | 类型 | 示例 |
G | Era 标志符 | Text | AD |
y | 年份 | Number | 1996; 96 |
M | 年份中的月份 | Text | July; Jul; 07 |
w | 年份中的周数 | Number | 27 |
W | 月份中的周数 | Number | 2 |
D | 年份中的天数 | Number | 189 |
d | 月份中的天数 | Number | 10 |
F | 月份中的星期 | Number | 2 |
E | 星期中的天数 | Text | Tuesday; Tue |
a | Am/pm 标记 | Text | PM |
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 |
上面内容参考原地址:
声明:
parse方法是将时间字符串转换成时间对象;format方法是将时间对象转换成时间字符串;
时间字符串一般有以下几种:
一般样式:
"yyyy-MM-dd HH:mm:ss"、"yyyy年MM月dd日 HH时mm分ss秒"
带有时区样式:
"yyyy-MM-dd'T'HH:mm:ss'Z'"
系统默认样式:
"EEE MMM dd HH:mm:ss z yyyy"
其中默认样式转换时,需要多加一个参数new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy",Locale.ENGLISH)
new出来的Date是没有格式的,输出的时候是调用了其toString的方法,格式如下:
Date now=new Date();
System.out.println(now);
Wed May 23 23:05:01 CST 2018
- 将时间对象转换为固定的时间字符串(对象与字符串的转换需要使用SimpleDateFormat)
Date now = new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(now));
2018-05-23 23:09:25
- 将时间字符串转换成时间对象
String dateStr="2018/05/23 23:09:25";
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date=sdf1.parse(dateStr);
System.out.println(date);
Wed May 23 23:09:25 CST 2018
这时候的date则按照默认的格式输出了,所以date对象则是从时间字符串对象转换成了时间对象
- 从某种格式时间字符串(yyyy-MM-dd'T'HH:mm:ss'Z')到指定格式时间字符串(yyyy/MM/dd HH:mm:ss)
String dateStr2="2017-01-22T17:33:18Z";
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
System.out.println(sdf1.format(sdf2.parse(dateStr2)));
2017/01/22 17:33:18
- 毫秒数转换成时间对象
long time=System.currentTimeMillis();
System.out.println("time:"+time);
Date date=new Date(time);
System.out.println("date:"+date);
time:1527089133265
date:Wed May 23 23:25:33 CST 2018
上面的时间按照固定的时间字符串格式输出了,所以转换成功。
- 时间对象获取毫秒数
Date date1=new Date();
System.out.println("time:"+date1.getTime());
time:1527089276078
Java中的时间操作:
获取时间中年、月、日等属性或者已有时间上增加一年、减少一天等亦或通过两个时间计算相关值,用Calendar最好。
- 获取时间的某个属性
Calendar now = Calendar.getInstance();
System.out.println("年: " + now.get(Calendar.YEAR));
System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");
System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));
System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));
System.out.println("分: " + now.get(Calendar.MINUTE));
System.out.println("秒: " + now.get(Calendar.SECOND));
年: 2018
月: 5
日: 24
时: 23
分: 24
秒: 45
- 已有时间上增加或减少时间
Calendar now = Calendar.getInstance();
System.out.println("原时间:"+now.getTime());
now.add(Calendar.YEAR, 1);
System.out.println("上个时间加一年时间:"+now.getTime());
now.add(Calendar.DAY_OF_MONTH, -1);
System.out.println("上个时间减一天时间:"+now.getTime());
原时间:Thu May 24 22:59:43 CST 2018
上个时间加一年时间:Fri May 24 22:59:43 CST 2019
上个时间减一天时间:Thu May 23 22:59:43 CST 2019
- 通过时间计算年龄
//计算年龄方法
public static String getAge(Date birthDay,Date EndDay) throws Exception {
Calendar cal = Calendar.getInstance();
//设置到哪天计算出年龄
cal.setTime(EndDay);
if (cal.before(birthDay)) {
throw new IllegalArgumentException(
"The birthDay is before Now.It's unbelievable!");
}
int yearNow = cal.get(Calendar.YEAR);
int monthNow = cal.get(Calendar.MONTH);
int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
//设置出生日期
cal.setTime(birthDay);
int yearBirth = cal.get(Calendar.YEAR);
int monthBirth = cal.get(Calendar.MONTH);
int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
int age = yearNow - yearBirth;
if (monthNow <= monthBirth) {
if (monthNow == monthBirth) {
if (dayOfMonthNow < dayOfMonthBirth) age--;
}else{
age--;
}
}
return Integer.toString(age);
}
System.out.println("今年的年龄:"+getAge(new SimpleDateFormat("yyyy-MM-dd").parse("1999-09-09"), new Date()));
System.out.println("2022年的年龄:"+getAge(new SimpleDateFormat("yyyy-MM-dd").parse("1999-09-09"),
new SimpleDateFormat("yyyy-MM-dd").parse("2022-01-01")));
今年的年龄:18
2022年的年龄:22
Oracle中的时间格式转换:
- 时间对象转时间字符串
select to_char(sysdate,'yyyy-MM-dd HH24:mm:ss') from dual;
- 时间字符串转时间对象
select to_date('2022-01-01 13:14:20','yyyy-MM-dd HH24:mi:ss') from dual;
Oracle中的时间操作:
- 获取时间的某个属性
select to_char(sysdate,'yyyy') as year,to_char(sysdate,'mm') as month,to_char(sysdate,'dd') as nowDay from dual;
- 时间加或减(参考地址:)
加法
select sysdate,add_months(sysdate,12) from dual; --加1年
select sysdate,add_months(sysdate,1) from dual; --加1月
select sysdate,to_char(sysdate+7,'yyyy-mm-dd HH24:MI:SS') from dual; --加1星期
select sysdate,to_char(sysdate+1,'yyyy-mm-dd HH24:MI:SS') from dual; --加1天
select sysdate,to_char(sysdate+1/24,'yyyy-mm-dd HH24:MI:SS') from dual; --加1小时
select sysdate,to_char(sysdate+1/24/60,'yyyy-mm-dd HH24:MI:SS') from dual; --加1分钟
select sysdate,to_char(sysdate+1/24/60/60,'yyyy-mm-dd HH24:MI:SS') from dual; --加1秒
减法
select sysdate,add_months(sysdate,-12) from dual; --减1年
select sysdate,add_months(sysdate,-1) from dual; --减1月
select sysdate,to_char(sysdate-7,'yyyy-mm-dd HH24:MI:SS') from dual; --减1星期
select sysdate,to_char(sysdate-1,'yyyy-mm-dd HH24:MI:SS') from dual; --减1天
select sysdate,to_char(sysdate-1/24,'yyyy-mm-dd HH24:MI:SS') from dual; --减1小时
select sysdate,to_char(sysdate-1/24/60,'yyyy-mm-dd HH24:MI:SS') from dual; --减1分钟
select sysdate,to_char(sysdate-1/24/60/60,'yyyy-mm-dd HH24:MI:SS') from dual; --减1秒
JSP中的时间转换:
使用JSTL:
使用JavaScript:
- 时间对象转时间字符串
需要自己定义一个方法,然后调用:
// 时间格式方法
Date.prototype.format = function(fmt) {
var o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //小时
"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;
}
var timeFromStr=new Date().format("yyyy-MM-ddTHH:mm:ssZ");
用的时候直接调用上面定义的方法,Solr的时间格式如上,是有时区的。在java中,格式字符串为"yyyy-MM-dd'T'HH:mm:ss'Z'",而在js中则需要去掉单引号,不然会单引号会依然存在在js解析后的内容中。
- 时间字符串转时间对象
var date1=new Date("2018-01-01");
var date2=new Date("2018/01/01");
console.log(date1);
console.log(date2);
Mon Jan 01 2018 08:00:00 GMT+0800 (中国标准时间)
Mon Jan 01 2018 00:00:00 GMT+0800 (中国标准时间)
这里需要特别说明的是,上面两种的解析结果会不同,第一个中间用“-”的解析出的结果会多出8个小时来。这可能是与默认时间配置有关,我的处理办法是在时间字符串放入参数之前将“-”使用.replace("-","/")进行字符串处理,然后在解析;
JavaScript中的时间操作:
var date=new Date(date.setMonth(date.getMonth()+1));
var timeToStr=date.format("yyyy-MM-ddTHH:mm:ssZ");
上面代码为当前时间加一月