1 Packagecom.java.test;2 3 ImportJava.text.SimpleDateFormat;4 ImportJava.util.Calendar;5 6 Public classTimes {7 8 Public Static voidMain (string[] args) {9SimpleDateFormat format =NewSimpleDateFormat ("Yyyy-mm-dd-hh-mm-ss-sss");TenString time =Format.format (Calendar.getinstance (). GetTime ()); OneSystem.out.println ("Full Time and Date:" +Time ); A -SimpleDateFormat FORMAT2 =NewSimpleDateFormat ("Yyyy-mm-dd"); -String time2 =Format2.format (Calendar.getinstance (). GetTime ()); theSystem.out.println ("Full Date:" +time2); - -SimpleDateFormat FORMAT3 =NewSimpleDateFormat ("Hh-mm-ss-sss"); -String Time3 =Format3.format (Calendar.getinstance (). GetTime ()); +System.out.println ("Full time:" +time3); - } + A}
Execution Result:
Full time and Date: 2016-07-11-17-00-49-861
Full Date: 2016-07-11
Full time: 17-00-49-862
As can be seen from the results, the local current time can be obtained by calendar.getinstance (). GetTime () and then displayed according to the different (Date and time Patterns) in format.
Description of the SimpleDateFormat class in the Date and time patterns reference Java API
Note: Avoid using the following method, because the second time to get the information is the same as the first time.
Calendar cal = Calendar.getinstance (); Cal.gettime ();//Get Once Other code Cal.gettime ();//Get one More time |
Use calendar to get the current date and time