System.currentTimeMillis()产生一个当前的毫秒,这个毫秒其实就是自1970年1月1日0时起的毫秒数,Date()其实就是相当于Date(System.currentTimeMillis());因为Date类还有构造Date(longdate),用来计算long秒与1970年1月1日之间的毫秒差。
得到了这个毫秒数,我们自己也可以算起现在的年月日周时,但是这不是我们去计算的,因为有Calendar。Calendar最终出的结果就是年月日周时时区。
System.currentTimeMillis() 获得的是自1970-1-01 00:00:00.000到当前时刻的时间距离,类型为long
String.valueOf(System.currentTimeMillis()) 这个语句可转为以下的型式:
long ct = System.currentTimeMillis();
String t = String.valueOf(ct);
其实上面的String t就相当于 ct+"";
只是转为字符串格式
public String refFormatNowDate() {
Date nowTime = newDate(System.currentTimeMillis());
SimpleDateFormat sdFormatter = newSimpleDateFormat("yyyy-MM-dd");
String retStrFormatNowDate =sdFormatter.format(nowTime);
return retStrFormatNowDate;
}
本文深入探讨了Java中日期时间的基本概念,重点介绍了如何使用System.currentTimeMillis()获取当前时间戳,并通过Calendar类实现年月日时的自定义格式化。通过实例演示了日期时间转换与格式化的过程,旨在帮助开发者更好地理解并应用Java的日期时间API。
3767

被折叠的 条评论
为什么被折叠?



