The easiest way to get the current time:
Method 1:
import java.util.Calendar;public class DateTime{public static void main(String[] args){Calendar c = Calendar.getInstance();System.out.println("Current time is:");System.out.println(c.get(Calendar.YEAR)+"year"+(c.get(Calendar.MONTH)+1)+"month"+c.get(Calendar.DATE)+"day");System.out.println(c.get(Calendar.HOUR)+"point"+c.get(Calendar.MINUTE)+"minute"+c.get(Calendar.SECOND)+"second");System.out.println("Today is the first of the year:" + c.get(Calendar.DAY_OF_YEAR) + "day");System.out.println("Today is the first of the year:" + c.get(Calendar.WEEK_OF_YEAR) + "weeks");}}Method 2:
import java.util.Date;import java.text.SimpleDateFormat; public class NowString {public static void main(String[] args) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//Set the date format System.out.println(df.format(new Date()));// New Date() is to get the current system time}}Method 3:
import java.util.Date; import java.util.Calendar; import java.text.SimpleDateFormat; public class TestDate{ public static void main(String[] args){ Date now = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//You can easily modify the date format String hehe = dateFormat.format( now ); System.out.println(hehe); Calendar c = Calendar.getInstance();//You can modify int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int date = c.get(Calendar.DATE); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); int second = c.get(Calendar.SECOND); System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second); } }