我有一个格式为 dd.MM.yy HH:mm
的字符串,例如2014 年 4 月 12 日 07:00
。
我使用以下行将该字符串转换为日期对象:
SimpleDateFormat sdfToDate = new SimpleDateFormat("dd.MM.yy HH:mm");
Date date_new = sdfToDate.parse(date);
但是日期采用以下格式:
Apr 12, 2014 7:00:00 AM
我需要显示 24 小时时间。我怎样才能做到这一点?
最佳答案
试试这个
String dateStr = "Apr 12, 2014 7:00:00 AM";
DateFormat readFormat = new SimpleDateFormat( "MMM dd, yyyy hh:mm:ss aa");
DateFormat writeFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
Date date = null;
try
{
date = readFormat.parse( dateStr );
}
catch ( ParseException e )
{
e.printStackTrace();
}
if( date != null )
{
String formattedDate = writeFormat.format( date );
}
关于java - 字符串到日期 24 小时格式 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23010437/