SQL日期时间更改格式显示

时间:2017-10-18 11:48:59

标签: android date datetime android-sqlite

如何更改日期时间类型的格式显示? 例如:

来自这个(sqlite默认格式):

("yyyy-mm-dd hh:mm:ss");

enter image description here

到此:

("dd/mm/yyyy, hh:mm");

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

首先从数据库的字符串中获取一个新的日期

 //Convert the date string (I recieve the Date in String format from the SQLite DB) to a Date
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefault());
    Date convertedDate = new Date();
    try {
//Here you convert the string from the employee time to a Date
        convertedDate = dateFormat.parse(employee.getTIME());
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

然后使用新的SimpleDateFormat格式化日期

String formatedDateToDisplay = new SimpleDateFormat("dd/MM/yyyy, HH:mm", Locale.getDefault()).format(convertedDate)

答案 1 :(得分:0)

当您从数据库中获取日期时,我们将发布第一个代码,然后在您保存或更新DATE之前通过发布的第二个代码运行它

带DASHES的样式代码

 batch.draw(mTextureBg, 0 , 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

现在,在将代码保存回数据库之前,请通过此代码运行

        helper = new DBHelper(this);
    dbList = new ArrayList<>();
    dbList = helper.getDataFromDB();

    if (dbList.size() > 0 && tORf.equalsIgnoreCase("false")) {

        btnSave.setVisibility(View.INVISIBLE);
        String NVrowid = String.valueOf(dbList.get(position).getRowid());
        String NVstation = dbList.get(position).getStation_Name();
        String NVpurchasedate = dbList.get(position).getDate_of_Purchase();
        StringBuilder addDASH = new StringBuilder(NVpurchasedate);
        addDASH.insert(2, '-');
        addDASH.insert(5, '-');
        /* Code ABOVE formats date XX-XX-XXXX sent from DB */
        /* Stored as a 8 character string this format 01292017*/
        String NVcost = dbList.get(position).getGas_Cost();
        etStation.setText(NVstation);
        etPurchaseDate.setText(addDASH);
        etCost.setText(NVcost);
    }
相关问题