您的位置:首页 > 其它

PAT 7 星系炸弹(日期转换)

2018-03-19 23:44 197 查看

星系炸弹

在X星系的广袤空间中漂浮着许多X星人造“炸弹”,用来作为宇宙中的路标。
每个炸弹都可以设定多少天之后爆炸。比如:阿尔法炸弹2015年1月1日放置,定时为15天,则它在2015年1月16日爆炸。有一个贝塔炸弹,2014年11月9日放置,定时为1000天,请你计算它爆炸的准确日期。请填写该日期,格式为 yyyy-mm-dd 即4位年份2位月份2位日期。比如:2015-02-19请严格按照格式书写。不能出现其它文字或符号。答案:(5分)f1:
Calendar c=Calendar.getInstance();
c.set(2014,10,9);
c.add(Calendar.DATE,1000);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date d=new Date();
d=c.getTime();
System.out.println(sdf.format(d));

f2:
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class T3date {

public static void main(String[] args) throws ParseException {
// 创建一个时间转换格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 将字符串类型的日期转换成日期格式
Date start = sdf.parse("2015-11-09");
// 得到日期的毫秒表示形式
long startLong = start.getTime();
// 将日期毫秒形式用BigInteger类表示,方便运算
BigInteger startBig = new BigInteger(String.valueOf(startLong));
// 进行long类型的数据的运算,如果不加L的话,会造成溢出
long num = 1000L * 24L * 1000L * 3600L;
BigInteger numBig = new BigInteger(num + "");
BigInteger endBig=startBig.add(numBig);
long endLong = endBig.longValue();
String end = sdf.format(endLong);
System.out.println(end);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法基础