您的位置:首页 > 移动开发 > Android开发

Android积累--常用小知识点

2017-03-23 15:33 477 查看
1、Java数字格式化(前面补零)

int i = 1;
// 0 代表前面补充0
// 4 代表长度为4
// d 代表参数为正数型
String str = String.format("%04d", youNumber);
Log.e("msg", str);//0001


2、日期格式化

获取系统时间并格式化
方法1:
long time=System.currentTimeMillis();//long now =android.os.SystemClock.uptimeMillis();
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1=new Date(time);
String t1=format.format(d1);
Log.e("msg", t1);

方法2:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
String t=format.format(new Date());
Log.e("msg", t);

方法3:
Calendar calendar = Calendar.getInstance();
String created = calendar.get(Calendar.YEAR) + "年"
+ (calendar.get(Calendar.MONTH)+1) + "月"//从0计算
+ calendar.get(Calendar.DAY_OF_MONTH) + "日"
+ calendar.get(Calendar.HOUR_OF_DAY) + "时"
+ calendar.get(Calendar.MINUTE)+"分"
+ +calendar.get(Calendar.SECOND)+"秒";
Log.e("msg", created);

方法4:
Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone
t.setToNow(); // 取得系统时间。
String time=t.year+"年 "+(t.month+1)+"月 "+t.monthDay+"日 "+t.hour+"h "+t.minute+"m "+t.second;
Log.e("msg", time);

获取星期日期:
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK);
String today = null;
if (day == 2) {
today = "Monday";
} else if (day == 3) {
today = "Tuesday";
} else if (day == 4) {
today = "Wednesday";
} else if (day == 5) {
today = "Thursday";
} else if (day == 6) {
today = "Friday";
} else if (day == 7) {
today = "Saturday";
} else if (day == 1) {
today = "Sunday";
}
Log.e("Today is:--> " + today);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: