Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将2015年9月24日转换为20150924?_Java - Fatal编程技术网

Java 如何将2015年9月24日转换为20150924?

Java 如何将2015年9月24日转换为20150924?,java,Java,如何将格式为mm/dd/yyyy的输入日期转换为格式为yyyymmdd的整数 我试过: SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd"); String input = "09/25/2015"; String t; t = ft.format(input); 及 这些都不起作用;第一个给了我一个运行时错误 假设输入日期为字符串类型: String strDate = "09/24/2015"; String[] tok =

如何将格式为
mm/dd/yyyy
的输入日期转换为格式为
yyyymmdd
的整数

我试过:

 SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
 String input = "09/25/2015"; 
 String t;
 t = ft.format(input);


这些都不起作用;第一个给了我一个运行时错误

假设输入日期为字符串类型:

String strDate = "09/24/2015";
String[] tok = strDate.split("/");
System.out.println(tok[2] + tok[0] + tok[1]);

您可以将它们拆分为标记。

您可以通过两种方式完成:字符串操作,或日期解析和重新格式化

// String substitution using regular expression
System.out.println("09/24/2015".replaceFirst("^(\\d{2})/(\\d{2})/(\\d{4})$", "$3$1$2"));

// Lenient date reformatting
System.out.println(new SimpleDateFormat("yyyyMMdd").format(new SimpleDateFormat("MM/dd/yyyy").parse("09/24/2015")));
字符串替换对坏格式没有任何作用,即使日期值不正确,也会转换好格式

对于错误的格式,日期重新格式化将失败(例外),并将“调整”错误的日期值(见下文),因为默认情况下它比较宽松。更改为“严格”也会在错误日期值时失败:

// Strict date reformatting
SimpleDateFormat mdy = new SimpleDateFormat("MM/dd/yyyy");
mdy.setLenient(false);
SimpleDateFormat ymd = new SimpleDateFormat("yyyyMMdd");
System.out.println(ymd.format(mdy.parse("09/24/2015")));
上述三种方法都将打印:

20150924
要显示错误日期值的影响,请使用错误格式的dn:

// Showing results with bad date
System.out.println("09/34/2015".replaceFirst("^(\\d{2})/(\\d{2})/(\\d{4})$", "$3$1$2"));
System.out.println(new SimpleDateFormat("yyyyMMdd").format(new SimpleDateFormat("MM/dd/yyyy").parse("09/34/2015")));
try { ymd.format(mdy.parse("09/34/2015")); } catch (Exception e) { System.out.println(e); }

// Showing results with bad formats
System.out.println("Hello".replaceFirst("^(\\d{2})/(\\d{2})/(\\d{4})$", "$3$1$2"));
try { new SimpleDateFormat("MM/dd/yyyy").parse("Hello"); } catch (Exception e) { System.out.println(e); }
try { ymd.format(mdy.parse("Hello")); } catch (Exception e) { System.out.println(e); }
输出

20150934
20151004
java.text.ParseException:无法解析的日期:“09/34/2015”
你好
java.text.ParseException:无法解析的日期:“您好”
java.text.ParseException:无法解析的日期:“您好”

那么您的输入是字符串?是你的朋友。请展示你的努力。你做过任何研究或编码尝试吗?不知道怎么做。我们使用M3转换器工具COMDAT来转换日期,但在本例中它不起作用。我试过这样做:SimpleDateFormat ft=newsimpledateformat(“yyyyMMdd”);字符串输入=“09/25/2015”;字符串t;t=英尺格式(输入);可能没有任何工作机会,所以我来找专家。@KinnethHerring我建议您将刚才的评论放到您的问题中,并投票支持重新开放。正如我所看到的,您确实做了一些努力。这是一个字符串,但我希望有一种方法可以做到这一点,同时允许一些日期验证。使用regex的较短版本:
“09/24/2015”。replaceFirst(^(\\d{2})/(\\d{2})/(\\d{4})$,“$3$1$2”)
@KinnethHerring如果它是字符串,有很多方法可以做到这一点,包括上面的解决方案,这也是有效的。您可以自己进行验证,尤其是在您已经标记了它们之后。你可以像
month=Integer.parseInt(tok[1])
那样做,然后检查
if(month>0&<13)
。我已经获得了建议的代码,正在通过M3中提供的软件对其进行验证。谢谢大家的帮助。当我看到令牌代码时,灯泡熄灭了。我已经把日期分开了,应该知道如何得到它。@KinnethHerring很高兴这有帮助。你可以考虑通过点击我的答案旁边的空格来接受我的解决方案。作为回报,您还可以获得2个销售代表。
// Showing results with bad date
System.out.println("09/34/2015".replaceFirst("^(\\d{2})/(\\d{2})/(\\d{4})$", "$3$1$2"));
System.out.println(new SimpleDateFormat("yyyyMMdd").format(new SimpleDateFormat("MM/dd/yyyy").parse("09/34/2015")));
try { ymd.format(mdy.parse("09/34/2015")); } catch (Exception e) { System.out.println(e); }

// Showing results with bad formats
System.out.println("Hello".replaceFirst("^(\\d{2})/(\\d{2})/(\\d{4})$", "$3$1$2"));
try { new SimpleDateFormat("MM/dd/yyyy").parse("Hello"); } catch (Exception e) { System.out.println(e); }
try { ymd.format(mdy.parse("Hello")); } catch (Exception e) { System.out.println(e); }