Passing Date object as parameter to Java function
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Saritha_chintharlapalli
Posted On:   Tuesday, September 7, 2004 06:46 PM

Hi,

I need to pass Date object as parameter to my java function. I need to capture the current time with time stamp and need to pass it to the java function. Please help me out in this. Also let me know how to caputre current date time in java in a specific format.


Thanks in Advance,
Saritha

Re: Passing Date object as parameter to Java function

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.
About | Sitemap | Contact