时间日期格式转换
Time Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description
对于日期的常用格式,在中国常采用格式的是“年年年年/月月/日日”或写为英语缩略表示的”yyyy/mm/dd”,此次编程竞赛的启动日期“2010/11/20”就是符合这种格式的一个日期,
而北美所用的日期格式则为“月月/日日/年年年年”或”mm/dd /yyyy”,如将“2010/11/20”改成这种格式,对应的则是”11/20/2010”。对于时间的格式,则常有12小时制和24小时制
的表示方法,24小时制用0-24来表示一天中的24小时,而12小时制只采用1-12表示小时,再加上am/pm来表示上午或下午,比如”17:30:00”是采用24小时制来表示时间,而对应的
12小时制的表示方法是”05:30:00pm”。注意12:00:00pm表示中午12点,而12:00:00am 表示凌晨12点。
对于给定的采用”yyyy/mm/dd”加24小时制(用短横线”-”连接)来表示日期和时间的字符串,请编程实现将其转换成”mm/dd/yyyy”加12小时制格式的字符串。
Input
第一行为一个整数T(T<=20),代表总共需要转换的时间日期字符串的数目。
接下来的总共T行,每行都是一个需要转换的时间日期字符串。
Output
分行输出转换之后的结果
Sample Input
2
2010/11/20-12:12:12
1970/01/01-00:01:01
Sample Output
11/20/2010-12:12:12pm
01/01/1970-12:01:01am
Hint
Source
import java.util.Scanner;
class time {
String str;
time(String s) {
str = s;
}
// String a[] = new String[10];
int a[] = new int[10];
void fenjie() {
int i = 0;
for (String x : str.split("/|-|\\:")) {
a[i++] = Integer.parseInt(x);
}
}
void show() {
fenjie();
boolean f = true;
if (a[3] >= 12) {
if (a[3] != 12)
a[3] = a[3] - 12;
f = false;
}
if(a[3] == 0){
a[3] = 12;
}
String t;
if (f)
t = "am";
else
t = "pm";
System.out.printf("%02d/%02d/%d-%02d:%02d:%02d%s\n", a[1], a[2], a[0], a[3], a[4], a[5], t);
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner ss = new Scanner(System.in);
int n;
n = ss.nextInt();
while (n > 0) {
String t;
t = ss.next();
time per = new time(t);
per.show();
n--;
}
ss.close();
}
}