时间日期格式转换
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
而北美所用的日期格式则为“月月/日日/年年年年”或”mm/dd /yyyy”,如将“2010/11/20”改成这种格式,对应的则是”11/20/2010”。对于时间的格式,则常有12小时制和24小时制
12小时制的表示方法是”05:30:00pm”。注意12:00:00pm表示中午12点,而12:00:00am 表示凌晨12点。
第一行为一个整数T(T<=20),代表总共需要转换的时间日期字符串的数目。
<span 18px;="" \"="" style="box-sizing: border-box;">接下来的总共T行,每行都是一个需要转换的时间日期字符串。
Output
分行输出转换之后的结果
Example Input
2 2010/11/20-12:12:12 1970/01/01-00:01:01
Example Output
11/20/2010-12:12:12pm 01/01/1970-12:01:01am
/** * 写成 函数的形式,自己写 */ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); String string; for (int i = 0; i < T; i++) { string = sc.next(); System.out.println(DateUtil.dateConvert(string)); } sc.close(); } } class DateUtil { public static String dateConvert(String strDate) { SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss"); // H:一天中的小时数(0-23);h: // am/pm // 中的小时数(1-12) SimpleDateFormat sdf2 = new SimpleDateFormat("MM/dd/yyyy-hh:mm:ssa",// a: // Am/pm // 标记 new Locale("US"));// Locale 对象表示了特定的地理、政治和文化地区。用于表示国家/地区的有用常量。 String str = null; try { str = sdf2.format(sdf1.parse(strDate)).toLowerCase();// 将最终的 // 格式转换为小写的am,pm } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return str; } } /** * 老师给的参考代码 */ /* * import java.text.ParseException; import java.text.SimpleDateFormat; import * java.util.Date; import java.util.Locale; import java.util.Scanner; * * public class Main { public static void main(String[] args) throws * ParseException { Scanner sc = new Scanner(System.in); int count = * sc.nextInt(); for (int i = 0; i < count; i++) { String strDate = sc.next(); * SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss"); // * H一天中的小时数(0-23); // 用给定的模式和默认语言环境的日期格式符号构 Date date = sdf.parse(strDate); * SimpleDateFormat sdf1 = new SimpleDateFormat( "MM/dd/yyyy-hh:mm:ssa", new * Locale("US"));// h am/pm // 中的小时数(1-12) // a Am/pm 标记 * System.out.println(sdf1.format(date).toLowerCase()); } sc.close(); } } */