Link to home
Start Free TrialLog in
Avatar of kgsoft_sys
kgsoft_sys

asked on

Generating Current time...

Dear expert...

can any of you provide me with the code how to generate current time in java...i need it urgently..thank !

from : Alex Gan
ASKER CERTIFIED SOLUTION
Avatar of Ovi
Ovi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ovi
Ovi

public class DateGenerator {
  public static void main(String[] args) {
    System.out.println("Curent date is : " + (new Date()).toString());
  }
}

Compile this and run'it.
Avatar of Jim Cakalic
System.currentTimeMillis() produces the current time in milliseconds with a resolution dependent upon your the native system's clock.

java.util.Date wraps the millisecond value and provides some comparison behavior although most of its useful behavior has been deprecated and moved to java.util.Calendar and its concrete subclasses. The no-arg constructor of Date initializes the new Date object with the current time provided by System.currentTimeMillis().

java.util.Calendar provides facilities for getting and setting specific date parts (like seconds, months, years) and for performing date arithmetic.

java.text.DateFormat and its more useful class java.text.SimpleDateFormat perform Date formatting to Strings and parsing from Strings.

Taken together, one or more of these classes is likely to have the functionality you are looking for with respect to "generating the current time".

Best regards,
Jim Cakalic
DateFormat dateFormat = DateFormat.getDateInstance();
Calendar calender = dateFormat.getCalendar();
System.out.println("time"+calender.getTime());