java 获取时间,1:date 2:Calendar
import java.text.SimpleDateFormat; import java.util.*; import java.io.*; public class Score{ public static void main(String args[]){ //获取程序开始的运行时间 long start=System.currentTimeMillis(); //使用Date和SimpleDateFormat 获取时间 SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS"); Date date=new Date(); String res=df.format(date); System.out.println(res); //使用Calendar获取时间 Calendar now=Calendar.getInstance(); int year=now.get(Calendar.YEAR); int month=now.get(Calendar.MONTH+1); //此处应该注意month+1才是正确的月份 int day=now.get(Calendar.DAY_OF_MONTH); int hour=now.get(Calendar.HOUR_OF_DAY); int minute=now.get(Calendar.MINUTE); int second=now.get(Calendar.SECOND); int millisecond=now.get(Calendar.MILLISECOND); System.out.println(year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second+":"+millisecond); //获取当前时间 SimpleDateFormat df2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS"); Date date2=new Date(System.currentTimeMillis()); String res2=df2.format(date2); System.out.println(res2); long end=System.currentTimeMillis(); //输出运行程序的时间间隔 System.out.println(end-start); }; }