如何更改日期时间类型的格式显示? 实施例:SQL日期时间更改格式显示
从这一个(源码默认格式):
("yyyy-mm-dd hh:mm:ss");
这样:
("dd/mm/yyyy, hh:mm");
感谢您的帮助。
如何更改日期时间类型的格式显示? 实施例:SQL日期时间更改格式显示
从这一个(源码默认格式):
("yyyy-mm-dd hh:mm:ss");
这样:
("dd/mm/yyyy, hh:mm");
感谢您的帮助。
首先得到的字符串的新日期从数据库
//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)
非常感谢,它工作正常:) – sharon2469
当你从DB日通过运行第一个代码,我们会发布然后在您保存或更新日期之前运行它通过第二个代码
风格代码与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);
}
现在你保存代码前回DB通过这个代码
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();
}
}
新SimpleDataFormat运行它( “DD/MM/YYYY,HH:MM”,Locale.ROOT).format(新日期(长)); – Thracian
对不起,但我不明白, 从数据库中获取时间我使用这个命令 | employee.getTIME()| 我回来了这个格式的日期字符串(“yyyy-mm-dd hh:mm:ss”);我怎么把这个字符串放在你给我的命令里面? – sharon2469
如果日期保存为字符串,则应在将它们重新格式化之前将它们解析为日期。检查[将字符串转换为日期](https://stackoverflow.com/questions/4216745/java-string-to-date-conversion)。 – Thracian