Java SimpleDateFormat的API运用
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();;
try{
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy MM dd HH mm ss");
Date date = sdf1.parse(str1);
System.out.println("北京时间为:"+sdf.format(date));
// 1秒等于1000毫秒,1分钟等于60秒...所以下方12小时等于....
long twelveHours = 1000 * 60 * 60 * 12;
// 纽约时间和北京时间相差12小时,所以相减即可
System.out.println("纽约时间为:"+sdf.format(date.getTime() - twelveHours));
}catch (Exception e){
System.out.println("您输入的数据不合理");
}
}
}