如何在java中解析来自SQLite DB的日期

时间:2014-11-13 15:59:17

标签: java android date datetime

我正在尝试解析来自db(2014-06-11 16:58:12.903)的日期,如下所示

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", new Locale("tr-TR"));
String tarih = cursor.getString(cursor.getColumnIndex("Created");
tarih = tarih.replace("T", " ");
Object.Created = dateFormat.parse(tarih);

下面的代码返回" Fri Dec 04 16:58:12 EET 16"

DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", new Locale("tr-TR"));
String tarih = cursor.getString(cursor.getColumnIndex(field.getName()));
Object.Created = dateFormat.parse(tarih);

此方法返回的日期类似于" Wed Jun 11 16:58:12 EEST 2014"

我如何获得像dd-MM-YYYY> 11-06-2014

这样的日期

1 个答案:

答案 0 :(得分:0)

//在方法中添加以下代码行。

SimpleDateFormat fmtOut = new SimpleDateFormat("dd-MM-yyyy");
return fmtOut.format(tarih);

请注意这个因素约一个月。 (要获得今天的约会)

final Calendar c = Calendar.getInstance();

 int year = c.get(Calendar.YEAR);
 int month = c.get(Calendar.MONTH)+1; // INCREMENT MONTH VALUE BY 1 (we get 0 for january and 11 for december)
 month_name = map_instance.findMonthName(month+"");
 int day = c.get(Calendar.DAY_OF_MONTH);

//年份值的问题可能是因为从数据库中恢复的数据

   DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", new Locale("tr-TR"));
    String tarih ="";
    try
    {
    tarih = dateFormat.parse(cursor.getString(cursor.getColumnIndex("Created"));
    }catch (ParseException e) {
    tarih = "";
    }
log.w("Data from database",tarih);
    tarih = tarih.replace("T", " ");
    Object.Created = tarih;