import java.util.Calendar; import java.util.Date; import java.text.SimpleDateFormat; public class DateExamples { public static void main(String[] args) { // ======================================================== // Part 1. Print current date and time in default format... // ======================================================== // Use the Calendar class to get a snapshot of this precise moment. // Convert to a Date and then to a string. Calendar thisInstant = Calendar.getInstance(); Date rightNow = thisInstant.getTime(); String ex1 = rightNow.toString(); System.out.println("Part 1. Current time/date (default format): " + ex1); System.out.println(""); // ================================================================ // Part 2. Get the current date and time, and print it out in a // specified way as described by the SimpleDateFormat that we've // designated below. // An alternative way to get the current date and time that doesn't // involve the Calendar class. // ================================================================ Date now = new Date(System.currentTimeMillis()); SimpleDateFormat sdfEx2 = new SimpleDateFormat("EEEE, MMM d, yyyy hh:mm aaa"); String ex2 = sdfEx2.format(now); System.out.println("Part 2. Current time and date (custom format): " + ex2); System.out.println(); // ================================================================ // Part 3. Print today's date (only) in a specified way as described // by the alternative SimpleDateFormat that we've designated. // ================================================================ SimpleDateFormat sdfEx3 = new SimpleDateFormat("MMMM d, yyyy"); String ex3 = sdfEx3.format(now); System.out.println("Part 3. Today's date (custom format 1): " + ex3); System.out.println(); // ================================================================ // Part 4. Print today's date (only) in yet another format. // ================================================================ SimpleDateFormat sdfEx4 = new SimpleDateFormat("MM/dd/yyyy"); String ex4 = sdfEx4.format(now); System.out.println("Part 4. Today's date (custom format 2): " + ex4); System.out.println(); // ================================================================ // Part 5. Craft a specific date as a Date object. // ================================================================ Calendar cal = Calendar.getInstance(); cal.set(1975, Calendar.DECEMBER, 1 ); Date birthDate = cal.getTime(); // We'll reuse the SimpleDateFormat from above. String ex5 = sdfEx4.format(birthDate); System.out.println("Part 5. Birth date: " + ex5); System.out.println(); // ================================================================ // Part 6. Compare two dates... // ================================================================ // We'll reuse the Calendar instance from Part 5 to craft two // different Dates. cal.set(2005, Calendar.JULY, 1); Date startSummerVacation = cal.getTime(); cal.set(2005, Calendar.JUNE, 1); Date endSummerVacation = cal.getTime(); // We'll use the before() method to compare the dates (note that there's // also an after() method and an equals() method ...). System.out.println("*** Part 6. Check scheduling of summer vacation" ); System.out.println("*** ===========================================" ); if (startSummerVacation.before( endSummerVacation) ) System.out.println("*** Dates are in correct order" ); else System.out.println("*** Dates are in incorrectly ordered!!!" ); } }