Questions tagged [java-time]

The java.time package is a modern date-time framework that debuted in Java 8, defined by JSR 310, supplanting the java.util.Date and .Calendar classes. Inspired by Joda-Time, but re-architected. It uses the ISO 8601 standard as the default for parsing and generating strings and includes classes to represent date-only, time-of-day-only, and span-of-time values.

Instant

A numeric timestamp, stored with nanosecond resolution. Useful for capturing a point in time, similar to System.currentTimeMillis(). Instant is the closest equivalent class to java.util.Date. The instant when printed looks like 2000-12-01T12:30:00.000Z.

LocalDate

A date without a time, offset or time zone. Useful for storing a birthday for example. The date when printed looks like 2000-12-01.

LocalTime

A time without a date, offset or time zone. Useful for storing store hours for example. The time when printed looks like 12:30:00.000.

LocalDateTime

A date and time without the offset or time zone. The date and time when printed looks like 2000-12-01T12:30:00.000.

ZonedDateTime

A date and time with offset and time zone. Useful for performing calculations that take into account the time zone like America/New_York. ZonedDateTime is the closest equivalent class to java.util.GregorianCalendar. The date and time when printed looks like 2000-12-01T12:30:00.000-05:00[America/New_York] It is recommended whenever possible, to use simpler classes without a time zone to model the domain, like LocalDate, LocalTime and LocalDateTime. The widespread use of time zones tends to add considerable complexity to an application. Many applications can use the simpler classes, with the time zone added only at the user interface layer. Other notable classes in the Java Time API are:

Clock

A clock providing access to the current instant, date and time using a time zone. A clock can be used instead of System.currentTimeMillis() and TimeZone.getDefault(). Although all key date and time classes have a now() factory method that uses the system clock, the primary purpose of this abstraction is to allow alternate clocks to be injected, which can greatly simplify testing.

Duration

A duration between two instants on the time-line, stored with nanosecond resolution. This class models a duration of time and is not tied to any instant. The model is directed, meaning that the duration can be negative. The duration when printed looks like PT3600S.

Period

A period of time expressed in units meaningful to humans, such as '1 Year, 2 Months and 3 Days'. The model is directed, meaning that individual parts of the period may be negative. The period when printed looks like P1Y2M3D.

ZoneId

A time zone ID, such as America/New_York.

ZoneOffset

A time zone offset from Greenwich/UTC, such as +02:00.

Documentation

Joda-Time

For similar usage pre-Java 8, see Joda-Time Library:

1742 questions
611
votes
9 answers

Converting between java.time.LocalDateTime and java.util.Date

Java 8 has a completely new API for date and time. One of the most useful classes in this API is LocalDateTime, for holding a timezone-independent date-with-time value. There are probably millions of lines of code using the legacy class…
Knut Arne Vedaa
  • 15,372
  • 11
  • 48
  • 59
601
votes
14 answers

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

What is the best way to convert a java.util.Date object to the new JDK 8/JSR-310 java.time.LocalDate? Date input = new Date(); LocalDate date = ???
JodaStephen
  • 60,927
  • 15
  • 95
  • 117
472
votes
11 answers

How can I parse/format dates with LocalDateTime? (Java 8)

Java 8 added a new java.time API for working with dates and times (JSR 310). I have date and time as string (e.g., "2014-04-08 12:30"). How can I obtain a LocalDateTime instance from the given string? After I finished working with the LocalDateTime…
micha
  • 47,774
  • 16
  • 73
  • 80
453
votes
13 answers

Convert java.time.LocalDate into java.util.Date type

I want to convert java.time.LocalDate into java.util.Date type. Because I want to set the date into JDateChooser. Or is there any date chooser that supports java.time dates?
Kavinda Gehan
  • 4,668
  • 2
  • 17
  • 20
420
votes
14 answers

How to get milliseconds from LocalDateTime in Java 8

I am wondering if there is a way to get current milliseconds since 1-1-1970 (epoch) using the new LocalDate, LocalTime or LocalDateTime classes of Java 8. The known way is below: long currentMilliseconds = new Date().getTime(); or long…
Georgios Syngouroglou
  • 18,813
  • 9
  • 90
  • 92
366
votes
14 answers

Calculate days between two Dates in Java 8

I know there are lots of questions on SO about how to get Dates in Java, but I want an example using new Java 8 Date API. I also know about the JodaTime library, but I want a method without relying on external libraries. The function needs to be…
Marcos
  • 4,827
  • 3
  • 20
  • 28
352
votes
25 answers

serialize/deserialize java 8 java.time with Jackson JSON mapper

How do I use Jackson JSON mapper with Java 8 LocalDateTime? org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class java.time.LocalDateTime] from JSON String; no single-String constructor/factory method…
Alexander Taylor
  • 16,574
  • 14
  • 62
  • 83
352
votes
7 answers

UnsupportedTemporalTypeException when formatting Instant to String

I'm trying to format an Instant to a String using the new Java 8 Date and Time API and the following pattern: Instant instant = ...; String out = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(instant); Using the code above I get an…
Dag
  • 10,079
  • 8
  • 51
  • 74
316
votes
8 answers

How can I create a Java 8 LocalDate from a long Epoch time in Milliseconds?

I have an external API that returns me dates as longs, represented as milliseconds since the beginning of the Epoch. With the old style Java API, I would simply construct a Date from it with Date myDate = new Date(startDateLong) What is the…
Vihung
  • 12,947
  • 16
  • 64
  • 90
300
votes
4 answers

Differences between Java 8 Date Time API (java.time) and Joda-Time

I know there are questions relating to java.util.Date and Joda-Time. But after some digging, I couldn't find a thread about the differences between the java.time API (new in Java 8, defined by JSR 310) and Joda-Time. I have heard that Java 8’s…
Zack
  • 5,108
  • 4
  • 27
  • 39
215
votes
1 answer

What is the difference between ZoneOffset.UTC and ZoneId.of("UTC")?

Why does ZonedDateTime now = ZonedDateTime.now(); System.out.println(now.withZoneSameInstant(ZoneOffset.UTC) .equals(now.withZoneSameInstant(ZoneId.of("UTC")))); print out false? I would expect the both ZonedDateTime instances to be equal.
Johannes Flügel
  • 3,112
  • 3
  • 17
  • 32
202
votes
2 answers

What's the difference between ZonedDateTime and OffsetDateTime?

I've read the documentation, but I still can't get when I should use one or the other: OffsetDateTime ZonedDateTime According to documentation OffsetDateTime should be used when writing date to database, but I don't get why.
Zhenya
  • 6,020
  • 6
  • 34
  • 42
184
votes
3 answers

How to convert java.sql.timestamp to LocalDate (java8) java.time?

In Java 8, how can I convert a Timestamp (in java.sql) to a LocalDate (in java.time)?
simonides
  • 3,040
  • 3
  • 19
  • 34
184
votes
13 answers

Get first and last day of month using threeten, LocalDate

I have a LocalDate which needs to get the first and last day of the month. How do I do that? eg. 13/2/2014 I need to get 1/2/2014 and 28/2/2014 in LocalDate formats. Using threeten LocalDate class.
user1746050
  • 2,415
  • 5
  • 20
  • 26
182
votes
7 answers

LocalDate to java.util.Date and vice versa simplest conversion?

Is there a simple way to convert a LocalDate (introduced with Java 8) to java.util.Date object? By 'simple', I mean simpler than this: Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); which seems a bit awkward…
George
  • 7,206
  • 8
  • 33
  • 42
1
2 3
99 100