I am trying to send a date via JSON using a format like "date":"2018-01-03"
but in my Java code I get 2018-01-03 02:00:00
and not 2018-01-03 00:00:00
as I would expect. Seems like it is adding some timezone to my date. Is this alright or am I missing something?

- 68
- 2
- 5
-
1how do you convert your string from Json to the Java Date ? Or, how do you trnasform your json string to a Java object ? – Stéphane Ammar Jan 03 '18 at 17:04
-
2If you convert it to `LocalDate` no time will be included. – Mordechai Jan 03 '18 at 17:06
-
the java code is actually an API that receives a json which contains a date and i am using spring framework for this. i annotate the controller parameter to which the json should be converted with `@RequestBody` so i don't really do any conversion myself. – Cosmin Jan 04 '18 at 12:00
2 Answers
To represent a date-only value, use a date-only type rather than a date+time-of-day type.
LocalDate
LocalDate
represents a date without a time-of-day and without a time zone.
LocalDate ld = LocalDate.of( "2018-01-03" ) ;
ZonedDateTime
To get the first moment of the day, specify a time zone.
ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
ZonedDateTime zdt = ld.atStartOfDay( z ) ;
Instant
To view that same moment in UTC, extract an Instant
object from the ZonedDateTime
.
Instant instant = zdt.toInstant() ;
These topics have been discussed many many times already. Search Stack Overflow for more info.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.

- 303,325
- 100
- 852
- 1,154
-
until i find something better i'll use Joda-Time's `DateTime()`. i.e. `Date myDate = new DateTime(someDateFromJson).withTimeAtStartOfDay().toDate();`. this way i'll have `2018-01-03 00:00:00` in java when sending `"date":"2018-01-03"` via json – Cosmin Jan 04 '18 at 12:06
-
@CosminREZMERITA FYI, the Joda-Time project, is now in maintenance mode, with the team advising migration to the java.time classes. Also, if you intend to represent a specific moment on the timeline, you'll likely regret ignoring the time zone or offset. – Basil Bourque Jan 04 '18 at 16:57
From the Java API "The class Date represents a specific instant in time, with millisecond precision." When you create a Date you automatically get a date with a time. If you want to send just the date you have some options: 1. Convert the date to a string on the server side using the desired format. 2. On the client side ignore the time. 3. On the server side, zero the time fields, using methods such as setMinutes(0). But please note that these methods are deprecated in favor of Calendar methods, and further the old Date and Calendar classes are replaced by the Java 8 date and time classes.

- 2,159
- 17
- 27