What's the best way to get the current date/time in Java?
There is no "best" way.
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 Javalong). This value is a delta from a UTC time-point, and is independent of the local time-zone1.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 aDateobject initialized with the current date / time. The problem is that theDateAPI methods are mostly flawed ... and deprecated.Calendar.getInstance()gives you aCalendarobject initialized with the current date / time, using the defaultLocaleandTimeZone. Other overloads allow you to use a specificLocaleand/orTimeZone. 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. https://stackoverflow.com/questions/6280829.)in Java 8, calling
java.time.LocalDateTime.now()andjava.time.ZonedDateTime.now()will give you representations2 for the current date / time.
Prior to Java 8, most people who know about these things recommended Joda-time as having (by far) the best Java APIs for doing things involving time point and duration calculations.
With Java 8 and later, the standard java.time package is recommended. Joda time is now considered "obsolete", and the Joda maintainers are recommending that people migrate3.
Note: the Calendar, org.joda.time and java.time solutions can use either the platform's default timezone or an explicit timezone provided via constructor arguments. Generally, using an explicit timezone rather than the default zone will make your application's behavior more predictable / less susceptible to problems if (for example) you redeploy to a data center in a different timezone.
But no matter what you do, you (and maybe your application) should be aware that the timezone of the user, your service and the data center can all be different. The concept of the "current date/time" is complicated.
1 - System.currentTimeMillis() gives the "system" time. While it is normal practice for the system clock to be set to (nominal) UTC, there will be a difference (a delta) between the local UTC clock and true UTC. The size of the delta depends on how well (and how often) the system's clock is synced with UTC.
2 - Note that LocalDateTime doesn't include a time zone. As the javadoc says: "It cannot represent an instant on the time-line without additional information such as an offset or time-zone."
3 - Note: your Java 8 code won't break if you don't migrate, but the Joda codebase may eventually stop getting bug fixes and other patches. As of 2020-02, an official "end of life" for Joda has not been announced, and the Joda APIs have not been marked as Deprecated.
What's the best way to get the current date/time in Java?
There is no "best" way.
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 Javalong). This value is a delta from a UTC time-point, and is independent of the local time-zone1.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 aDateobject initialized with the current date / time. The problem is that theDateAPI methods are mostly flawed ... and deprecated.Calendar.getInstance()gives you aCalendarobject initialized with the current date / time, using the defaultLocaleandTimeZone. Other overloads allow you to use a specificLocaleand/orTimeZone. 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. https://stackoverflow.com/questions/6280829.)in Java 8, calling
java.time.LocalDateTime.now()andjava.time.ZonedDateTime.now()will give you representations2 for the current date / time.
Prior to Java 8, most people who know about these things recommended Joda-time as having (by far) the best Java APIs for doing things involving time point and duration calculations.
With Java 8 and later, the standard java.time package is recommended. Joda time is now considered "obsolete", and the Joda maintainers are recommending that people migrate3.
Note: the Calendar, org.joda.time and java.time solutions can use either the platform's default timezone or an explicit timezone provided via constructor arguments. Generally, using an explicit timezone rather than the default zone will make your application's behavior more predictable / less susceptible to problems if (for example) you redeploy to a data center in a different timezone.
But no matter what you do, you (and maybe your application) should be aware that the timezone of the user, your service and the data center can all be different. The concept of the "current date/time" is complicated.
1 - System.currentTimeMillis() gives the "system" time. While it is normal practice for the system clock to be set to (nominal) UTC, there will be a difference (a delta) between the local UTC clock and true UTC. The size of the delta depends on how well (and how often) the system's clock is synced with UTC.
2 - Note that LocalDateTime doesn't include a time zone. As the javadoc says: "It cannot represent an instant on the time-line without additional information such as an offset or time-zone."
3 - Note: your Java 8 code won't break if you don't migrate, but the Joda codebase may eventually stop getting bug fixes and other patches. As of 2020-02, an official "end of life" for Joda has not been announced, and the Joda APIs have not been marked as Deprecated.
(Attention: only for use with Java versions <8. For Java 8+ check other replies.)
If you just need to output a time stamp in format YYYY.MM.DD-HH.MM.SS (very frequent case) then here's the way to do it:
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
Videos
If you want a blank date, just use \date{}.
If you want to remove the spacing altogether where the date usually goes, look into using the titling package, or else the mechanisms provided by your document class, depending on what it is.
For a cheap and easy solution to removing the spacing without delving into the titling package that frabjous mentions, try:
\documentclass{article}
\author{Me}
\title{Foo}
\date{\vspace{-5ex}}
%\date{} % Toggle commenting to test
\begin{document}
\maketitle
Bar
\end{document}