14

如何将 dd / mm / yyyy 中的日期转换为支持 sqlite yyyy-MM-dd'T'HH: mm: ss 的格式

例如:

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;
}
4

4 回答 4

21
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;
}

这会将解析的日期作为字符串返回,格式(初始和结束)作为方法的参数。

于 2013-05-07T19:13:17.740 回答
19
   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.
    }

旧格式 : 21 06 2013

新格式:2013 06 21 00:00:00

于 2013-05-07T19:30:52.867 回答
11
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);
于 2016-05-11T08:29:16.867 回答
1
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;
}
于 2016-08-24T05:18:55.843 回答