JAVA36. 日期换算
描述
已知,纽约时间比北京时间慢12小时,请根据用户输入的北京时间输出相应的纽约时间,若用户输入错误的月份或日期等信息则将其顺加。例如用户输入2021 13 32 14 43 54则生成北京时间为:2022-02-01 14:43:54 纽约时间为:2022-02-01 02:43:54输入描述
一组字符串,年,月,日,时,分,秒用空格隔开,如果用户数据输入不正常,应输出“您输入的数据不合理”输出描述
示例1
输入:
2021 09 08 14 38 05
输出:
北京时间为:2021-09-08 14:38:05 纽约时间为:2021-09-08 02:38:05
示例2
输入:
2021 09 08 14 38
输出:
您输入的数据不合理
Java 解法, 执行用时: 38ms, 内存消耗: 11680KB, 提交时间: 2021-11-13
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(); String [] arr = str1.split(" "); if(arr.length != 6){ System.out.println("您输入的数据不合理"); }else{ String str = arr[0]+"-"+arr[1]+"-"+arr[2]+" "+arr[3]+":"+arr[4]+":"+arr[5]; Date date = sdf.parse(str); System.out.println("北京时间为:" + sdf.format(date.getTime())); System.out.println("纽约时间为:" + sdf.format(date.getTime() - (long)12* 60 * 60 * 1000)); } //write your code here...... } }
Java 解法, 执行用时: 39ms, 内存消耗: 11552KB, 提交时间: 2021-11-23
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[] dates = str1.split(" "); if(dates.length != 6){ System.out.println("您输入的数据不合理"); }else{ String temp = dates[0] + "-" + dates[1] + "-" + dates[2] + " " + dates[3] + ":" + dates[4] + ":" + dates[5]; Date date = sdf.parse(temp); System.out.println("北京时间为:" + sdf.format(date.getTime())); System.out.println("纽约时间为:" + sdf.format(date.getTime() - 12 * 60 * 60 * 1000L)); } } }
Java 解法, 执行用时: 39ms, 内存消耗: 11640KB, 提交时间: 2021-11-25
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[] arr = str1.split(" "); if(arr.length != 6){ System.out.println("您输入的数据不合理"); }else{ String str = arr[0]+"-"+arr[1]+"-"+arr[2]+" "+arr[3]+":"+arr[4]+":"+arr[5]; Date date = sdf.parse(str); System.out.println("北京时间为:" + sdf.format(date.getTime())); System.out.println("纽约时间为:" + sdf.format(date.getTime() - (long)12* 60 * 60 * 1000)); } } }
Java 解法, 执行用时: 39ms, 内存消耗: 11724KB, 提交时间: 2022-02-08
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[] arr = str1.split(" "); if(arr.length != 6){ System.out.println("您输入的数据不合理"); }else{ String str = arr[0]+"-"+arr[1]+"-"+arr[2]+" "+arr[3]+":"+arr[4]+":"+arr[5]; Date date = sdf.parse(str); System.out.println("北京时间为:" + sdf.format(date.getTime())); System.out.println("纽约时间为:" + sdf.format(date.getTime() - (long)12* 60 * 60 * 1000)); } } }
Java 解法, 执行用时: 39ms, 内存消耗: 11736KB, 提交时间: 2022-02-08
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[] arr = str1.split(" "); if(arr.length != 6){ System.out.println("您输入的数据不合理"); }else{ String str = arr[0]+"-"+arr[1]+"-"+arr[2]+" "+arr[3]+":"+arr[4]+":"+arr[5]; Date date = sdf.parse(str); System.out.println("北京时间为:"+sdf.format(date.getTime())); System.out.println("纽约时间为:"+sdf.format(date.getTime()-(long)12*60*60*1000)); } // String[] arr = str1.split(" "); // if(arr.length != 6){ // System.out.println("您输入的数据不合理"); // }else{ // String str = arr[0]+"-"+arr[1]+"-"+arr[2]+" "+arr[3]+":"+arr[4]+":"+arr[5]; // Date date = sdf.parse(str); // System.out.println("北京时间为:" + sdf.format(date.getTime())); // System.out.println("纽约时间为:" + sdf.format(date.getTime() - (long)12* 60 * 60 * 1000)); } }