1.通过util包中的Date类来获取当前时间
方法1详细代码:
public class Exercise17 { public static void main(String[] args){ Date date = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String str = simpleDateFormat.format(date); System.out.println("用日期类Date获取当前的系统时间为:" ); System.out.println(str); } }方法1运行截图:
方法2详细代码:
public class Exercise17 { public static void main(String[] args){ Date date = new Date(); //获取年月日 String yearMonthDay = String.format("%tF",date); //获取时分秒 String hourMinuteSecond = String.format("%tT",date); System.out.println("当前的系统时间为:" ); System.out.println(yearMonthDay +" "+ hourMinuteSecond); } }方法2运行截图:
方法3详细代码:
public class Exercise17 { public static void main(String[] args){ Date date = new Date(); //分别获取年,月,日,时,分,秒 String year = String.format("%tY", date); String month = String.format("%tm", date); String day = String.format("%td", date); String hour = String.format("%tH",date); String minute = String.format("%tM",date); String second = String.format("%tS",date); System.out.println("当前的系统时间为:"); System.out.println(year + "年" + month + "月" + day + "日" + hour + "时" + minute + "分"+ second + "秒"); } }方法3运行截图:
方法4详细代码:
public class Exercise17 { public static void main(String[] args){ Date date = new Date(); System.out.println("直接打印date对象,获取英文格式的当前时间:" ); System.out.println(date); } }方法4运行截图:
方法5详细代码:
public class Exercise17 { public static void main(String[] args){ Date date = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(); String str = simpleDateFormat.format(date); System.out.println("用SimpleDateFormat的默认时间格式,获取当前时间:" ); System.out.println(str); } }方法5运行截图:
2.通过util包中的Calendar类来获取当前时间
方法1详细代码:
public class Exercise17 { public static void main(String[] args){ Calendar calendar = Calendar.getInstance(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String str3 = simpleDateFormat.format(calendar.getTime()); System.out.println("用Calendar类获取当前的系统时间为:"); System.out.println(str3); } }方法1运行截图:
方法2详细代码:
public class Exercise17 { public static void main(String[] args){ Calendar calendar = Calendar.getInstance(); //分别获取年,月,日,时,分,秒 int year = calendar.get(Calendar.YEAR); //MONTH是从0开始计数,所以要加1 int month = calendar.get(Calendar.MONTH) + 1; int day = calendar.get(Calendar.DATE); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); //该年中的第几天 int days = calendar.get(Calendar.DAY_OF_YEAR); System.out.println("用Calendar类分别获取年月日时分秒,得到当前的系统时间为:"); System.out.println(year + "年" + month + "月" + day + "日" + hour + "时" + minute + "分"+ second + "秒"); System.out.println("现在为今年的第" + days + "天"); } }方法2运行截图:
3.通过lang包中的System类来获取当前时间
方法1详细代码:
public class Exercise17 { public static void main(String[] args){ long time = System.currentTimeMillis(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String str1 = simpleDateFormat.format(time); System.out.println("用System类的currentTimeMillis()方法获取当前的系统时间为:"); System.out.println(str1); } }方法1运行截图:
String.format方法使用-浅析:
https://blog.csdn.net/u010137760/article/details/82869637
总结:获取当前时间的两步走方针!!!
step 1:拿到当前时间
(1)通过Date类
(2)通过Calendar类
(3)通过System类中的currentTimeMillis()方法
step 2:设置时间格式
(1)调用SimpleDateFormat类
(2)String中的format方法