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,890 points

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 and will give you representations1 for the current date / time.

answered Dec 30, 2020 by Gitika
• 65,950 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,070 points
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,820 points
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,630 points
0 votes
1 answer

How to calculate the difference between two date instances in Java?

You can use Joda Time Library. Interval i ...READ MORE

answered May 4, 2018 in Java by Parth
• 4,630 points
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
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
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,170 points
+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,900 points
+15 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
0 votes
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