将未指定格式的日期字符串转化成java.util.Date类型日期对象
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
-
- public class Test {
-
-
-
-
-
-
-
-
-
-
-
-
- public static void main(String[] args) {
- Test test=new Test();
- try {
- System.out.println(test.parseStringToDate("03/2-3 03小时3:3").toLocaleString());
- } catch (ParseException e) {
-
- e.printStackTrace();
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public Date parseStringToDate(String date) throws ParseException{
- Date result=null;
- String parse=date;
- parse=parse.replaceFirst("^[0-9]{4}([^0-9]?)", "yyyy$1");
- parse=parse.replaceFirst("^[0-9]{2}([^0-9]?)", "yy$1");
- parse=parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)", "$1MM$2");
- parse=parse.replaceFirst("([^0-9]?)[0-9]{1,2}( ?)", "$1dd$2");
- parse=parse.replaceFirst("( )[0-9]{1,2}([^0-9]?)", "$1HH$2");
- parse=parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)", "$1mm$2");
- parse=parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)", "$1ss$2");
-
- DateFormat format=new SimpleDateFormat(parse);
-
- result=format.parse(date);
-
- return result;
- }
- }
没有评论:
发表评论