@TOC
System.currentTimeMillis()
在开发过程中,通常很多人都习惯使用new Date()
来获取当前时间。new Date()
所做的事情其实就是调用了System.currentTimeMillis
()。如果仅仅是需要或者毫秒数,那么完全可以使用System.currentTimeMillis
()去代替new Date()
,效率上会高一点。如果需要在同一个方法里面多次使用new Date(),通常性能就是这样一点一点地消耗掉,这里其实可以声明一个引用。
package demo.spli;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class ShowCurrentTime {
/**
* @显示当前时间
* @2014.9.3
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//获得系统的时间,单位为毫秒,转换为妙
long totalMilliSeconds = System.currentTimeMillis();
DateFormat dateFormatterChina = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM);//格式化输出
TimeZone timeZoneChina = TimeZone.getTimeZone("Asia/Shanghai");//获取时区 这句加上,很关键。
dateFormatterChina.setTimeZone(timeZoneChina);//设置系统时区
long totalSeconds = totalMilliSeconds / 1000;
//求出现在的秒
long currentSecond = totalSeconds % 60;
//求出现在的分
long totalMinutes = totalSeconds / 60;
long currentMinute = totalMinutes % 60;
//求出现在的小时
long totalHour = totalMinutes / 60;
long currentHour = totalHour % 24;
//显示时间
System.out.println("总毫秒为: " + totalMilliSeconds);
System.out.println(currentHour + ":" + currentMinute + ":" + currentSecond + " GMT");
Date nowTime = new Date(System.currentTimeMillis());
System.out.println(System.currentTimeMillis());
SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
String retStrFormatNowDate = sdFormatter.format(nowTime);
System.out.println(retStrFormatNowDate);
}
}