Get Current Date and Time in Java
Learn to get the current date and/or time in Java. Note that the appropriate way to handle date-time information is different before and after JDK 8.
- For JDK 8 or later, the recommended way is to use LocalDate and LocalTime classes.
- For JDK 7 or earlier, we can use of Date and Calendar classes only.
1. Get Current Date and Time (Java 8 or Later)
In Java 8 or later, the date and time information is represented by the following classes. These classes provide the current date and time locally to the user, and there is no timezone information is associated with it.
- java.time.LocalDate – Represents the Date only information in yyyy-MM-dd pattern.
- java.time.LocalTime – Represents the Time only information in HH:mm:ss.SSSSSSSSS pattern.
- java.time.LocalDateTime – Represents the Date and Time informations, both, without any timezone information. The pattern is the combination of local date and time information.
To get the current date and time information in another timezone/locale, we can use the following classes.
The following code shows how to get the current date-time information using the now() method in each class. The now() method returns an immutable and thread-safe instance of the class for which it is invoked.
// Get current date LocalDate today = LocalDate.now(); System.out.println(today); //2022-02-15 // Get current time LocalTime now = LocalTime.now(); System.out.println(now); //12:43:51.519251200 // Get current date and time LocalDateTime instance = LocalDateTime.now(); System.out.println(instance); //2022-02-15T12:43:51.519251200
To get the timezone-specific date and time information, pass the zone information in the ZonedDateTime.now() method.
// Get current date and time in GMT ZonedDateTime tzInstance = ZonedDateTime.now(ZoneId.of("GMT")); System.out.println(tzInstance); //2022-02-15T07:13:51.519251200Z[GMT]
1.3. Display Formatted Date and Time
To default string representations of the above classes are fixed. If we want to display the information in some custom pattern, we can use DateTimeFormatter class.
LocalDateTime instance = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy hh:mm"); String formattedString = formatter.format(instance); //15-02-2022 12:43
2. Get Current Date and Time (Java 7 or Earlier)
Till version 7 or earlier, Java didn’t have separate classes for the date and time parts. The main classes were :
Let us have a quick look at the methods used for getting the current date and time information in legacy Java classes.
Date date = new Date(); System.out.println(date); //Tue Feb 15 13:00:31 IST 2022 Calendar cal = Calendar.getInstance(); System.out.println(cal);
2.3. Display Formatted Date and Time
To display, the date-time in a custom formatted manner, we should use SimpleDateFormat class.
Date date = new Date(); Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm"); System.out.println(sdf.format(date)); //15-02-2022 01:04 System.out.println(sdf.format(cal.getTime())); //15-02-2022 01:04
Get the Current Date and Time in Java
The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.
To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.
Connect your cluster and start monitoring your K8s costs right away:
We rely on other people’s code in our own work. Every day.
It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.
The problem is, of course, when things fall apart in production — debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky.
Lightrun is a new kind of debugger.
It’s one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics.
Learn more in this quick, 5-minute Lightrun tutorial:
Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.
The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.
Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.
Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:
DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema.
The way it does all of that is by using a design model, a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.
And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.
The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.
To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.
Connect your cluster and start monitoring your K8s costs right away:
We’re looking for a new Java technical editor to help review new articles for the site.
1. Introduction
This quick article describes how we may get the current date, current time and current time stamp in Java 8.
2. Current Date
First, let’s use java.time.LocalDate to get the current system date:
LocalDate localDate = LocalDate.now();
To get the date in any other timezone we can use LocalDate.now(ZoneId):
LocalDate localDate = LocalDate.now(ZoneId.of("GMT+02:30"));
We can also use java.time.LocalDateTime to get an instance of LocalDate:
LocalDateTime localDateTime = LocalDateTime.now(); LocalDate localDate = localDateTime.toLocalDate();
3. Current Time
With java.time.LocalTime, let’s retrieve the current system time:
LocalTime localTime = LocalTime.now();
To get the current time in a specific time zone, we can use LocalTime.now(ZoneId):
LocalTime localTime = LocalTime.now(ZoneId.of("GMT+02:30"));
We can also use java.time.LocalDateTime to get an instance of LocalTime:
LocalDateTime localDateTime = LocalDateTime.now(); LocalTime localTime = localDateTime.toLocalTime();
4. Current Timestamp
Use java.time.Instant to get a time stamp from the Java epoch. According to the JavaDoc, “epoch-seconds are measured from the standard Java epoch of 1970-01-01T00:00:00Z, where instants after the epoch have positive values:
Instant instant = Instant.now(); long timeStampMillis = instant.toEpochMilli();
We may obtain the number of epoch-seconds seconds:
Instant instant = Instant.now(); long timeStampSeconds = instant.getEpochSecond();
5. Conclusion
In this tutorial we’ve focused using java.time.* to get the current date, time and time stamp.
As always, the code for the article is available over on GitHub.
Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.
The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.
Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.
Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes: