华为机试题 计算下一秒
题目大意是:
输入:2012/03/12 15:51:30
输出:2012/03/12 15:51:31
就是说在给定的时间上增加一秒,并输出同样的格式时间。
要考虑到闰年!
题外话:这题本来是可以用几行代码就可以搞定的(运用Java自带的API),但是。。。华为。。。
先给出运用时间 API 来完成这个任务吧。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author fangzheng
* @date 2016年9月25日 下午4:07:47
*/
public class Test111 {
public static void main(String[] args) {
String input = "2012/03/12 15:51:30";
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = null;
try {
// 根据固定格式将字符串时间转换为Date类型
date = format.parse(input);
} catch (ParseException e) {
e.printStackTrace();
}
long nextSecond = date.getTime() + 1000;// 加1秒
String nextSecondDate = format.format(new Date(nextSecond));
System.out.println(nextSecondDate);
}
}
下面运用人工计算的方式去完成计算下一秒:
待续。。。