how to convert a date dd / mm / yyyy to yyyy-MM-dd HH:mm:ss Android [duplicate]

只谈情不闲聊 提交于 2019-12-09 09:46:32

问题


How can I convert a date in dd / mm / yyyy to a format that supports 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;
}

回答1:


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.




回答2:


   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

New Format : 2013 06 21 00:00:00




回答3:


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);



回答4:


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


来源:https://stackoverflow.com/questions/16426703/how-to-convert-a-date-dd-mm-yyyy-to-yyyy-mm-dd-hhmmss-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!