今天在用hadoop处理数据时,发现时间为十二进制,不利于我们的操作,于是自行搜索,发现是使用SimpleDateFormat("yyyy-MM-dd HH:mm:ss")将其中的HH改为hh,而我的数据是12:39:36 AM这种带AM,PM格式的,处理起来不方便,于是自己写了个时间字符串格式化。
//字符串时间格式化方法
public static String formattime(String time) {
String last=time.substring(time.length()-2);
if (last.equals("PM")) {
int Temp=Integer.valueOf(time.substring(0, 2))+12;
time=Temp+time.substring(2);
}
time=time.replace(last, "");
return time;
}
//方法测试
public static void main(String[] args) {
String time="12:39:36 AM";
String time1="06:39:36 PM";
System.out.println(formattime(time));
System.out.println(formattime(time1));
}
最后测试结果: