Java localtime from date

Convert Date to LocalDate or LocalDateTime and Back

announcement - icon

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:

announcement - icon

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:

announcement - icon

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.

announcement - icon

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. Overview

Starting with Java 8, we have a new Date API: java.time.

However, sometimes we still need to perform conversions between the new and old APIs, and work with date representations from both.

Further reading:

Migrating to the New Java 8 Date Time API

Introduction to the Java 8 Date/Time API

In this article we will take a look at the new Java 8 APIs for Date and Time and how much easier it is to construct and manipulate dates and times.

2. Converting java.util.Date to java.time.LocalDate

Let’s start with converting the old date representation to the new one.

Here, we can take advantage of a new toInstant() method, which was added to java.util.Date in Java 8.

When we’re converting an Instant object, it’s required to use a ZoneId because Instant objects are time-zone agnostic — just points on the timeline.

The atZone(ZoneId zone) API from Instant object returns a ZonedDateTime, so we just need to extract LocalDate from it using the toLocalDate() method.

First, we’re using the default system ZoneId:

public LocalDate convertToLocalDateViaInstant(Date dateToConvert)

And a similar solution but with a different way of creating an Instant object — using the ofEpochMilli() method:

public LocalDate convertToLocalDateViaMilisecond(Date dateToConvert)

Before we move on, let’s also have a quick look at the old java.sql.Date class and how that can be converted to a LocalDate as well.

Starting with Java 8, we can find an additional toLocalDate() method on java.sql.Date, which also gives us an easy way of converting it to java.time.LocalDate.

In this case, we don’t need to worry about the time zone:

public LocalDate convertToLocalDateViaSqlDate(Date dateToConvert)

Very similarly, we can convert an old Date object into a LocalDateTime object as well. Let’s have a look at that next.

3. Converting java.util.Date to java.time.LocalDateTime

To get a LocalDateTime instance, we can similarly use an intermediary ZonedDateTime and then use the toLocalDateTime() API.

Just like before, we can use two possible solutions to get an Instant object from java.util.Date:

public LocalDateTime convertToLocalDateTimeViaInstant(Date dateToConvert) < return dateToConvert.toInstant() .atZone(ZoneId.systemDefault()) .toLocalDateTime(); >public LocalDateTime convertToLocalDateTimeViaMilisecond(Date dateToConvert)

Note that for dates before Oct 10, 1582, it’s necessary to set up Calendar as a Gregorian calendar and call method setGregorianChange():

GregorianCalendar calendar = new GregorianCalendar(); calendar.setGregorianChange(new Date(Long.MIN_VALUE)); Date dateToConvert = calendar.getTime();

And starting with Java 8, we can also use java.sql.Timestamp to obtain a LocalDateTime:

ocalDateTime convertToLocalDateTimeViaSqlTimestamp(Date dateToConvert)

4. Convert java.time.LocalDate to java.util.Date

Now that we have a good understanding of how to convert from the old data representation to the new one, let’s have a look at converting in the other direction.

We’ll discuss two possible ways of converting LocalDate to Date.

In the first, we use a new valueOf(LocalDate date) method provided in java.sql.Date object, which takes LocalDate as a parameter:

public Date convertToDateViaSqlDate(LocalDate dateToConvert)

As we can see, it is effortless and intuitive. It uses local time zone for conversion (all is done under the hood, so no need to worry).

In another Java 8 example, we use an Instant object that we pass to the from(Instant instant) method of java.util.Date object:

public Date convertToDateViaInstant(LocalDate dateToConvert)

Notice we make use of an Instant object here and that we also need to care about time zones when doing this conversion.

Next, let’s use a very similar solution to convert a LocalDateTime to a Date object.

5. Convert java.time.LocalDateTime to java.util.Date

The easiest way of getting a java.util.Date from LocalDateTime is to use an extension to the java.sql.Timestamp — available with Java 8:

public Date convertToDateViaSqlTimestamp(LocalDateTime dateToConvert)

But of course, an alternative solution is using an Instant object, which we obtain from ZonedDateTime:

Date convertToDateViaInstant(LocalDateTime dateToConvert)

6. Java 9 Additions

In Java 9, there are new methods available that simplify conversion between java.util.Date and java.time.LocalDate or java.time.LocalDateTime.

LocalDate.ofInstant(Instant instant, ZoneId zone) and LocalDateTime.ofInstant(Instant instant, ZoneId zone) provide handy shortcuts:

public LocalDate convertToLocalDate(Date dateToConvert) < return LocalDate.ofInstant( dateToConvert.toInstant(), ZoneId.systemDefault()); >public LocalDateTime convertToLocalDateTime(Date dateToConvert)

7. Conclusion

In this article, we covered possible ways of converting old java.util.Date into new java.time.LocalDate and java.time.LocalDateTime, as well as the other way around.

The full implementation of this article is available over on GitHub.

announcement - icon

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:

Источник

Читайте также:  Chi 2 contingency python
Оцените статью