【问题标题】:SQL datetime change format displaySQL 日期时间更改格式显示
【发布时间】:2018-03-30 06:51:14
【问题描述】:

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

从这个(sqlite默认格式):

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

到这里:

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

感谢您的帮助。

【问题讨论】:

  • new SimpleDataFormat("dd/mm/yyyy, hh:mm", Locale.ROOT).format(new Date(long));
  • 对不起,但我不明白,要从数据库中获取时间,我使用这个命令 |员工.getTIME() |我正在以这种格式返回带有日期的字符串(“yyyy-mm-dd hh:mm:ss”);我如何将此字符串放入您给我的命令中?
  • 如果日期保存为字符串,您应该在重新格式化之前将它们解析为 Date。检查converting String to Date。

标签: android date datetime android-sqlite


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    带破折号的样式代码

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

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

                    if(etPurchaseDate.length()==10) {
                                /* NO spaces \ No = \ NO - \ NO -- \ ONLY numbers \ NO - at the end*/
                    String tstr = "^(?!.*\\s)^(?=.*)^(?!-)(?!.*--)[0-9-]+(?<!-)$";
                    String astr = etPurchaseDate.getText().toString().trim();
                    Pattern regex = Pattern.compile(tstr);
                    Matcher regexMatcher = regex.matcher(astr);
    
                    boolean foundMatch = regexMatcher.find();
    
    
                    if (foundMatch == true ) {//IF FOUND REMOVE THE DASH'S AND STORE DATA
                    /* Routine puts DATE back in DB as 8 Character String REMOVES dashes "-" */
                        StringBuilder removeTHIS = new StringBuilder(etPurchaseDate.getText().toString());
                        String value = removeTHIS.toString().replace("-", "");
                        etPurchaseDate.setText(value);
                        etCost.requestFocus();
                        purchase_date = value;
                        etCost.requestFocus();
                        //return;
                        Toast.makeText(DetailsActivity.this, "Date was Formatted for Storage", Toast.LENGTH_LONG).show();
                    }
                }
    

    【讨论】:

      猜你喜欢
      • 2020-12-22
      • 1970-01-01
      • 1970-01-01
      • 2020-08-23
      • 2013-06-29
      • 2017-05-13
      • 2015-08-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多