Java Date
Date provides methods for examining and manipulating date and time. Time in java is measured in milliseconds since January 1, 1970. Java attempts to handle time from the system with which it is interfacing. UTC is coordinated Universal Time, which seems to be the worldwide standard.
The Java language provides direct support for time based objects. The java.util.Date and the java.util.Calendar class provides access to storing and manipulating dates. It is recommended to use Calendar if possible. Existing API may required that you convert from Date to Calendar and vice versa, e.g. during database access you often get a java.sql.Date object.
To create a Date object for the current date and time use the noargs Date() constructor like this:
Date now = new Date();
To create a Date object for a specific time, pass the number of milliseconds since midnight, January 1, 1970, Greenwich Meantime to the constructor, like this:
Date midnight_jan2_1970 = new Date(24L*60L*60L*1000L);
You can return the number of milliseconds in the Date as a long, using the getTime() method. For example, to time a block of code, you might do this
Date d1 = new Date();
// timed code goes here
Date d2 = new Date();
long elapsed_time = d2.getTime() - d1.getTime();
System.out.println("That took " + elapsed_time + " milliseconds");

Ads by Google


You can change a Date by passing the new date as a number of milliseconds since midnight, January 1, 1970, GMT, to the setTime() method, like this:
Date midnight_jan2_1970 = new Date();
midnight_jan2_1970.setTime(24L*60L*60L*1000L);

Previous - Java Strings                                                          Next - Java Inheritance