Android中的日期格式

时间:2013-03-12 04:05:12

标签: android sqlite

在Android中将数据保存到SQLite数据库时,如何将此格式的日期mm/dd/yyyy转换为yyyy-mm-dd

4 个答案:

答案 0 :(得分:2)

然后您可以使用Calendar对象。

        Calendar cal=Calendar.getInstance();

您可以使用

检索相同内容
      long date=cursor.getLong(cursor.getColumnIndex("expired_date"));
      Calendar cal=Calendar.getInstance();
      cal.clear();
      cal.setTimeInMillis(date);

有关各种日期格式,请参阅此链接 http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html

     Calendar cal=Calendar.getInstance();
     String date_time=String.format("%1$tY  %1$tB %1$te,%1$tI:%1$tM:%1$tS %1$Tp",cal);
     Toast.makeText(getApplicationContext(),date_time,Toast.LENGTH_SHORT).show();

答案 1 :(得分:2)

SimpleDateFormat  new_format= new SimpleDateFormat("yyyy-MM-dd);
SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy");

Date date;
String local_date = null;
try{
    date = sdf.parse(value);

    local_date = new_format.format(date);

}catch(ParseException e)
{

e.printStackTrace();

}

答案 2 :(得分:1)

查看此代码..这将解决有关日期时间转换的所有疑问。

    public static String convertDateStringFormat(String currentFormat,
            String newFormat, String strDate) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(currentFormat);
        Date date = null;
        try {
            date = dateFormat.parse(strDate);
        } catch (ParseException e) {
            CommonFunctions.DoCatchOperation(e);
        }

        String newFormatString = convertDateToString(newFormat, date);
        return newFormatString;
    }

    public static Date convertStringToDate(String dateFormatter, String strDate) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(dateFormatter);
        Date date = null;
        try {
            date = dateFormat.parse(strDate);
        } catch (ParseException e) {
            CommonFunctions.DoCatchOperation(e);
        }
        return date;
    }

    public static String convertDateToString(String dateFormatter, Date date) {
        if (date == null)
            return "";
        else {
            SimpleDateFormat dFormat = new SimpleDateFormat(dateFormatter);
            return dFormat.format(date);
        }
    }

答案 3 :(得分:0)

public String formatDate(String value) {

    SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy",
            Locale.US);

    Date date;
    String dateformat = "";
    try {
        date = sdf.parse(value);
        sdf.applyPattern("yyyy-mm-dd");
        dateformat = sdf.format(date);
    } catch (Exception e) {
        return "";
    }

    return dateformat;

}