当前时间
from datetime import datetime
t_now = datetime.now()
时间取整
应该把时间转换为时间戳
时间对象
Python2 是 datetime 对象
import time
from datetime import datetime, timedelta
ts_now = time.mktime(t_now.timetuple())
t_block = datetime.fromtimestamp(ts_now - ts_now % 300)
时间戳格式化
Python2
d_now_ft = t_now.strftime('%Y%m%d')
t_block_ft = t_block.strftime('%Y-%m-%d %H:%M:%S')
时间字符串解析
Java
public static Date strToDate(String dateTimeStr,String formatStr){
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
return dateTime.toDate();
}
public static String dateToStr(Date date,String formatStr){
if(date == null){
return StringUtils.EMPTY;
}
DateTime dateTime = new DateTime(date);
return dateTime.toString(formatStr);
}
static public DateTimeFormatter dtFormater =
DateTimeFormat.forPattern("yyyyMMdd").withZone(DateTimeZone.forOffsetHours(8));