How to get the current date time in Java

0 votes
What's the best way to get the current date/time in Java?
Dec 30, 2020 in Java by Rajiv
• 8,870 points
1,258 views

1 answer to this question.

0 votes

It depends on what form of date/time you want:

  • If you want the date/time as a single numeric value, then System.currentTimeMillis() gives you that, expressed as the number of milliseconds after the UNIX epoch (as a Java long). This value is a delta from a UTC time-point and is independent of the local time-zone ... assuming that the system clock has been set correctly.

  • If you want the date/time in a form that allows you to access the components (year, month, etc) numerically, you could use one of the following:

    • new Date() gives you a Date object initialized with the current date/time. The problem is that the Date API methods are mostly flawed ... and deprecated.

    • Calendar.getInstance() gives you a Calendar object initialized with the current date/time, using the default Locale and TimeZone. Other overloads allow you to use a specific Locale and/or TimeZone. The calendar works ... but the APIs are still cumbersome.

    • new org.joda.time.DateTime() gives you a Joda-time object initialized with the current date/time, using the default time zone and chronology. There are lots of other Joda alternatives ... too many to describe here. (But note that some people report that Joda time has performance issues.; e.g. Jodatime's LocalDateTime is slow when used the first time.)

    • in Java 8, calling java.time.LocalDateTime.now() and java.time.ZonedDateTime.now() will give you representations1 for the current date / time.

answered Dec 30, 2020 by Gitika
• 65,770 points

Related Questions In Java

0 votes
2 answers

How do I get the current date and time using Java?

If you require a time stamp in ...READ MORE

answered Aug 23, 2019 in Java by Sirajul
• 59,230 points
2,931 views
0 votes
1 answer

How to get the current working directory in Java?

Hello, Using java.nio.file.Path and java.nio.file.Paths, you can do the following to ...READ MORE

answered May 28, 2020 in Java by Niroj
• 82,840 points
3,955 views
0 votes
0 answers

How to get the current London time and date using javascript

Using the code below, I attempted to& ...READ MORE

Dec 7, 2022 in Java by Nicholas
• 7,760 points
1,159 views
0 votes
2 answers

How can I get current time in YYYY:MM:DD HH:MI:Sec:Millisecond format in Java?

public String getCurrentTimeStamp() { ...READ MORE

answered Sep 21, 2018 in Java by Parth
• 4,640 points
7,508 views
0 votes
1 answer

How can I get the current date and time in UTC or GMT in Java?

This definitely returns UTC time: as String ...READ MORE

answered Jun 7, 2018 in Java by Rishabh
• 3,620 points
27,612 views
0 votes
1 answer

Measure time elapsed in Java?

This is also a way to work ...READ MORE

answered Jul 4, 2018 in Java by scarlett
• 1,290 points
850 views
0 votes
3 answers

Increment the date in Java by 1-day

import java.time.LocalDate; public class DateIncrementer { static ...READ MORE

answered Aug 1, 2018 in Java by Akrati
• 3,190 points
4,535 views
+1 vote
2 answers

Calculating date/time difference in Java

We can also use java.util.concurrent.TimeUnit class. long diff = d2.getTime() ...READ MORE

answered Aug 28, 2018 in Java by Sushmita
• 6,920 points
25,718 views
+17 votes
25 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7, 2019 in Java by Suresh
• 720 points
253,613 views
+1 vote
6 answers

How can I separate the digits of an int number in Java?

You can also have a look here: To ...READ MORE

answered Dec 9, 2020 in Java by Roshni
• 10,480 points
209,893 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP