Date class is available in java.util package, it represents a specific instant of time, with millisecond precision. Date allows the interpretation of dates as year, month, day, hour, minute, and second values. It also allows the formatting and parsing of date strings.
Date Constructors:
Date Methods:
Simple Date Format Codes:
import java.util.*;
class DateExampleTest { public static void main(String arg[]) { try { // Get current date and time Date date = new Date(); // LINE A System.out.println(date); // Convert Date to String. SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss"); // LINE B String stringDate = dateFormat.format(date); // LINE C System.out.println(stringDate); // Convert String to Date. SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); String dateInString = "15/08/1947 02:25:56"; date = df.parse(dateInString); // LINE D System.out.println(date); } catch (Exception e) { e.printStackTrace(); } } } OUTPUTFri Apr 18 14:25:43 IST 2014 18/04/2014 02:25:43 Fri Aug 15 02:25:56 IST 1947 DESCRIPTIONIn this program, at LINE A we are getting the current date and time . At LINE C we are converting date to string format and string to date format at LINE D by using SimpleDateFormat at LINE B and Date class. The output varies as the time various so it need not be same as the present output.
import java.text.SimpleDateFormat; import java.util.Date;
class DateMethodTest { public static void main(String arg[]) { try { Date date1 = new Date(); // LINE A // Current date is stored in date1 String dateInString = "15-08-1947 02:25:56"; SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); Date date2 = df.parse(dateInString); // LINE B System.out.println("The date1 is after date2 : " + date1.after(date2)); System.out.println("The date2 is before date1 : " + date2.before(date1)); Date date3 = (Date) date1.clone(); // Copies date1 into date3 System.out.println(date1.compareTo(date3)); // LINE C // Prints 0 System.out.println(date1.compareTo(date2)); // LINE D // Prints 1 System.out.println("The date3 is equals to date1 : " + date3.equals(date1)); System.out.println("Milli second for date1 : " + date1.getTime()); System.out.println("Hash code for date1 : " + date1.hashCode()); System.out.println("The date1 before setTime : " + date1); date1.setTime(10000); // LINE E System.out.println("The date1 after setTime : " + date1); String dateString = date1.toString(); // LINE F System.out.println("The string form of date1 : " + dateString); } catch (Exception e) { e.printStackTrace(); } } } OUTPUTThe date1 is after date2 : true The date2 is before date1 : true 0 1 The date3 is equals to date1 : true Milli second for date1 : 1397812350984 Hash code for date1 : 1947980109 The date1 before setTime : Fri Apr 18 14:42:30 IST 2014 The date1 after setTime : Thu Jan 01 05:30:10 IST 1970 The string form of date1 : Thu Jan 01 05:30:10 IST 1970 DESCRIPTIONIn this program, At LINE A current date is stored in date1 and at LINE B the value of String dateInstring is stored in date2 . The after, before, clone, compareTo, equals, getTime, hashCode, setTime( ) and toString methods are applied on these two dates, compareTo returns 0 if they are same else 1. The output varies as the time various so it need not be same as the present output for current date. THINGS TO TRY
- Compare two dates
1/1/1990 and 31/1/1990 using compareTo method which is shown at LINE C in the above example.
|