【问题标题】:How to parse Date coming from SQLite DB in java如何在java中解析来自SQLite DB的日期
【发布时间】:2014-11-13 15:59:16
【问题描述】:

我正在尝试解析来自 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 这样的日期

【问题讨论】:

    标签: java android date datetime


    【解决方案1】:

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

    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;
    

    【讨论】:

    • 谢谢。这似乎可行,但我没有得到它解析“2014-06-11 00:00:00”之类的东西,如“Fri Dec 04 00:00:00 EET 16”你认为今年有什么问题吗?
    • 原因是 - 月份从 0 到 11。根据 SimpleDateFormat 类,数字 11 代表 12 月。
    • 谢谢,但我知道年份是 2014 年,但在解析日期中有 04 和 16?这部分我没有得到
    • @BahadirGul,尝试使用上述代码,如果您仍然遇到问题,请提及您从 DB 获得的日志值
    猜你喜欢
    • 1970-01-01
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多