用于日/月/年的java字符串到日期的对话
如何将此字符串转换为日期? 2015年5月14日和 2014年12月12日 我试过使用“dd/MM/yyy”和“dd/MM/yy”,但它们不起作用用于日/月/年的java字符串到日期的对话,java,date,format,Java,Date,Format,如何将此字符串转换为日期? 2015年5月14日和 2014年12月12日 我试过使用“dd/MM/yyy”和“dd/MM/yy”,但它们不起作用 SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); 你把日子和月份搞混了 SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); 在您的问题中,dd和MM值在传递到SimpleDataFormat
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
你把日子和月份搞混了
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
在您的问题中,dd和MM值在传递到SimpleDataFormat的模式字符串中的错误位置,您需要交换它们
从要转换的日期可以看出,在日期字符串中,月份在日期之前。因此,在模式字符串中,MM必须在dd之前。使用简单格式:-
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Some Helpfull stuff
// SimpleDateFormat can be used to control the date/time display format:
// E (day of week): 3E or fewer (in text xxx), >3E (in full text)
// M (month): M (in number), MM (in number with leading zero)
// 3M: (in text xxx), >3M: (in full text full)
// h (hour): h, hh (with leading zero)
// m (minute)
// s (second)
// a (AM/PM)
// H (hour in 0 to 23)
// z (time zone)
如何处理这一年?我有15年而不是2015年。尽管
MM/dd/yy
很清楚如何处理这一年,但这种模式仍然有效。我有15年而不是2015年。稍微解释一下会有帮助。
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Some Helpfull stuff
// SimpleDateFormat can be used to control the date/time display format:
// E (day of week): 3E or fewer (in text xxx), >3E (in full text)
// M (month): M (in number), MM (in number with leading zero)
// 3M: (in text xxx), >3M: (in full text full)
// h (hour): h, hh (with leading zero)
// m (minute)
// s (second)
// a (AM/PM)
// H (hour in 0 to 23)
// z (time zone)