书写优雅的shell脚本(八)- 日期格式化

1. 将日期格式转为时间戳

获取当前时间:currenttime=`date "+%Y-%m-%d %H:%M:%S"` 结果:2015-04-13 11:15:43
将当前时间转为时间戳:timestamp=`date -d "$curtime" +%s` 结果:1428894966

 

2. 将时间戳转为日期格式

将当前时间戳转为日期格式:
date -d "1970-01-01 UTC $timestamp seconds" "+%F" 结果:2015-04-13
date -d "1970-01-01 UTC $timestamp seconds" "+%F %T" 结果:2015-04-13 11:16:06
将java中的时间戳(精确到ms)转为日期格式:
时间戳:1428887171141 
date -d "1970-01-01 UTC 1428887171.141 seconds" "+%F" 结果:2015-04-13 09:06:11
date -d "1970-01-01 UTC 1428887171 seconds" "+%F" 结果:2015-04-13 09:06:11

 

posted @ 2015-04-13 11:38  JerryShao  阅读(310)  评论(0编辑  收藏  举报