Java 如何将日期 dd / mm / yyyy 转换为 yyyy-MM-dd HH:mm:ss Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16426703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert a date dd / mm / yyyy to yyyy-MM-dd HH:mm:ss Android
提问by Douglas Mesquita
How can I convert a date in dd / mm / yyyy to a format that supports sqlite yyyy-MM-dd'T'HH: mm: ss
如何将 dd / mm / yyyy 中的日期转换为支持 sqlite yyyy-MM-dd'T'HH: mm: ss 的格式
for example:
例如:
public static String convertStringToData(String stringData)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/aaaa");//yyyy-MM-dd'T'HH:mm:ss
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date data = sdf.parse(stringData);
String formattedTime = output.format(data);
return formattedTime;
}
采纳答案by fcm
public static String formatDate (String date, String initDateFormat, String endDateFormat) throws ParseException {
Date initDate = new SimpleDateFormat(initDateFormat).parse(date);
SimpleDateFormat formatter = new SimpleDateFormat(endDateFormat);
String parsedDate = formatter.format(initDate);
return parsedDate;
}
This will return the parsed date as a String, with the format (both initial and end) as parameters to the method.
这会将解析后的日期作为字符串返回,格式(初始和结束)作为方法的参数。
回答by Raghunandan
SimpleDateFormat originalFormat = new SimpleDateFormat("dd MM yyyy");
SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy MM dd HH:mm:ss" );
Date date;
try {
date = originalFormat.parse("21 6 2013");
System.out.println("Old Format : " + originalFormat.format(date));
System.out.println("New Format : " + targetFormat.format(date));
} catch (ParseException ex) {
// Handle Exception.
}
Old Format : 21 06 2013
旧格式:21 06 2013
New Format : 2013 06 21 00:00:00
新格式:2013 06 21 00:00:00
回答by Sumanth
Date initDate = new SimpleDateFormat("dd/MM/yyyy").parse("10/12/2016");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String parsedDate = formatter.format(initDate);
System.out.println(parsedDate);
回答by Rahil Ali
public static final String DATE_DASH_FORMAT = "yyyy-MM-dd";
public static final String DATE_FORMAT = "MM/dd/yyyy";
public static String prepareYearMonthDateFromString( String dateStr ){
try
{
Date date = new SimpleDateFormat( DATE_FORMAT , Locale.ENGLISH ).parse( dateStr );
DateFormat formatter = new SimpleDateFormat( DATE_DASH_FORMAT , Locale.getDefault() );
dateStr = formatter.format( date.getTime() );
}
catch( ParseException e )
{
e.printStackTrace();
}
return dateStr;
}