how can i change format display of datetime type? Example:
from this one(sqlite default format):
("yyyy-mm-dd hh:mm:ss");
to this:
("dd/mm/yyyy, hh:mm");
Thanks for helping.
how can i change format display of datetime type? Example:
from this one(sqlite default format):
("yyyy-mm-dd hh:mm:ss");
to this:
("dd/mm/yyyy, hh:mm");
Thanks for helping.
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)
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();
}
}