Posted By:
Pavan_Gattu
Posted On:
Wednesday, September 8, 2004 02:18 AM
You can get the current time using the "System" class which returns "long" value, which is the number of milli seconds .
Construct the "Date" object using that milliseconds.
This Date object can be parsed to any format you require using "SimpleDateFormat" class.
Check the API's for the following classes and you can get additional details from the site
http://java.sun.com/j2se/1.4.2/docs/api/index.html
System, Date, DateFormat, SimpleDateFormat Sample code is given below...
import java.util.Date;
import java.text.SimpleDateFormat;
public class TestDate
{
public static void main (String args[])
{
//Current Time
long currentTime = System.currentTimeMillis();
System.out.println("Current Time in Millis: " + currentTime);
//Current Time converted to Date Object
Date current_date_time = new Date(currentTime);
//Format the date to any pattern you want
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
String current_date_time_formatted1 = sdf.format(current_date_time);
System.out.println("Current Date Time Formatted: " + current_date_time_formatted1);
//Pass the date to a function
someFunction(current_date_time);
}
public static void someFunction(Date now)
{
//Format the date to any pattern you want
SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy HH:mm:ss");
String current_date_time_formatted2 = sdf.format(now);
System.out.println("Current Date Time Formatted: " + current_date_time_formatted2);
}
}
Hope this helps. Good luck.