您的位置:首页 > 其它

Date和Long之间互换,实现文件打开时间的的设置和获取

2015-03-03 15:57 323 查看
<pre name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif;">不出意外的话,代码应该如下,但是……</span>


事实告诉我们,正如方法名一样,<pre name="code" class="java">lastModified指的是上一次修改时间。这样,我们就想一个不影响原文内容,又可以改变修改时间的方法。。。


</pre><pre name="code" class="java"> time= file.lastModified();

SimpleDateFormatsdf= new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
java.util.Datedt = new Date(time);
StringsDateTime = sdf.format(dt);  //得到精确到秒的表示:08/31/2006 21:08:00

file_lastmodified.setText("file_lastmodifiedtime:"+String.valueOf(time)+"===="+sDateTime);

DatecurDate  = new  Date(System.currentTimeMillis());//获取当前时间

time3= curDate.getTime();

current_time.setText("currenttime:"+String.valueOf(time3)+"===="+sdf.format(curDate));

file.setLastModified(time3);
time = file.lastModified();

SimpleDateFormat sdf= new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
java.util.Date dt = new Date(time);
String sDateTime = sdf.format(dt);  //得到精确到秒的表示:08/31/2006 21:08:00
file_lastmodified.setText("file_lastmodifiedtime:"+String.valueOf(time)+"===="+sDateTime);
</pre><pre name="code" class="java">
解决方案:
采用一个文件追加的方式来添加内容,当然,只是添加一个“”,也就是空,这样就能实现我们要的效果啦
</pre><pre name="code" class="java">  BufferedWriter out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));

out.write("");

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();

} catch (IOException e) {
e.printStackTrace();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐