Java code for getting current time [duplicate]
I am searching code in java
for fetching or synchronizing my local PC system time into my application.
Try this:
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class currentTime {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
System.out.println( sdf.format(cal.getTime()) );
}
}
You can format SimpleDateFormat in the way you like. For any additional information you can look in java api:
SimpleDateFormat
Calendar
Both
new java.util.Date()
and
System.currentTimeMillis()
will give you current system time.
tl;dr
Instant.now() // UTC
…or…
ZonedDateTime.now(
// Specify time zone.
ZoneId.of( "Pacific/Auckland" )
)
Details
The bundled java.util.Date
/.Calendar
classes are notoriously troublesome. Avoid them. They are now legacy, supplanted by the java.time framework.
Instead, use either:
-
java.time
Built-in with Java 8 and later. Official successor to Joda-Time.
Back-ported to Java 6 & 7 and to Android. -
Joda-Time
Third-party library, open-source, free-of-cost.
java.time
ZonedDateTime zdt = ZonedDateTime.now();
If needed for old code, convert to java.util.Date. Go through at Instant
which is a moment on the timeline in UTC.
java.util.Date date = java.util.Date.from( zdt.toInstant() );
Time Zone
Better to specify explicitly your desired/expected time zone rather than rely implicitly on the JVM’s current default time zone.
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.now( zoneId ); // Pass desired/expected time zone.
Joda-Time
FYI, the Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes.
DateTime now = DateTime.now();
To convert from a Joda-Time DateTime object to a java.util.Date for inter-operating with other classes…
java.util.Date date = now.toDate();
Search StackOverflow before posting. Your question has already been asked and answered.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
-
Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
-
Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
-
Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
System.currentTimeMillis()
everything else works off that.. eg new Date() calls System.currentTimeMillis().