读取文件列表时,获取到文件的修改时间,但怎么转换为正常可辨的时间值呢?

//function A
//CODE...
File myfile = new File(apk);
long mytime = myfile.lastModified();
String timeStr = time2String( mytime );
//end of A

private String time2String( long time ){

//方式1 按系统的日期方式
//  Calendar   cal = Calendar.getInstance();
//  cal.setTimeInMillis( time );
//  return cal.getTime().toLocaleString();

//方式2 按自定义格式
  SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
  Date currentTime = new Date( time );
  return format1.format(currentTime);
}


把大小转换为易读的格式,这里偷懒,最大是M的:

private String size2string(long size){
  DecimalFormat df = new DecimalFormat("0.00");
  String mysize = "";
  if( size > 1024*1024){
    mysize = df.format( size / 1024f / 1024f ) +"M";
  }else if( size > 1024 ){
    mysize = df.format( size / 1024f ) +"K";
  }else{
    mysize = size + "B";
  }
  return mysize;
}


更多方式可以参考:
1 JAVA时间格式化处理
2 Rounding off a float to two decimal places


原创内容如转载请注明:来自 阿权的书房
收藏本文到网摘
Tags:
发表评论
表情
emotemotemotemotemotemotemotemotemotemotemotemotemot
emotemotemotemotemotemotemotemotemotemotemotemot
打开HTML 打开UBB 打开表情 隐藏
昵称   密码   游客无需密码
网址   电邮   [注册]
               

验证码 不区分大小写
 

阅读推荐

服务器相关推荐

开发相关推荐

应用软件推荐