System.currentTimeMillis()得到的是当前时间距离1970年凌晨的秒数,通过装换可以得到当前时间long dateTaken = System.currentTimeMillis(); Date date = new Date(dateTaken); String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
public static void main(String[] args) throws Exception { URL url=new URL("http://www.bjtime.cn");//取得资源对象 URLConnection uc=url.openConnection();//生成连接对象 uc.connect(); //发出连接 long ld=uc.getDate(); //取得网站日期时间 Date date=new Date(ld); //转换为标准时间对象 //分别取得时间中的小时,分钟和秒,并输出 System.out.print(date.getHours()+"时"+date.getMinutes()+"分"+date.getSeconds()+"秒"); }
Date date = new Date(dateTaken);
String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
URL url=new URL("http://www.bjtime.cn");//取得资源对象
URLConnection uc=url.openConnection();//生成连接对象
uc.connect(); //发出连接
long ld=uc.getDate(); //取得网站日期时间
Date date=new Date(ld); //转换为标准时间对象
//分别取得时间中的小时,分钟和秒,并输出
System.out.print(date.getHours()+"时"+date.getMinutes()+"分"+date.getSeconds()+"秒");
}