This code will print the current Date and Time to the console.
import java.text.SimpleDateFormat; import java.util.Calendar; public class DateAndTime{ public static final String DATE_FORMAT_NOW = "dd-MM-yyyy HH:mm:ss"; public static String dt; public static String DateTime() { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); return sdf.format(cal.getTime()); } public static void main(String[] args){ DateAndTime dAT = new DateAndTime(); dt = dAT.DateTime(); System.out.println(dt); } }
Output example:
19-05-2008 13:35:07