Java中时间的计算 年月日小时分钟秒毫秒微秒


//用到的类 DateUtils
//这个类存在于 org.apache.commons.lang.time.DateUtils;
//也就是这个包 commons-lang-2.3.jar
//API文档在这里
//http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/time/DateUtils.html
//当年上学的时候第一次发现的时候就彻底震惊了



//一个例子毫秒加减
String sTime = "10:25:47:874";
String format = "hh:mm:ss:SSS";
//"yyyy-MM-dd";
//"yyyy-MM-dd hh:mm:ss";
SimpleDateFormat sDateFormat = new SimpleDateFormat(format);
Date cd = new Date();

try {
cd = sDateFormat.parse(sTime);
} catch (ParseException e){
//TODO Auto-generated catch block
e.printStackTrace();
}

//关键的
Date newDate = DateUtils.addMilliseconds(cd,100));
//就是对cd加上100毫秒,减100秒就是加上-100毫秒

//将生成的时间输出为字符串
String newDate_str = sDateFormat.format(newDate);

你可能感兴趣的:(Java)