Java获取当前时间方法总结
最简单获取当前时间的方法:
方法一:
importjava.util.Calendar;
publicclassDateTime{
publicstaticvoidmain(String[]args){
Calendarc=Calendar.getInstance();
System.out.println("当前时间为:");
System.out.println(c.get(Calendar.YEAR)+"年"+(c.get(Calendar.MONTH)+1)+"月"+c.get(Calendar.DATE)+"日");
System.out.println(c.get(Calendar.HOUR)+"点"+c.get(Calendar.MINUTE)+"分"+c.get(Calendar.SECOND)+"秒");
System.out.println("今天是一年中的第:"+c.get(Calendar.DAY_OF_YEAR)+"天");
System.out.println("今天是一年中的第:"+c.get(Calendar.WEEK_OF_YEAR)+"个星期");
}
}
方法二:
importjava.util.Date;
importjava.text.SimpleDateFormat;
publicclassNowString{
publicstaticvoidmain(String[]args){
SimpleDateFormatdf=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");//设置日期格式
System.out.println(df.format(newDate()));//newDate()为获取当前系统时间
}
}
方法三:
importjava.util.Date;
importjava.util.Calendar;
importjava.text.SimpleDateFormat;
publicclassTestDate{
publicstaticvoidmain(String[]args){
Datenow=newDate();
SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy/MM/ddHH:mm:ss");//可以方便地修改日期格式
Stringhehe=dateFormat.format(now);
System.out.println(hehe);
Calendarc=Calendar.getInstance();//可以对每个时间域单独修改
intyear=c.get(Calendar.YEAR);
intmonth=c.get(Calendar.MONTH);
intdate=c.get(Calendar.DATE);
inthour=c.get(Calendar.HOUR_OF_DAY);
intminute=c.get(Calendar.MINUTE);
intsecond=c.get(Calendar.SECOND);
System.out.println(year+"/"+month+"/"+date+""+hour+":"+minute+":"+second);
}
}