java getTime

java getTime

To get the current time in Java, you can use the getInstance and getTime methods of the Calendar class.

Here is an example of how to get the current time:

Calendar calendar = Calendar.getInstance();
Date time = calendar.getTime();
‮ww:ecruoS‬w.lautturi.com

The getInstance method returns a Calendar object initialized with the current date and time. The getTime method returns the Date object representing the date and time represented by the Calendar object.

The Date class represents a specific point in time, and provides methods for accessing and formatting the date and time.

For example:

long timestamp = time.getTime(); // returns the timestamp (milliseconds since 1970-01-01 00:00:00 UTC)
String str = time.toString(); // returns a string representation of the date and time (e.g. "Mon Dec 26 20:20:50 PST 2022")

Note that the Calendar class represents dates and times in the default time zone of the JVM. If you need to get the time in a different time zone, you can use the getInstance method that takes a TimeZone object as an argument.

For example:

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Date time = calendar.getTime();

This will get the current time in the GMT time zone.

Created Time:2017-11-03 22:21:12  Author:lautturi