题解 | #日期换算#
日期换算
http://www.nowcoder.com/practice/08f8a66cb5584268a78ecca2749a2da5
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner;
public class Main { public static void main(String[] args) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Scanner in = new Scanner(System.in); String str1 = in.nextLine();
//write your code here......
//用字符串数组暂存获取的数字
String str2[] = str1.split(" ");
//判断用户输入的数字是否合理
if(str2.length!=6){
System.out.println("您输入的数据不合理");
}else{
//改成吧日期yyyy-MM-dd HH:mm:ss形式
String temp=str2[0]+"-"+str2[1]+"-"+str2[2]+" "+str2[3]+":"+str2[4]+":"+str2[5];
//写入Date类方便计算
Date data=sdf.parse(temp);
System.out.println("北京时间为:"+sdf.format(data.getTime()));
//减12个小时,单位为秒
System.out.println("纽约时间为:"+sdf.format(data.getTime()-(long)12*60*60*1000));
}
}
}
查看12道真题和解析