这种没法增加啊,楼主你看我下面这个日期是什么?2003-8-112:10:10是2003-8-11 2:10:10呢还是2003-8-1 12:10:10?如果能够排除上面的情况,那么可以用SimpleDateFormat public static void main(String args[])throws Exception { String date ="2003-8-112:10:10"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss"); SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf1.format(sdf.parse(date))); } // End of method: main
import java.util.regex.Matcher; import java.util.regex.Pattern;public class FormatDate { public static void main(String[] args) { String input = "2003-8-82:10:10"; String output = null; Pattern p = Pattern.compile("([1-9]\\d*-(?:0?[1-9]|1[012])-(?:0?[1-9]|[12]\\d|3[01]))\\s*((?:0?\\d|1\\d|2[0-4]):(?:0?\\d|[1-5]\\d):(?:0?\\d|[1-5]\\d))"); Matcher m = p.matcher(input); if (m.matches()) { output = m.group(1) + " " + m.group(2); } System.out.println(output); }}
public static Timestamp getDateTimestamp(String dateTime) throws ParseException { if (isBlank(dateTime)) { return null; } return new Timestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateTime).getTime()); }
public static Timestamp getDateTimestamp(String dateTime) throws ParseException { if (isBlank(dateTime)) { return null; } return new Timestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateTime).getTime()); }
{
String date ="2003-8-112:10:10";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf1.format(sdf.parse(date)));
} // End of method: main
import java.util.regex.Pattern;public class FormatDate { public static void main(String[] args) {
String input = "2003-8-82:10:10";
String output = null;
Pattern p = Pattern.compile("([1-9]\\d*-(?:0?[1-9]|1[012])-(?:0?[1-9]|[12]\\d|3[01]))\\s*((?:0?\\d|1\\d|2[0-4]):(?:0?\\d|[1-5]\\d):(?:0?\\d|[1-5]\\d))");
Matcher m = p.matcher(input);
if (m.matches()) {
output = m.group(1) + " " + m.group(2);
}
System.out.println(output);
}}
if (isBlank(dateTime)) {
return null;
}
return new Timestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateTime).getTime());
}
if (isBlank(dateTime)) {
return null;
}
return new Timestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateTime).getTime());
}