获取时间并替换到String.xml 中

mac2022-06-30  28

调用System.currentTimeMillis()获得当前时间

SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");Date date=new Date(System.currentTimeMillis());Log.i("myDebug", ""+date);Log.i("myDebug", ""+format.format(date));/*打印结果: * Thu Sep 15 10:22:27 GMT+08:00 2011* 2011年09月15日 10:22:27* */

调用new java.util.Date()获取当前时间 

SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date2=new java.util.Date(); Log.i("myDebug", ""+date2);Log.i("myDebug", ""+format2.format(date2));/*打印结果 * Thu Sep 15 10:25:52 GMT+08:00 2011 * 2011-09-15 10:25:52 * */

string.xml定义

<string name="time">现在北京时间%1$s</string>

%后1表示中有一个值需要修改,s表示字符串;另外d表示是整型

java:

String text = String.format(getString(R.string.time), format.format(date));Log.i("myDebug", ""+text);/*打印结果:*现在北京时间2011年09月15日 10:40:40**/

 

转载于:https://www.cnblogs.com/xiaobuild/archive/2011/09/15/2177072.html

最新回复(0)