Java SimpleDateFormat 使用具有欢乐性格的模式

Java SimpleDateFormat use pattern with jolly character

有没有一种方法可以使用包含一些 "special char" 或 "jolly char" 作为标准 SimpleDateFormat 分隔符的模式来解析日期?我想使用 "yyyy MM dd""yyyy/MM/dd""yyyy-MM-dd" 等模式来解析我的日期...,所以我正在搜索类似 "yyyy*MM*dd" 的内容,其中 * 是一个特殊的字符含义 'a random character'

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 stringdate = "2017x01-18"; 
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date d = fmt.parse(Pattern.compile("(....).(..).(..)").matcher(stringdate).replaceAll("--"));

您可以尝试'normalize'您输入的格式:

SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
fmt.parse(stringdate.replaceAll(" ", "-").replaceAll("/","-"));