-3

how can i change format display of datetime type? Example:

from this one(sqlite default format):

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

enter image description here

to this:

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

Thanks for helping.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
sharon2469
  • 89
  • 1
  • 11
  • new SimpleDataFormat("dd/mm/yyyy, hh:mm", Locale.ROOT).format(new Date(long)); – Thracian Oct 18 '17 at 11:58
  • Sorry, but i am not understand, to get the time from database i am use this command | employee.getTIME() | And i am getting back string with date on this format ("yyyy-mm-dd hh:mm:ss"); how i put this string inside the command that you gave me? – sharon2469 Oct 18 '17 at 12:56
  • If dates are saved as Strings you should parse them to Date before reformatting them. Check for [converting String to Date](https://stackoverflow.com/questions/4216745/java-string-to-date-conversion). – Thracian Oct 18 '17 at 13:07

2 Answers2

0

First get a new Date from the String from the Database

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

Then use a new SimpleDateFormat to format the date

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

When you get the date from the DB run it through the first code we will post then before you save or update the DATE run it through the second code posted

Style code with DASHES

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

Now before you save the code back to the DB run it through this code

                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();
                }
            }
Vector
  • 3,066
  • 5
  • 27
  • 54