The LocalDateTime. now() method returns the instance of LocalDateTime class. If we print the instance of LocalDateTime class, it prints the current date and time. To format the current date, you can use DateTimeFormatter class which is included in JDK 1.8.
You can get the time from the LocaldateTime object using the toLocalTime() method. Therefore, another way to get the current time is to retrieve the current LocaldateTime object using the of() method of the same class. From this object get the time using the toLocalTime() method.
Core Java The class Date represents a specific instant in time, with millisecond precision. To find out if two Date objects contain the same day, we need to check if the Year-Month-Day is the same for both objects and discard the time aspect.
System. out. printf("%d-%02d-%02d %02d:%02d:%02d. %03d", year, month, day, hour, minute, second, millis);
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.
Instant.now() // UTC
…or…
ZonedDateTime.now(
// Specify time zone.
ZoneId.of( "Pacific/Auckland" )
)
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:
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() );
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.
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.
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?
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().
Try this way, more efficient and compatible:
SimpleDateFormat time_formatter = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss.SSS");
String current_time_str = time_formatter.format(System.currentTimeMillis());
//Log.i("test", "current_time_str:" + current_time_str);
Just to inform for furthers developers, and thankfully to Basil Bourque, I just wanna add my stone to this topic.
If you want simply get the HH:MM:SS format then do this:
LocalTime hour = ZonedDateTime.now().toLocalTime().truncatedTo(ChronoUnit.SECONDS);
Cheers.
P.S.: This will work only at least with Java 8 !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With