I will expand on the correct answer by JeroenWyseur.
ISO 8601
The ISO 8601 standard format is absolutely the best way to serialize a date-time value for data exchange. The format is unambiguous, intuitive to peoples across cultures, and increasingly common around the world. Easy to read for both humans and machines.
2015-01-16T20:15:43+02:00
2015-01-16T18:15:43Z
The first example has an offset of two hours ahead of UTC. The second example shows the common use of Z
("Zulu") to indicate UTC, short for +00:00
.
java.time
The java.util.Date & .Calendar classes bundled with Java are notoriously troublesome, confusing, and flawed. Avoid them. Instead use:
- java.time package, built into Java 8, inspired by Joda-Time, defined by JSR 310.
The java.time package supplants its predecessor, the Joda-Time library.
By default, both libraries use ISO 8601 for both parsing and generating String representations of date-time values.
Note that java.time extends the ISO 8601 format by appending the proper name of the time zone, such as 2007-12-03T10:15:30+01:00[Europe/Paris]
.
Search StackOverflow.com for many hundreds of Questions and Answers with much discussion and example code.
Avoid Count-From-Epoch
Some of the other answers recommend using a number, a count from epoch. This approach is not practical. It is not self-evident. It is not human-readable, making debugging troublesome and frustrating.
Which number is it, whole seconds as commonly used in Unix, milliseconds used in java.util.Date & Joda-Time, microseconds commonly used in databases such as Postgres, or nanoseconds used in java.time package?
Which of the couple dozen epochs, first moment of 1970 used in Unix, year 1 used in .Net & Go, "January 0, 1900" used in millions (billions?) of Excel & Lotus spreadsheets, or January 1, 2001 used by Cocoa?

See my answer on a similar question for more discussion.
LocalDate
I'm passing around some objects through web service and some of them contain java.sql.Date
The replacement for the terrible java.sql.Date
class is java.time.LocalDate
.
Best to avoid the legacy class entirely, but you can convert back and forth by calling new methods added to the old class: myJavaSqlDate.toLocalDate()
Serializing LocalDate
The LocalDate
class implements Serializable
. So you should have no problem with it automatically serializing, both marshaling and unmarshalling.
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
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
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.