简体   繁体   English

无法将字符串解析为正确的日期格式

[英]Unable to parse string to the right date format

I have a date in string format and I want to parse it to date format "ddMMyy". 我有一个字符串格式的日期,我想将其解析为日期格式“ ddMMyy”。

I used SimpleDateFormat as follows: 我使用了SimpleDateFormat ,如下所示:

String stringDate = "050109";
SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyy");
Date  date = dateFormat.parse(stringDate);

when I print the "date" I get in this format: 当我打印“日期”时,我得到以下格式:

Mon Jan 05 00:08:00 CET 2009 2009年1月5日星期一0:08:00

even further when I marshal the Java object I get in new format: 当我封送Java对象时,甚至得到了新的格式:

2009-01-05T00:08:00+01:00 2009-01-05T00:08:00 + 01:00

anyone have an idea how to get the right format? 任何人都有一个想法如何获得正确的格式?

如果要以解析的格式打印日期:

String fmtDate = dateFormat.format( date );

You need to format a Date object, parse method is for parsing strings into Date s, not the other way around. 您需要格式化Date对象, parse方法用于将字符串解析为Date ,而不是相反。 As you can see your string is being parsed correctly into a Date object. 如您所见,您的字符串已正确解析为Date对象。 If you want to format this date for output at some point you can use the format method of SimpleDateFormat : 如果您想格式化该日期以便在某些时候输出,可以使用SimpleDateFormatformat方法:

SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyy");
String formattedDate=dateFormat.format(date);

where date is your Date object. date是您的Date对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM