Java SimpleDataFormat使用带有jolly字符的模式
有没有一种方法可以用包含一些“特殊字符”或“jolly字符”作为分隔符的模式来解析日期,并使用标准SimpleDataFormat?我想使用模式Java SimpleDataFormat使用带有jolly字符的模式,java,date,simpledateformat,Java,Date,Simpledateformat,有没有一种方法可以用包含一些“特殊字符”或“jolly字符”作为分隔符的模式来解析日期,并使用标准SimpleDataFormat?我想使用模式“yyyy-MM-dd”,“yyy/MM/dd”,“yyy-MM-dd”等解析我的日期,所以我在搜索类似“yyy*MM*dd”的内容,其中*是一个特殊字符,表示“随机字符” SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); Date d = fmt.parse(stringdate);
“yyyy-MM-dd”
,“yyy/MM/dd”
,“yyy-MM-dd”
等解析我的日期,所以我在搜索类似“yyy*MM*dd”的内容,其中*是一个特殊字符,表示“随机字符”
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
Date d = fmt.parse(stringdate);
你应该试试这个
String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
System.out.println(date);
你应该试试这个
String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
System.out.println(date);
使用正则表达式过滤掉有趣的字符,然后解析过滤后的字符串
String stringdate = "2017x01-18";
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date d = fmt.parse(Pattern.compile("(....).(..).(..)").matcher(stringdate).replaceAll("$1-$2-$3"));
使用正则表达式过滤掉有趣的字符,然后解析过滤后的字符串
String stringdate = "2017x01-18";
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date d = fmt.parse(Pattern.compile("(....).(..).(..)").matcher(stringdate).replaceAll("$1-$2-$3"));
您可以尝试将输入“标准化”为所需格式:
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
fmt.parse(stringdate.replaceAll(" ", "-").replaceAll("/","-"));
您可以尝试将输入“标准化”为所需格式:
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
fmt.parse(stringdate.replaceAll(" ", "-").replaceAll("/","-"));
可以帮助您,因为它解决了通过一组可能的输入格式解析日期字符串的问题。My time library Time4J可以以一种非常高效的方式用于此目的,参见此,在Java-6平台上也可以使用稍微不同的语法。可以帮助您,因为它解决了通过一组可能的输入格式解析日期字符串的问题。My time library Time4J可以以一种非常高效的方式用于此目的,参见此,在Java-6平台上也可以使用稍微不同的语法。